Looping Control Statements
Looping Control Statements
A Looping control statement is a control statement that allows the repeated execution of a group of
statements, until a certain specified condition is satisfied. A looping control statement is also called as an
iterative control statement or repetitive control statement.
Loop is the repeated execution of a group of statements (or loop body), until a certain specified condition
is satisfied. At some stage, the condition should be dissatisfied. Then only, the loop gets terminated or
ended. Otherwise, the loop becomes indefinite.
Loop counter is a variable that controls the iterations of a loop. Usually, it holds a sequence of values
that are of incremented or decremented. If it holds all the incremented values starting from initial value,
then it is incremented loop counter. If it holds all the decremented values starting from initial value, then
it is decremented loop counter.
Each of these looping control statements should consist of following three essential statements for efficient
looping mechanism:
Initialization statement: This statement is used to set the initial value for the loop counter.
Condition: This statement is used to determine whether the group of statements is to be executed
or not.
Updating statement: This statement is used to increment or decrement the loop counter.
1) While statement: It is one of the looping control statements. It is also called as entry-controlled
looping control statement. i.e., it tests the condition before entering into the loop body. The syntax
for “while” statement is as follows:
Initialization statement;
while(condition)
{
statement(s);
}
next_statement;
In this syntax,
while is the keyword. condition is a relational expression or a compound relational expression or
any expression that returns either true or false. initialization statement, statement(s) and
next_statement are valid ‘c’ statements. The statements with in the curly braces are called as
while loop body. The updating statement should be included with in the while loop body.
Whenever “while” statement is encountered, the initialization statement gets executed first.
After then, the condition will be tested.
If the condition returns the value true, the control enters into the while loop body and all
the statements in that body will be executed. When the end of the body is reached, the condition is
tested again with the updated loop counter value. If the condition returns the value true, the above
said process will be repeated.
If the condition returns the value false, the control transfers to next_statement with out
executing while loop body.
Initialization statement
Initialization statement
false true
conditio
n
Next_statement
Ex: write a program to calculate the sum of n numbers
/*program to calculate the sum of n numbers*/
/*1+2+3+4+5+…+n*/
#include<stdio.h>
main()
{
int i,n,sum;
printf(“\n Enter n value:”);
scanf(“%d”,&n);
i=1;
sum=0;
do
{
sum=sum+i;
i=i+1;
} while(i<=n);
printf(“\n sum of %d numbers=%d”,n,sum);
}
3) for statement: It is one of the looping control statements. It is also called as entry-controlled
looping control statement. i.e., it tests the condition before entering into the loop body. The syntax
for “for” statement is as follows:
for(exp1;exp2;exp3)
{
statement(s);
}
next_statement;
In this syntax,
for is a keyword. exp1 is the initialization statement. If there is more than one statement, then
those should be separated with commas. exp2 is the condition. It is a relational expression or a
compound relational expression or any expression that returns either true or false. The statements
with in the curly braces are called as “for” loop body. The exp3 is the updating statement. If there
is more than one statement, then those should be separated with commas. exp1, exp2 and exp3
should be separated with two semi-colons. statement(s) and next_statement are valid ‘c’
statements.
Whenever “for” statement is encountered, first exp1 gets executed. After then, exp2 is
tested. If it returns the value true, the control enters into for loop body. When the end of the body
is reached, exp3 is executed. Again, exp2 is tested. If the exp2 returns the value true, the above
said procedure will be repeated.
If exp2 is false, the control transfers to next_statement with out executing the for loop
body.
exp1
false true
exp2
exp3
Next_statement
Jumping control statements are the control statements those transfer the control to the specified location
or out of the loop or to the beginning of the loop. There are 3 jumping control statements:
1) break statement: The “break” statement is used with in the looping control statements, switch
statement and nested loops. When it is used with the for, while or do-while statements, the
control comes out of the corresponding loop and continues with the next statement. When it is
used in the nested loops or switch statements, the control comes out of that loop / switch
statement within which it is used. But, it does not come out of the complete nesting. The syntax for
the “break” statement is:
break;
In this syntax, break is the keyword. The following diagram shows the transfer of control when
break statement is used:
Any loop
{
statement_1;
statement_2;
:
break;
:
}
next_statement
Ex: write a program to count the number of positive, negative and zero values
/*program to calculate count the number of positive, negative and zero values */
#include<stdio.h>
main()
{
char ch;
int n,pc,nc,zc;
pc=nc=zc=0;
while(1)
{
printf(“\n Enter any number (+ve,-ve or zero):”);
scanf(“%d”,&n);
if(n>0)
pc++;
else if(n<0)
nc++;
else
zc++;
printf(“\n Do you want to continue (y/n):”);
ch=getchar();
if((ch=getchar())==’n’)
break;
}
printf(“\n No.of positive numbers=%d”,pc);
printf(“\n No.of negative numbers=%d”,nc);
printf(“\n No.of zeros=%d”,zc);
}
2) continue statement: A continue statement is used within loops to end the execution of the
current iteration and proceed to the next iteration. It provides a way of skipping the remaining
statements in that iteration after the continue statement. It is important to note that a continue
statement should be used only in loop constructs and not in selective control statements. The
syntax for continue statement is:
continue;
where continue is the keyword. The following diagram shows the transfer of control when
continue statement is used:
Any loop
{
statement_1;
statement_2;
:
continue;
:
}
next_statement
Ex: write a program to find the sum of the series 5 2+102+152+202+… upto n terms
/*program to find sum of series 5*5+10*10+15*15+20*20+… upto n terms*/
#include<stdio.h>
main()
{
int i,sum=0;
printf(“\n Enter n value:”);
scanf(“%d”,&n);
for(i=5;i<=n;i++)
{
If(i%5!=0)
continue;
sum=sum+i*i;
}
printf(“\n Sum=%d”,sum);
}
continue vs goto: The following example clears the flow of control when using both of these:
#include<stdio.h> #include<stdio.h>
main() main()
{ {
int i=0; int i=0;
while(i<10) while(i<10)
{ {
i++; i++;
if(i==5) if(i==5)
break; continue;
printf(“%3d”,i); printf(“%3d”,i);
} }
} }
Output: 1 2 3 4 Output: 1 2 3 4 6 7 8 9 10
Ex: write a program to check whether given number is prime or not
/*program to check whether given number is prime or not*/
#include<stdio.h>
main()
{
int i,n;
printf(“\n Enter any number:”);
scanf(“%d”,&n);
for(i=2;i<=n/2;i++)
{
if(n%i!=0)
continue;
else
break;
}
if(n/2+1==i)
printf(“\n Given number is prime”);
else
printf(“\n Given number is not prime”);
}
Null statement: A "null statement" is a statement containing only a semicolon; it can appear wherever a
statement is expected. Nothing happens when a null statement is executed. The correct way to code a null
statement is:
;
Labeled statement: A statement that is preceded by a label along with a colon is known as a labeled
statement. The label must be a valid identifier. The correct way to use it is:
<any_label>: <statement>;
3) goto statement: The goto statement transfers the control to the specified location
unconditionally. There are certain situations where goto statement makes the program simpler. For
example, if a deeply nested loop is to be exited earlier, goto may be used for breaking more than
one loop at a time. In this case, a break statement will not serve the purpose because it only exits
a single loop.
The identifier following goto is a statement label and need not be declared. The name of the statement or
label can also be used as a variable name in the same program if it is declared appropriately. The compiler
identifies the name as a label if it appears in a goto statement and as a variable if it appears in an
expression.
If the block of statements that has label appears before the goto statement, then the control has to move
to backward and that goto is called as backward goto. If the block of statements that has label appears
after the goto statement, then the control has to move to forward and that goto is called as forward
goto.
Ex:
/*program to read a string using goto*/ /*program to generate 4 perfect numbers*/
#include<stdio.h> #include<stdio.h>
main() main()
{ {
char ch; int n,i,sum=0,count=0;
printf(“\n Enter any string:\n”); for(n=4;;n++)
read: ch=getchar(); {
if(ch!=’\n’) sum=0;
{ for(i=1;i<=n/2;i++)
putchar(ch); {
goto read; if((n%i)==0)
} Output: Enter any string: sum=sum+i; Output:
} Pradeep Kumar } 6
Pradeep Kumar If(sum==n) 28
{ 496
printf(“%d\n”,sum);
8128
count++;
if(count==4)
goto end;
}
}
end: ; /*null statement*/
}
Additional topics:
Nested loops: whenever a looping control statement appears within another looping control statement, it
is said to be nested loop structure. The loop should not overlap each other. That is, the names used in
expressions of one loop statement must be different from other.
Ex: for(i=1;i<=5;i++) //outer loop
{ Output: 1
printf(“%3d”,j); 123
printf(“\n”); 1234
} 12345
Infinite loop:
An infinite loop is a sequence of instructions in a computer program which loops endlessly, either due to
the loop having no terminating condition or having one that can never be met.
Ex: int a;
for (a = 0; a < 5; a++)
{
//code
a = 2;
printf(“%d”,a);
}