• Sometimes it is required to quit the loop as soon as certain condition occurs.
• For example, consider searching a particular number in a set of 100 numbers, as soon as the search number is found it is desirable to terminate the loop.
• A break statement is used to jump out of a loop.
• A break statement provides an early exit from for, while, do…while and switch constructs.
• A break causes the innermost enclosing loop or switch to be exited immediately.
Example : Read and sum numbers till -1 is entered
void main()
{
int i, num=0;
float sum=0,average;
printf(“Input the marks, -1 to end\n”);
while(1)
{
scanf(“%d”,&i);
if(i==-1)
break;
sum+=i;
}
printf(“%d”, sum);
}
continue;
• The continue statement can be used to skip the rest of the body of an iterative loop.
• The continue statement tells the compiler, “SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION”.
Example: Find sum of 5 positive integers. If a negative number is entered then skip it.
void main()
{
int i=1, num, sum=0;
for (i = 0; i < 5; i++)
{
scanf(“%d”, &num);
if(num < 0)
continue; // starts with the beginning of the loop
sum+=num;
}
printf(“The sum of positive numbers entered = %d”,sum);
}
goto:
• The goto statement is a jump statement which jumps from one point to another point within a function.
• The goto statement is marked by label statement. Label statement can be used anywhere in the function above or below the goto statement.
• Generally goto should be avoided because its usage results in less efficient code, complicated logic and difficult to debug.
Example: Following program prints 10,9,8,7,6,5,4,3,2,1
void main ()
{
int n=10;
loop:
printf(“%d,”,n);
n–;
if (n>0)
goto loop;
}
Entry control loop | Exit control loop |
Entry control loop checks condition first and then body of the loop will be executed. | Exit control loop first executes body of the loop and checks condition at last. |
Body of loop may or may not be executed at all. | Body of loop will be executed at least once because condition is checked at last. |
for, while are example of entry control loop. | Do…while is example of exit control loop. |
• Loops are used to repeat execution of a block of code.
• During looping, a set of statements are executed until some condition for termination is encountered.
Generally, looping process would include the following four steps
1) Initialization of a counter
2) Test for a termination condition
3) Loop body statements
4) Increment the counter
C supports three types of looping
while loop
• The simplest of all looping structure is while statement.
• The general format of the while statement is:
Initialization;
while (test condition)
{
body of the loop ;
increment or decrement;
}
• Test condition is evaluated and if the condition is true then the body of the loop is executed.
• After the execution of the body, the test condition is once again evaluated and if it is true, the body is executed once again.
• This process is repeated till the test condition is true. When it becomes false, the control is transferred out of the loop.
• On exit, the program continues with the statements immediately after the body of the loop.
• While loop is also known as entry control loop because first control-statement is executed and if it is true then only body of the loop will be executed.
Example: To print first 10 positive integer numbers void main()
{
int i;
i = 1; \\ initialization of i
while(i <= 10) \\ condition checking
{
printf(“\t%d”,i); \\ statement execution
i++; \\ increment of control variable
}
}
do…while loop
• In contrast to while loop, the body of the do…while loop is executed first and then the loop condition is checked.
• The body of the loop is executed at least once because do…while loop tests condition at the bottom of the loop after executing the body.
• do…while loop is also known as exit control loop because first body statements are executed and then control-statement is executed, thus condition checking happens at exit point.
• The general format of the do…while statement is:
initialization;
do
{
statement;
increment or decrement;
}
while(test-condition);
Example: To print first 10 positive integer numbers
void main()
{
int i;
i = 1; \\ initialization of i
do
{
printf(“\t%d”,i); \\ statement execution
i++; \\ increment of control variable
} while(i <= 10); \\ condition checking
}
for Loop
• for loop provides a more concise loop control structure.
• The general form of the for loop is:
for (initialization; test condition; increment)
{
body of the loop;
}
• When the control enters for loop, the variables used in for loop is initialized with the starting value such as i=0, count=0. Initialization part will be executed exactly one time.
• Then it is checked with the given test condition. If the given condition is satisfied, the control enters into the body of the loop. If condition is not satisfied then it will exit the loop.
• After the completion of the execution of the loop, the control is transferred back to the increment part of
the loop. The control variable is incremented using an assignment statement such as i++
• If new value of the control variable satisfies the loop condition then the body of the loop is again executed. This process goes on till the control variable fails to satisfy the condition.
• For loop is also entry control loop because first control-statement is executed and if it is true then only body of the loop will be executed.
Example: // The following is an example that finds the sum of the first ten positive
integer numbers
void main()
{
int i; //declare variable
int sum=0;
for(i=1; i < = 10; i++) // for loop
{
sum = sum + i ; // add the value of i and store it to sum
}
printf(“%d”, sum);
}
• We can include multiple expressions in any of the fields of for loop provided that we separate such expressions by commas. For example: for( i = 0; j = 100; i < 10 && j>50; i++, j=j-10)
for Loop
for( i=1; i < = 10; i++)
{
sum = sum + i ;
}
while Loop
i=1;
while(i<=10)
{
sum = sum + i;
i ++;
}
do…while Loop
i=1;
do
{
sum = sum + i;
i ++;
} while(i<=10);