Decision Making in C

By | August 19, 2020

C conditional statements allow you to make a decision, based upon the result of a condition. These statements are called Decision Making Statements or Conditional Statements.

Decision making statements available in C are:

  1. if statement
  2. if..else statements
  3. nested if statements
  4. if-else-if ladder
  5. switch statements
  6. Jump Statements:
    1. break
    2. continue
    3. goto
    4. return

1. if statement –
If a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if(expression)
{
    statement inside;
}
    statement outside; 

2. if..else statements –
If a certain condition is true then a block of statement is executed otherwise else part will be executed.

Syntax :

if(expression)
{
    statement block1;
}
else
{
    statement block2;
}

3. nested if statements –
Nested if statements means an if statement inside another if statement.

Syntax :

if( expression )
{
    if( expression1 )
    {
        statement block1;
    }
    else 
    {
        statement block2;
    }
}
else
{
    statement block3;
} 

4. if-else-if ladder –
There can be again if condition inside else part of a if condition. Statement associated with that if is executed, and the rest of the C else-if ladder is bypassed. Final else statement will be executed, if none of the conditions are true.

Syntax :

if(expression1)
{
    statement block1;
}
else if(expression2) 
{
    statement block2;
}
else if(expression3 ) 
{
    statement block3;
}
else 
    default statement;

5. switch statements –
C switch statement is used when you have multiple possibilities for the if statement. Switch case will allow you to choose from multiple options.

Syntax :

switch(variable)
{
 case 1:
   //execute your code
 break;

 case n:
   //execute your code
 break;

 default:
   //execute your code
 break;
}

Note that there can be nested switch statements.

6. ? : Operator –
It is ternary operator. Conditional operator is of the form

Expression1 ? Expression2 : Expression3  

Expression1 is the condition to be evaluated.

  • If the condition(Expression1) is True then we will execute and return the result of Expression2, Otherwise we will execute and return the result of Expression3.
  • We may replace the use of if..else statements by conditional operators.

Important Notes :

  • We should enclose conditions using into curly braces ‘{ … }’, for if statements, otherwise only first line will be considered as expression of if part.
  • ‘==’ must be used for comparison in the expression of if condition, if you use ‘=’ the expression will always return true, because it performs assignment not comparison.
  • Other than 0(zero), all other values are considered as true
  • ‘beak’ loop control statement is used to terminate the loop.
  • ‘continue’ statement is opposite to that of break statement, instead of terminating the loop, it forces to execute the next iteration of the loop.
  • ‘goto’ statement is a unconditional jump statement can be used to jump from one point to another within a function.
  • ‘return’ in C, returns the flow of the execution to the function from where it is called.



Please write comments if you find anything incorrect. A gentle request to share this topic on your social media profile.