Reading:  

Getting started with C Language


Decision making and branching in C

In C programming it support sequential program statements which execute one statement immediately after another. Here the flow is sequential it never change the flow of control from the next line. The compiler executes the program sequentially in the order which they appear. This happens when there are no options and if the repeated steps are not needed. But there are another option when we need the options and when we want to use the selective steps repeatedly. In many situations we may have to change the order of execution and want to repeat certain steps based on any condition or until the condition meets, which means decision making. C supports much decision making or control statements.

The main important decision making statements in c are as follows.

  • If statement
  • Switch statement

If statement

‘If’ is the most powerful decision making statement in C language. Thus it is used to control the execution of statements. ‘If’ is used along with an expression. It is a two way branching or decision making statement.

Syntax :

if(test expression)
{
Body of if;
}

Using an ‘if’ a computer first evaluate the expression and if it is true the body of if is executed which means the flow of control passes to the if’s body, or if the test condition is  false then the flow of control passes to the immediate step next to if’s body. If there are more than one step in the body of ‘if’ they are normally written with in brackets ‘{ }’ to denote it is the body of ‘if’.

The test expression will return a Boolean value which is a’0’ or ‘1’. Here 1 is ‘true’ and 0 denotes it is ‘false’.

Two way branching

The figure explain the two way branching of ‘if’ statement.

decision making in C

Example:

if(age>55){
printf(“ Retired”);
}             

Depending upon the complexity and conditions to be tested if can be further subdivided.

  • Simple if
  • if…..else
  • nested if…else
  • if…else…if ladder

simple if statement

The general syntax of a simple if statement is

if(test expression)
{                      
Body of if;
}

Statement X;

The body of ‘if’ may be a single statement or group of statements. When the test expression returns a true value these body of if is executed. If the condition is false the flow of control passes to the statement X. The statement X will be executed in both cases.

 

If else if statement

an extension to simple if is called if.. else statement.

Syntax:

if(test expression)
{                      
Body of if;
}
else
{
Body of else;
}

Statement x;

As a simple if statement first tests the condition. If it is true then executes the body of if and then skips the next part and then passes the control in to the statement x. Here we added another block which is the set of statements to do when the condition is false and it is named as else. The flow passes to else part after skipping the body of if whenever the condition fails. The condition may return ‘true’ or ‘false’ in both cases the statement x is executed.

Example :

if( code==1)
 boy=boy+1;
else
 girl=girl+1;

 

Nested if statement

It is used when a series of decisions are involved  and have to use more than one if…else statement.

Syntax :

   if(condition 1)
{
            if(condition 2)
            {
               body of if;
            }
         else
          {
           Body of else;
          }
   }
else
   {
        Body of else;
   }

Statement x;

Example :


if( a>b)
 {
     if(a>c)
       printf(“ a is greater”);
    else
       printf(“ c is greater”);
  }
else
   {
    if(b>c)
      {
          Printf(“b is greater”);
       }
   else
     {
      Printf(“c is greater”);
    }
}

Here it explains the greatest among three numbers.

The if… else… if ladder

To take multipath decisions or chain of ifs then we use the if …else…if ladder. It is in the following general form

if(condition 1)  
statement 1;
   else if(condition 2)
      statement 2;
       else if(condition 3)
          statement 3;
          else
          statement 4;

statement x;

The conditions are evaluated from the top of the ladder down words as soon as a true condition is found. The compiler executes the ladder till a true condition is found, and when found a true condition execute the statements associated with it. After executing the statements the control passes to the statement x.

Example :

if(percentage >==80) 
 printf(“ Distinction”);
        else if(percentage >==60)
             printf(“  first class ”);
                 else if(percentage >==50)
                      printf(“ second class ”);
                else
           printf(“ failed “);

The switch statement

If is a conditional statement where ‘switch’ is  a selection statement which means if we want to  select one from many alternatives we use ‘switch’. The selective statement ‘switch’ is a multi way decision statement, that is using a single expression we can direct the flow of control to multiple paths. ‘Switch ‘ statement tests the value of given expression or variable against a list of case values and when a match is found that block of statements associated  with that case is executed.

Syntax :

switch(expression)
 {
   case constant1 :
                               block1;
                               break;
  case constant2 :
                               block2;
                               break;
  case constant n:
                               block n;
                               break;
                           .
                                   .
                                    .
default:
              default block;
                 break;
}

statement x;

When the ‘switch’ is executed the value of the expression is successfully compared against the case’s constants. When a case constant is found the block of codes associated with it is executed, and  if till the last case constant no match is found the block associated with the default case is executed. The default is an optional case . If it is not present and no matches are found then control passes to the statement x.

Example :

int num;
printf(“enter a number”);
scanf(‘%d”,&num);
switch(num)
{
case 1:
             printf(“Sunday”);
             break;     
case 2:     
             printf(“Monday”);
             break;     
case 3:     
             printf(“Tuesday”);
             break;
case 4:     
             printf(“Wednesday”);
             break;
case 5:     
             printf(“Thursday”);
             break;
case 6:     
             printf(“Friday”);
             break;
case 7:     
             printf(“Saturday”);
             break;
default:    
             printf(“wrong choice”);
             break;
}

Here according to numbers from 1 to 7 the corresponding day is displayed. If we input any number other than 1 to 7 then the default case is executed.

 

Description

This tutorial introduces you to C language. Many tutorials has been written. We are writing this tutorial to be brief and to the point, so that you concentrate on what matter, rather than finding yourself in a mess. We want you to get a start and build form knowledge you will gain from this tutorial



Environment

We will cover environment needs as we progress through this tutorial. At the very least you will need a PC with windows or Linus operating system.

Audience

Programmers new to C Language

Learning Objectives

Learn basics of C language and build from that knowledge.

Author: Subject Coach
Added on: 3rd Jan 2015

You must be logged in as Student to ask a Question.

None just yet!