Lesson 4: Decision Making and Looping.

Prev || Home || Next

In looping a sequence of statements are executed until some conditions for the termination of the loop are satisfied. A program therefore consists of two segments, one known as the body of the loop and the other known as the control statement. The control statement tests certain conditions and then directs the repeated execution of the statements contained in the body of the loop.

A looping process would include the following four steps -

1) Setting and initializing of a counter.

2) Execution of the statements in the loop.

3) Test for a specified condition for execution of the loop.

4) Increamenting or decreamenting the counter.

There are three looping constructs:-

1) The while statements.

2) The do --- while statements.

3) The for statements.

while Statements:

Syntax:

while(test condition)

{

body of the loop

}

The while is an entry controlled loop statement. The test condition is evaluated and if condition is true then the body of the loop is executed. Again test condition is evaluated and if true the body is executed once again. This process of repeated execution of the body continues until the test condition finally becomes flase and the control is transferred out of the loop. On exit, the program continues with the statement immediately after the body of the loop.

Program:- To print multiplication table of the given number.

main()

{

int n1,n2,p;

clrscr();

printf("Enter any number=");

scanf("%d",&n1);

n2=1;

while(n2<=10)

{

p=n1*n2;

printf("%d\n",p);

n2=n2+1;

}

}

do --- while Statements:

This is an exit controlled loop. In this process the body of the loop is executed first. At the end of the loop, the test condition in the while statement is evaluated. If the condition is true, the program continues to evaluate the body of the loop once again. This process continues as long as the condition is true. When the condition becomes false, the loop will be terminated and the control goes to the statement that appears immediately after the while statement.

Syntax:-

do

{

body of the loop

}

while(test condition);

Program:- To print all the numbers between 1 to 100 divisible by 3 and 5.

main()

{

int n=1;

do

{

if(n%3==0 && n%5==0)

{

printf("The number %d is divisble by 3 and 5\n",n);

n=n+1;

}

while(n<=100);

}

for Statements:

The for loop is another entry controlled loop that provides a more concise loop control structure.

Syntax:-

for(initialization;test condition; increament\decreament)

{

body of the loop

}

1) Initialization of the control variable is done first.

2) Then test condition is evaluated. If it is true, body of the loop is executed and control is transferred back to the for statement.

3) Now the control variable is increamentes or decreamented using an assignment statement and the new value of control variable is again tested to see whether it satisfies the loop condition. If the condition is satisfied the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test condition.

Nested Loop:

A loop surrounded by another loop or loops is termed as nesting of loops.

Program:- To print the given output using nested loop.

main()                                                                                         Output:-

{                                                                                                   1

int i,j;                                                                                         12

for(i=1;i<=5;i++)                                                                        123

{                                                                                                1234

for(j=1;j<=i;j++)                                                                     12345

{

printf("%d",j);

}

printf("\n);

}

}

break Statement:

The keyword break is used to jump out of the loop instantly, without waiting to get back to the conditional test. When break is encountered inside a loop, control automatically passes to the first statement after the loop.

Program:- Use break statement using for loop.

main()

{

int i;

for(i=1;i<=10;i++)

{

if(i==5)

break;

else

printf("%d\n",i);

}

printf("Hello");

}

continue Statement:

Sometimes it is needed to transfer programming control to the begining of the loop, bypassing the statements inside the loop which have not yet executed. Keyword continue allows to do this. When continue is encountered inside the loop, control automatically passes to the begining of the loop.

Program:- Use of continue using for loop.

main()

{

int i;

for(i=1;i<=10;i++)

{

if(i==5)

continue;

else

printf("%d\n",i);

}

printf("Hello");

}

Prev || Home || Next