|
INTRODUCTION : -
There may be a situation when WE need to execute a block of code several number of
times. In general statements are executed sequentially: The first statement in a function is
executed first, followed by the second, and so on.
Programming languages provide various control structures that allow for more complicated
execution paths.
A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages:
C programming language provides following types of loop to handle looping requirements. Click the following links to check their detail.
------------------------------------------------------------------------------------------------------------------------------------------------
|
Control Statement | Description |
---|---|
break statement | Terminates the loop or switch statement and transfers execution to the statement immediately following the loop or switch. |
continue statement | Causes the loop to skip the remainder of its body and immediately retest its condition prior to reiterating. |
goto statement | Transfers control to the labeled statement. Though it is not advised to use goto statement in our program. |
THE BREAK STATEMENT :
The break statement in C programming language has following two usage:
- When the break statement is encountered inside a loop, the loop is immediately terminated and program control resumes at the next statement following the loop.
- It can be used to terminate a case in the switch statement (covered in the next chapter).
If you are using nested loops ( ie. one loop inside another loop), the break statement will stop the execution of the innermost loop and start executing the next line of code after the block.
Syntax:
The syntax for a break statement in C is as follows:
break;
Flow Diagram:
Example:
#include <stdio.h> int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; if( a > 15) { /* terminate the loop using break statement */ break; } } return 0; }
When the above code is compiled and executed, it produces following result:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15
THE CONTINUE STATEMENT :
The continue statement in C programming language works somewhat like the break statement. Instead of forcing termination, however, continue forces the next iteration of the loop to take place, skipping any code in between.
For the for loop, continue statement causes the conditional test and increment portions of the loop to execute. For the while and do...while loops, continue statement causes the program control passes to the conditional tests.
Syntax:
The syntax for a continue statement in C is as follows:
continue;
Flow Diagram:
Example:
#include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ do { if( a == 15) { /* skip the iteration */ a = a + 1; continue; } printf("value of a: %d\n", a); a++; }while( a < 20 ); return 0; }
When the above code is compiled and executed, it produces following result:
value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 16 value of a: 17 value of a: 18 value of a: 19
The Infinite Loop:
A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the for loop are required, WE can make an endless loop by leaving the conditional expression empty.
#include <stdio.h> int main () { for( ; ; ) { printf("This loop will run forever.\n"); } return 0; }
When the conditional expression is absent, it is assumed to be true. WE may have an initialization and increment expression, but C programmers more commonly use the for( ; ; ) construct to signify an infinite loop.
NOTE: WE can terminate an infinite loop by pressing Ctrl + C OR (ctrl+pause break ) keys.
No comments:
Post a Comment