0% found this document useful (0 votes)
9 views10 pages

Looping Control Statements

Uploaded by

manasabezawada04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views10 pages

Looping Control Statements

Uploaded by

manasabezawada04
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 10

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.

Looping Control Statements

while statement do-while statement for statement

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

false conditio true


n

Next_statement while loop body

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;
while(i<=n)
{
sum=sum+i;
i=i+1;
}
printf(“\n sum of %d numbers=%d”,n,sum);
}
2) do- while statement: It is one of the looping control statements. It is also called as exit-
controlled looping control statement. i.e., it tests the condition after executing the do-while loop
body.
The main difference between “while” and “do-while” is that in “do-while” statement, the loop body
gets executed at least once, though the condition returns the value false for the first time, which is
not possible with while statement. In “while” statement, the control enters into the loop body when
only the condition returns true.
The syntax for “do-while” statement is as follows: Initialization statement;
do
{
statement(s);
} while(condition);
In this syntax, next_statement;
While and do are the keywords. 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 do-while loop body. The updating statement should be included with in the do-
while loop body. There should be a semi-colon (;) at the end of while(condition).
Whenever “do-while” statement is encountered, the initialization statement gets executed first.
After then, the control enters into do-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 false, the control transfers to next_statement with out
executing do-while loop body. Hence, it states that, the do-while loop body gets executed for the
first time, though the condition returns the value false.
If the condition returns the value true, the control enters into do-while 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 counter value. If the condition returns the value true, the above said
procedure will be repeated.

Initialization statement

do-while loop body

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

for loop body

exp3
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;
for(i=0;i<=n;i++)
sum=sum+i;
printf(“\n sum of %d numbers=%d”,n,sum);
}
Jumping control statements

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:

Jumping Control Statements

break continue goto

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>;

Ex: even: printf(“\n Given number is even”);


Note: As with any other C statement, we can include a label before a null statement. To label an item that
is not a statement, such as the closing brace of a compound statement, we can label a null statement and
insert it immediately before the item to get the same effect.

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 syntax for goto statement is:


label:
{
statement_1;
statement_2;
:
}
:
goto label;
In this syntax, goto is the keyword and label is any valid identifier and should be ended with a colon (:).

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

for(j=1;j<=i;j++) // inner loop 12

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);
}

Conditional Branching vs Unconditional Branching:


When a program breaks the sequential flow and jumps to the new section of code, it is called branching.
When this branching is based on a condition, the program is performing conditional branching. When no
condition is involved and the program always branches when it encounters a particular branching
instruction, the program is performing unconditional branching.
Unconditional Branching occurs when a program branches to a new location in the code with out
analyzing the data and making a decision using condition. Conditional branching occurs when the program
branches based on a decision of some type.
Ex: Conditional branching statement: if… else
Unconditional branching statement: goto

Entry-controlled looping construct vs Exit-controlled looping construct:


The "for" and "while" loops test the condition before executing the loop body. Hence these are called as
entry-controlled looping constructs. This implies that “for” and “while” loops might not execute the loop
body even once.
The “do-while” loop tests the condition after executing the loop body. Hence, it is called as an exit-
controlled looping construct. This implies that “do-while” loop execute the loop body at least once even
when the condition is false.

Counter-controlled loop vs Sentinel-controlled loop:


When we know in advance exactly how many times the loop will be executed, we use a counter-controlled
loop( definite repetition loop). When we don’t the number of repetitions exactly, we use a sentinel-

controlled loop (indefinite repetition loop).


Counter-controlled loop Sentinel-controlled loop
i=1; int i,count=0;
while(i<=10) printf(“\n Enter numbers (end with -999):”);
{ do
printf(“%d”,i); {
i=i+1; scanf(“%d”,&i);
} count++;
}while(i!=-999);
Printf(“\n count of entered numbers=%d”,count);

You might also like