UnConditional And Loop Control Statements of C Language Part-2

Engg.... | 04:14 | | | | Best Blogger Tips
do-while loop

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:
  1. goto statement is used whenever necessary else should be avoid.
  2. goto statement create an infinite loop.
  3. Any number of goto statements used in C program.
  4. 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.
  1. While Loop
  2. Do-while Loop
  3. 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:



 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.

Example:
int a=0;
while(a<5)
{
printf(" the value of a is=%d/n" , a);
a++;
}

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



Please Share it! :)
Do you like this Story?

Get Free Email Updates Daily!

Follow us!

2 comments:

  1. I believe that unarmed truth and unconditional love will have the final word in reality. This is why right, temporarily defeated, is stronger than evil triumphant. See the link below for more info.

    #unconditional
    www.mocsbar.com

    ReplyDelete
  2. I used a lot of emoticons in my sms and in my networking site. I find it cute and interesting. It portraits on my emotions.

    Bob
    www.gofastek.com

    ReplyDelete