} } }
Output:
Output:
Please enter
Please enter a no a no between 1 and between 1 and 5 5 33 You choice three
You choice three
1.13 LOOPING STATEMENTS 1.13 LOOPING STATEMENTS
Sometimes it is necessary for the program to
Sometimes it is necessary for the program toexecute the statement severalexecute the statement several times
times, and C loops execute a block of commands a specified number of times until a condition is, and C loops execute a block of commands a specified number of times until a condition is met.
met.
What is Loop?
What is Loop?
A computer is the most suitable machine to perform repetitive tasks and
A computer is the most suitable machine to perform repetitive tasks and can tirelessly docan tirelessly do a task tens of thousands of times. Ever
a task tens of thousands of times. Every programming language has the feature to y programming language has the feature to instruct to doinstruct to do such repetitive tasks with the help of certain form
such repetitive tasks with the help of certain form of statements. Theof statements. Theprocess of repeatedlyprocess of repeatedly executing a collection of statement is called looping
executing a collection of statement is called looping . The statements get executed many. The statements get executed many numbers of times based on the
numbers of times based on the condition. But if the condition is given in condition. But if the condition is given in such a logic that thesuch a logic that the repetition continues any number of times with no fixed condition to stop looping those repetition continues any number of times with no fixed condition to stop looping those statements, then this type of looping is called
statements, then this type of looping is called infinite looping.infinite looping.
C supports following types of loops:
C supports following types of loops:
while loopswhile loops
do while loopsdo while loops
for loopsfor loops while loops while loops
C while loops statement
C while loops statementallows to repeatedly run the same block of codeallows to repeatedly run the same block of code until a until a condition is met
condition is met. while loop is a most basic loop in C programming. while loop . while loop is a most basic loop in C programming. while loop has one controlhas one control condition, and executes as long the condition
condition, and executes as long the condition is true. is true. The condition of the loop is tThe condition of the loop is tested beforeested before the body of the loop is executed, hence it is called an
the body of the loop is executed, hence it is called anentry-controlled loopentry-controlled loop..
Syntax:
Syntax:
Example:
Example:
#include<stdio.h>
#include<stdio.h>
int main () int main () {
{
/* local variable Initializati /* local variable Initialization */on */
int n = 1,times=5;
int n = 1,times=5;
/* while loops execution */
/* while loops execution */
while( n <= times ) while( n <= times ) {
{
printf("C while loops: %d\n", n);
printf("C while loops: %d\n", n);
n++;
n++;
} }
return 0;
return 0;
} }
Output:
Output:
C while loops:1 C while loops:1 C while loops:2 C while loops:2 C while loops:3 C while loops:3 C while loops:4 C while loops:4 C while loops:5 C while loops:5
while (condition) while (condition) {
{
statement(s);
statement(s);
Increment statement;
Increment statement;
} }
Do..while loops:
Do..while loops:
C do while loops are very similar to the while
C do while loops are very similar to the while loops, but it always executes theloops, but it always executes thecodecode block
block at least onceat least once and furthermore as long as the and furthermore as long as the condition remains true. This is ancondition remains true. This is anexit- exit-controlled loop
controlled loop..
Syntax:
Syntax:
Example:
Example:
#include<stdio.h>
#include<stdio.h>
int main () int main () {
{
/* local variable Initialization */
/* local variable Initialization */
int n = 1,times=5;
int n = 1,times=5;
/* do loops execution */
/* do loops execution */
do do { {
printf("C do while loops: %d\n", n);
printf("C do while loops: %d\n", n);
n = n + 1;
n = n + 1;
}while( n <= times );
}while( n <= times );
return 0;
return 0;
} }
Output:
Output:
C do while loops:1 C do while loops:1 C do while loops:2 C do while loops:2 C do while loops:3 C do while loops:3
do do { {
statement(s);
statement(s);
}while( condition );
}while( condition );
C do while loops:4 C do while loops:4 C do while loops:5 C do while loops:5 for loops
for loops
C for loops is very
C for loops is very similar to a while loopssimilar to a while loops in that it continues to process a block in that it continues to process a block of codeof code until a statement becomes false, and ever
until a statement becomes false, and everything isything isdefined in a single linedefined in a single line. The for loop is. The for loop is also
alsoentry-controlledentry-controlled loop. loop.
Syntax:
Syntax:
Example:
Example:
#include<stdio.h>
#include<stdio.h>
int main () int main () {
{
/* local variable Initialization */
/* local variable Initialization */
int n,times=5;;
int n,times=5;;
/* for loops execution */
/* for loops execution */
for( n = 1; n <= times; n = n for( n = 1; n <= times; n = n + 1 )+ 1 ) {
{
printf("C for loops: %d\n", n);
printf("C for loops: %d\n", n);
} }
return 0;
return 0;
} }
for ( init; condition; increment ) for ( init; condition; increment ) {
{
statement(s);
statement(s);
} }
Output:
C Loop Control Statements C Loop Control Statements
Loop control statements are used to change the normal sequence of execution of the loop.
Loop control statements are used to change the normal sequence of execution of the loop.
Statement
Statement Syntax Syntax DescriptionDescription break
break statement statement
break;
break; It is used toIt is used toterminate loopterminate loop or or switch statementsswitch statements..
continue continue statement statement
continue;
continue; It is used toIt is used tosuspend the execution of current loopsuspend the execution of current loop iteration
iteration and andtransfer controltransfer control to the loop for the next to the loop for the next iteration.
Ittransfers current programtransfers current program execution sequence to some execution sequence to some other part of the program.
other part of the program.