Decision making and branching system in C language

 introduction-: we have seen that C program is a set of statements which are normally executed sequentially in the order in which they appear.this happens when no option or no repetition for certain calculation are necessary . how were in practice we have a number of situation where we may have to change the order of execution of statement based on certain condition or repeat a group of statement and till certain specified condition.

C language possesses such  decision making capability by supporting the following statement:-

1.if statement

2.switch statement

3.conditional  operator statement

4.Go to statement

Decision making with if statement:- the if statement is a powerful decision making statement and is used to control the flow of execution of statement it is a basically to way decision statement and  is used conjunction with an expression . Like 

IF(Test expression )

it allow the computer to evaluated the expression first and then depending on whether the value of the expression is true or false  it transfer the control to a particular statement. This point has two path

1 is condition is true and second the condition is false some example has be given here

  1. If (bank balance ==0)

Borrow money

The if statement may be 4 types

  1. Simple if statement
  2. If……..else statement
  3. Nested if ….else statement
  4. else if Ladder


simple if statement-: The general form of simple if statement is

if(test expression)               …..// condition is true then


statement block;


if test expression the statement block. maybe a single statement or a group of statement if the test expression is true to the statement block will be executed otherwise the statement block will be skipped the execution will jump to the statement x .remember when the condition is true both the statement block and the statement x are executed  in sequence

2.The if….. else  statement -: the if else statement is an extension of simple if statement the general form is 

If(test expression ) …………//condition is true


true block statement ;


else                          ……//statement false


false statement block;


 If the ext expression is true then the first condition is block and if the first condition is not the true the else condition will be block .


3.Nesting If………. statements:- when is series  of decisions are involved we may have to use more than one if ……..else statement in nested .

If(test condition 1) …………//condition is true


if(test condition  2)


true block statement 1 ;


else                          ……//statement false


false statement 2 block;



else                          ……//statement false


false statement 3 block;


Statement x;



if the condition first is false the statement 3 will be executed otherwise it continue to perform, i  the 2nd test if the condition  is true the statement first will be evaluated otherwise the statement 2 will be evaluated and then the control transfer to the statement x.

4 The else if ….ladder :- there is another way of putting ifs  together when multiple decisions are involved a multiple decision is a chain of ifs in which the statement associate with each else  is an if. it takes the following general form 

if(condition 1)

statement 1;

else if (condition 2)

statement 2;

else if (condition 3)

statement 3

else if (condition n )
statement n;

 identation rules:- when using control structure a statement of open control many other statement that follow it. it is a good practice to use identation to show that the Indentation statement are dependent on the preceding controlling statement some guidelines that could be followed while using identation are listed below

 :—— indeed statement that are depended on the previous statement provide at least three speace identation .

:——– align vertically else clause with their mathing if cluse

:——— use braces on separate line to identify a block statement
:———-indentation  statement in the Block by at least 3 space to the right of the braces

:————align the opening and closing braces.

:———–use appropriate  comments to signify in the beginning and end of blocks.

:———–indentation  the nested statement as per the above rules.

:———–Code only one clause or statement on each time.


The switch statement:- we have seen that when one of the many alternative is to be selected we can use an if statement to control the selection .Some time  complexity of such a program increasing dramatically when the number of alternative increases. the program become difficult to read and follow at times it may confused even the person who designed it. fortunately C has a built inmultiway  decision statements known as a switch statement.



case value -1:




case value -2:





Default :

default block



statement x;

the expression is an integer expression or character. value 1 value 2……… are a constant or constant expression and are known as a case label each of these should be unique within a switch statement Block 1 Block 2 ….are statement list and many contain 0 or non 0 statement there is no need to put braces around these block. note that case label and with a colon(:).

when the switch is executed the value of expression is successfully compare against the values

The break  statement and the end of each block signal the end of a particular case causes an exit from the switch statement  transferring the control to the statement as following the switch.

the default is a optional case.when  present it will be executed if the value of the expression does not match with any of the case value if not present no action takes place it all matches failed and the control go to the statement X.


Rules for  switch statement-: The switch expression must be an integer type .

case labels  must be constant or constant expression.

case labels  must be unique no 2 Level can have the same value

case  labels  must end with semicolon

the breaks statement transfer the control out of the switch statement

the break statement is optional that is two or more case labels  may belong to the same statement

the default labels  is optional if present it will be executed when the expression does not find a matching case labels 

there can be at most 1 default labels 

the default maybe place anywhere but usually placed at the end

it is permitted to next switch statement


The ? : operator-:

The C language has an unusual operator useful for making two way decision this operator is a combination of ? and :  and takes 3 operands. this operator is popularly known as the conditional operator. the general form of use of the condition operator is as follows

condition expression ? expression 1: expression 2

the condition expression is evaluated first if the result is non zero expression 1 is evaluated and is returned  as the value of the condition expression. otherwise expression 2 is evaluated and its value is returned  for example 





it written this type

flag =(a<0)?0:1;

The go to statement:- like many other language c supports the go to statement to branch Unconditionally from one point to another in the program although it may not be essential to use the go to statement in a highly structured language like see their maybe occasion when the use of go to might be desirable.

the go to require a label in order to identify the place where the branch is to be made a label is any valid variable name and must be followed by colon The label is placed immediately before the statement where the control is to be transferred 

Leave a Reply

Your email address will not be published. Required fields are marked *