C Branching & Loops

Branching / Decision Making

C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.

if statement

/* local variable definition */
int a = 100;
int b = 200;

/* check the boolean condition */
if( a == 100 ) {  
  if( b < 200 ) {
     /* if condition is true then print the following */
     printf("Value of a is 100 and b is < 200\n" );
  }
  else if (b==200)
  {
     printf("b is 200\n");
  }
  else
  {
     printf("b is too big\n");
  }
}

switch statement

char grade = 'B';

switch(grade) {
case 'A' :
   printf("Excellent!\n" );
   break;
case 'B' :
case 'C' :
   printf("Well done\n" );
   break;
case 'D' :
   printf("You passed\n" );
   break;
case 'F' :
   printf("Better try again\n" );
   break;
default :
   printf("Invalid grade\n" );
}

The ? Operator

We have the conditional operator ? : which can be used to replace if…else statements. It has the following general form −
Exp1 ? Exp2 : Exp3; The value of a ? expression is determined like this −

  • Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the value of the entire ? expression.
  • If Exp1 is false, then Exp3 is evaluated and its value becomes the value of the expression

Examples

int x=1;
int y=2;
x > y ? printf("x is greater") : printf("y is greater");
int x=1;
int y=2;
printf( x > y ? "x is greater" : "y is greater");

Both output y is greater and are equivalent to

int x=1;
int y=2;
if (x > y) {
   printf("x is greater")
 }
 else {
   printf("y is greater");
 }

Loops

for Loop

   int a;
	
   /* for loop execution */
   for( a = 10; a < 20; a = a + 1 ){
      printf("value of a: %d\n", a);
   }

while Loop

   int a = 10;

   /* while loop execution */
   while( a < 20 ) {
      printf("value of a: %d\n", a);
      a++;
   }

do .. while Loop

   int a = 10;

   /* do loop execution */
   do {
      printf("value of a: %d\n", a);
      a = a + 1;
   }while( a < 20 );

Loop Control Statements

break; exits the loop immeadiately and proceeds to the next instruction

   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;
      }
   }

continue; cause the loop to skip the remainder of its body and immeadiately retest prior to looping

   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 );

would output

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
  • goto transfers to the labelled statement
   int a = 10;

   /* do loop execution */
   FRED:do {
   
      if( a == 15) {
         /* skip the iteration */
         a = a + 1;
         goto FRED;
      }
		
      printf("value of a: %d\n", a);
      a++;

   }while( a < 20 );

will output

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

Like global variables goto is considered bad programming practice that could be avoided with better coding. But, again, sometimes it is quicker and easier to use.