Flow Control Statement in C

C programming language supports a set of statements to control the flow of code in a program. Normally, in a program statements are executed sequentially in the order in which they appear.

But in practice, we encounter several situations where we want to change the order of execution of statements based on certain conditions or repeat a statement until certain conditions are met.

C language supports such decision-making capabilities to perform tasks based on the decision-making of the user, these statements include:

  • if statement
  • for statement
  • while statement
  • break and continue statement
  • switch statement
  • goto statement

if statement

This statement allows the computer to evaluate between two expressions, depending on whether the value of one of the expressions (relational or conditional) is ‘true’ (or non-zero) or ‘false’ (zero), it transfers the control to a particular statement.

It takes the following form:

if (test expression)

Depending upon the complexity of conditions to be evaluated if statements can be implemented into the following forms:

  • Simple if statement
  • if….else statement
  • Nested if….else statement
  • else if ladder

Simple if statement

syntax:

if(test expression)

{

   statement-block;

}

statement-x;

Working of an if statement:

  • The ‘statement-block’ contains a single or group of statements to be executed if the test expression is true.
  • If the statement becomes false, the statement-block will be skipped.
  • Either way, the execution will jump to the statement-x.

Example: Program to print a number using simple if statement.

#include <stdio.h>
int main()
{
    int a;
    printf("Enter a number: ");
    scanf("%d", &a);
    if (a<0)
{
    printf("The number you entered is %d.\n",a);
}
printf("Program finished.");
return 0;
}

Output:

Enter a number: -5
The number you entered is -5
Program finished.

if….else statement

It is an extension of the simple if statement.

syntax:

if(test expression)

{

True block-statement(s)

}

else 

{

False block-statement(s)

}

statement-x;

Working of an if….else statement:

  • If the test expression is true, then the true block-statement(s) is executed.
  • If the test expression is false, then the false block-statement(s) is executed.
  • In either case, statement-x is executed next.

Nested if….else statements

This statement involves a series of decisions, here we use more than one if….else statement in nested form.

syntax:

if(condition-1)

{

   if(condition-2)

   {

      statement-1;

   }

   else

   {

      statement-2;

   }

}

else

{

   statement-3;

}

statement-x;

Working of a nested if….else statement:

  • If the condition-1 is false, statement-3 will be executed.
  • If the condition-1 is true, it performs the second test.
  • If the condition-2 is true, statement-1 will be executed.
  • If condition-2 is false, statement-2 will be executed.
  • In any of the cases, the statement-x will be executed next.

else if ladder

This statement involves a multipath decision chain of ‘ifs’ in which the statement associated with each ‘else’ is an ‘if’.

syntax:

if(condition-1)

   statement-1;

      else if(condition-2)

         statement-2;

        else if(condition-3)

               statement-3;

                  else if(condition-n)

                     statement-n;

                   else

                     default-statement;

                  statement-x;

Working of an else if ladder:

  • The conditions are evaluated from top to downwards of the ladder.
  • When the true condition is found, the statement associated with it is executed and then statement-x is executed next.
  • When all the n conditions become false, then default-statement of the final else is executed and then statement-x is executed next.

for statement

The for loop is an entry-controlled loop the provides a concise loop control structure.

Simple ‘for’ loops

syntax:

for(initialization;condition;increment/decrement)

{

body

}

Working of a for loop:

  • The initialization of the loop control variables is done first. E.g. i=1,count=0,etc.
  • The value in the loop control variable is tested using the condition.
  • If the condition is true, the body of the loop is executed. Otherwise, the loop is terminated and the next statement after the loop is executed.
  • When the body of the loop is executed, the control goes back to the for statement.
  • Then the control variable is incremented/decremented(++/–) and the new value of the control variable is again tested using the condition.
  • If the condition is true, the body of the loop is again executed.
  • This process continues until the condition becomes false and then the loop gets terminated.

Example: Program to print a series of number using for statement.

#include <stdio.h>
int main()
{
    int i;
    for(i=1;i<10;i++)
    {
        printf("%d ",i);
    }
    return 0;
}

Output:

1 2 3 4 5 6 7 8 9

Nesting of for loops

syntax:

for(i=1;i<3;i++) [Outer loop]

{

statement(s);

for(j=1;i<2;j++)[Inner loop]

     {

      statement(s);

      }

      statement(s);

}

while statement

The while statement is an entry-controlled loop statement. It is the simplest of all the looping structures in the C programming language.

syntax:

while(condition)

{

body of the loop

}

Working of while statement:

  • The condition is evaluated at first.
  • If the condition is true, then the body of the loop is executed.
  • Again the condition is evaluated and again the body of the loop is executed.
  • This process continues until the condition becomes false.
  • Then the control is transferred to the statement after the loop.

Example: Program to print a series of number using while statement.

#include <stdio.h>
int main() 
{  
    int i=1;  
    while(i<=5) 
    {  
        printf("%d ",i);  
        i++; 
    }
    return 0;
}

Output:

1 2 3 4 5

 

do….while statement

The do….while statement is used when we want to execute the body of the loop before testing the condition.

It is an exit-controlled loop and therefore the body of the loop is executed at least once.

syntax:

do

{

body of the loop

}

while(condition);

Working of a do….while statement:

  • When entering the do statement, the body of the loop is evaluated.
  • After that, the condition in the while statement is evaluated.
  • If the condition is true, once again the body of the loop is evaluated.
  • This process continues until the condition becomes false.
  • Then the loop is terminated and the control goes to the statement next to the while statement.

