UNCONDITIONAL CONTROL STATEMENT:
- C supports an unconditional control statement that is goto.
- goto is used to transfer control from one point to other in a C program.
- Goto is a branching statement.
- It requires a label.
- goto is a keyword.
The syntax of goto statement is:
goto END: ................ ................ ................ END: ................. |
Points to remember about goto statement:
- goto statement is used whenever necessary else should be avoid.
- goto statement create an infinite loop.
- Any number of goto statements used in C program.
- No two goto statements have the same label name.
NOTE: Lable can be placed either before or after the goto statement.
LOOP CONTROL STRUCTURE:
Three types of loop control structures are used in C language.
- While Loop
- Do-while Loop
- For Loop
While Loop:
This is used to execute a set of statements repeatedly as long as the condition is TRUE.The syntax of While Loop is:
This is used to execute a set of statements repeatedly as long as the condition is TRUE.The syntax of While Loop is:
while(condition) { statement; } |
Where
Statement => simple or compound statement
while => keyword
Condition => results in TRUE or FALSE
Explanation: If the logical condition is TRUE then the statement is repetedly executed. If the result is FALSE then control comes out of the loop and continues with the next executable statement.The value of variable involved in the logical condition will be changed during every pass of while loop. If the value does not changed then control does not come out of the loop and statements executed repeatedly that is infinite loop.
Statement => simple or compound statement
while => keyword
Condition => results in TRUE or FALSE
Explanation: If the logical condition is TRUE then the statement is repetedly executed. If the result is FALSE then control comes out of the loop and continues with the next executable statement.The value of variable involved in the logical condition will be changed during every pass of while loop. If the value does not changed then control does not come out of the loop and statements executed repeatedly that is infinite loop.
Example:
int a=0;
while(a<5)
{
printf(" the value of a is=%d/n" , a);
a++;
}
Do-while Loop:
Output:
Value of a=1
Value of a=2
Value of a=3
Value of a=4
Do-while Loop:
This is used to execute a set of statements repeatedly,untill the logical test results in FALSE.The syntax of Do-while Loop is:
do { statement; } while(condition); |
Explanation: If the logical condition is TRUE then the statement is repetedly executed. If the result is FALSE then control comes out of the do-while loop and continues with the next executable statement.
For Loop:
It is used when the programmer knows how many times a set of statements are to be executed. The syntax of for loop is:
for ( exp1; exp2; exp3) { statement; } |
Where
Exp1 => assign the value to variable.
Exp2 => give condition to variable.
Exp3 => increment or decrement in the value of variable.
Statement => compound or simple statement
for => keyword
Example:
for (i=8 ; i>4 ; i--)
{
printf("value of i=%d/n" , i);
}
Output:
Value of i=8
Value of i=7
Value of i=6
Value of i=5
0 Response to "UnConditional And Loop Control Statements of C Language Part-2"
Post a Comment