Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Decision Making and Looping using while statement , do while loop , for statement

Posted By: Verner Fischer     Category: C Programming     Views: 7373

This article explains about how decision making is done in looping in C programming.

While Statement

 while (condition) { body of loop }
    
     i=0; 
   
     while (i<10) 
     {        printf (“%f”, i*i);
               i++;         }

      ans = ‘y’;

 while (ans==‘y’)    {/*process*/ 
 printf (“Do you want to continue y/n”)
          scanf(“%c”, ans); }

Do While Loop

 do {body of the loop} while (condition);
 It’s exit controlled loop

     i=0;

     do 
         { printf (“%f”, i*i); i++; }
     while (i<10);
 
while (number > 0 && number < 12)
 The de Morgan's law

For Statement

 for(initialization; condition; increment)

    for(i=0;i<10;++i) {printf (“%d”,i);}
    for(i=10; i>0; --i) {printf (%d”, i);}
    for(i=0,j=0;i<10;i++)
    for(i=0,j =0;i<10;i++,j++)
    for(i=0,j=0; i<50 || j <10;j++)
    for (i=0;i<10000;i++)    ;

Execution of For Loop

  • Initialization of control variable is done first
  • The value of control variable is tested then
  • If the test condition value is true, the loop executes, otherwise the body gets skipped
  • After the first execution of the loop the control goes to the third part of for
    •  for (total=0;ans=“n”; total = total + marks) {scanf (“%f”, marks; /* ask for yes no*/}


Different Ways to Use For

 i = 5;
for ( ; i < 10 ; i++ )
 i = 5;
 for (; i<10; ) { … ; i++; …}
 for (; ;) { … } /* The infinite loop */
 for (row =0; row < rowmax; ++row)
  { for (col = 0; col < colmax; ++ col;)
      printf( “%d”, row * col;);  }
/* Above is known as nesting */


Jumps in Loops

  • break and goto for jumping out
  • goto for jumping anywhere from anywhere!
  • Skipping next bunch of statements and continue with next iteration by continue
  • continue does not breaks out of loop
  • In while and do loops, continue causes control to go to condition, in for it goes to increment section and then to condition

Continue Graphically


Avoiding goto

  • It generates less efficient code
  • Careful program design can usually avoid use of goto
  • Many of goto will make the program logic complicated and less readable
  • In case any goto is absolutely necessary, it should be properly documented


goto Which Causes Problems


Concise Test Expressions

if (expression = 0) can be rewritten as 
if (!expression)
if (expression != 0) can be rewritten as
if (expression)
  • The second form is more common in professional C programs
  • It does not use any relational operators
  
Share: 



Verner Fischer
Verner Fischer author of Decision Making and Looping using while statement , do while loop , for statement is from Frankfurt, Germany.
 
View All Articles

 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!