break and continue statement

In C programming language we can jump from one statement to another statement inside a loop as well as jump out of the loop using break and continue statements respectively.

break statement

  • Inside a loop, we can use a break statement to immediately exit the loop and the control goes to the statement next to the following loop.
  • In a nested loop, the break statement would only exit from the loop containing it.

Use of a break statement:

for(…..)

{

   statement(s);

   if(condition)

   break;

   statement(s);

}

Example: Program to check whether a number is prime or composite using break statement.

#include <stdio.h>
int main()
{
    int a, prime,i;
    printf("Enter a number: ");
    scanf("%d", &a);
    prime=1;
    for(i=2; i<a; i++)
    {
        if(a%i == 0)
        {
            prime = 0;
            break;
        }
    }
    if(prime == 1)
    {
        printf("Prime number");
    }
    else
    {
        printf("Composite number");
    }
    return 0;
}

Output:

Enter a number: 5
Prime number

continue statement

The continue statement instructs the program to skip any statements in between the iteration and continues with the next iteration.

Use of a continue statement:

for(…..)

{

statement(s);

   if(…..)

    continue;

    statement(s); [skipped]

}

Example: Program to find out even numbers using continue statement.

#include <stdio.h>
int main()
{
    int a;
    printf("Even numbers between 1 to 10: \n");
    for(a=1; a<=10; a++)
    {
        if(a%2==1)
            continue;
        printf("%d ",a);
    }
    return 0;
}

Output:

Even numbers between 1 to 10:
2 4 6 8 10

switch statement

  • The switch statement evaluates the value of a variable with a list of case values. When a match is found, the block of statements corresponding to the case is executed.
  • The switch statement is used in very complex programs having a large number of alternatives where using if statement could cause confusion even to the programmer.

Rules for using a switch statement:

  • The expression must be an integer or character.
  • Value-1,value-2,etc are constants known as case labels.
  • Each of the values must be unique in a switch statement.
  • Case labels must end with a colon.
  • There can be at most one default.
  • The switch statements can be nested.

Use of a switch statement:

switch(expression)

{

   case value-1;

      block-1

      break;

   case value-2;

      block-2

      break;

——–

——–

    default;

      default-block

      break;

}

statement-x;

Working of a switch statement:

  • When the switch statement is executed first, the value of the expression is compared with the case labels.
  • After the values are matched, then the block statement(s) associated with the case value is executed.
  • The break statement at the end of each block causes exit from the switch statement, and the control goes to statement-x following the switch.
  • Default is executed only if the value of the expression does not match with any of the case values.

Example: Program to check days of week using switch statement.

#include <stdio.h>
int main()
{
    int x;
    printf("Enter a week day number: ");
    scanf("%d", &x);

    switch(x)
    {
        case 1: 
            printf("Monday.\n");
            break;
        case 2: 
            printf("Tuesday.");
            break;
        case 3: 
            printf("Wednesday.");
            break;
        case 4: 
            printf("Thursday.\n");
            break;
        case 5: 
            printf("Friday.");
            break;
        case 6: 
            printf("Saturday.\n");
            break;
        case 7: 
            printf("Sunday.\n");
            break;
        default: 
            printf("Wrong input");
    }
    return 0;
}

Output:

Enter a week day number: 3
Wednesday.

goto statement

  • The goto statement is used to jump unconditionally from one point to another point in a program.
  • It is also used to transfer the control out of a loop(or nested loop) when a complex condition is encountered.
  • It enhances the readability and execution speed of the program.

Use of a goto statement:

[Forward jump]

goto label;

————

————

————

label;

statement;

[Backward jump]

label;

statement;

————–

————–

————–

goto label;

Working of a goto statement:

  • In forward jump, if the label is placed after the goto label; the program will skip the statement.
  • In the backward jump, if the label placed before the goto label; a loop will occur and some statements will be executed repeatedly.

Example: Program to sum using goto statement.

#include <stdio.h>
int main()
{
  int a=0;
  for(int i = 0; i<=5; i++)
  {
a = a+i;
if(i==2)
{
  goto sum;
}
  }
  sum:
  printf("%d", a);
  return 0;
}

Output:

3

Few more Flow Control Statement Examples

Example 1: Program to check whether a number is less than 8 or not using if statement.

 

#include <stdio.h>
int main ()
{
  int i = 4;
  if( i < 8 )
  {
      printf("%d is less than 8\n",i );
  }
  printf("Value of i is : %d\n", i);
  return 0;
}

Output:

4 is less than 8
Value of i is : 4

Example 2: Program to print star (*) using nested for loop.

#include <stdio.h>
int main () 
{
  int i, j;
  for(i = 0; i<3; i++) 
  {
      for(j = 0; j <= 2; j++)
{
printf("*\n");
}
  }
  return 0;
}

Output:

*
*
*
*
*
*
*
*
*

Example 3: Program to print star (*) using do while loop.

#include <stdio.h>
{
int main ()
{
  int i = 5;
  do {
      printf("*");
      i = i + 1;
  }
while( i < 10 ); 
  return 0;
}

Output:

*****

Example 4: Program to check whether a number is positive or negative using else if ladder.

#include<stdio.h>
int main()
{
    int i;
    printf("Enter a number:");
    scanf("%d",&i);
    if(i<0)
        printf("Negative number");
    else if(i>0)
        printf("Positive number");
    else
        printf("Number is equivalent to zero");
    return 0;
}

Output:

Enter a number: +2
Positive number