Decision making is about deciding the order of execution of statements based on certain conditions or repeat a group of statements until certain specified conditions are met. 

If statement:
Syntax :
if(expression)
statement1;
Explanation :
 Expression is Boolean Expression
 It may have a true or false value

Meaning of If Statement :
 It Checks whether the given Expression is Boolean or not !!
 If Expression is True Then it executes the statement otherwise jumps to next_instruction
Sample Program Code :

void main()
{
   int a=5,b=6,c
   c = a + b ;if (c==11)
   printf("Execute me 1");
   printf("Execute me 2");
}

Output:
Execute me 1

If Statement :

if(conditional)
{
    Statement No 1
    Statement No 2
    .
    .
    .
    the Statement No N
}

Note:-
More than one condition can be Written inside the If statement.

  1. Opening and Closing Braces are required only when ―Code‖ after if statement
    occupies multiple lines.

if(conditional)
Statement No 1
Statement No 2
In the above example, only Statement 1 is a part of the
if Statement.

Code will be executed if the condition statement is True.

Non-Zero Number Inside if means “TRUE Condition”

if(100)
    print("True Condition");

if-else Statement :

We can use the if-else statement in c programming so that we can check any condition and depend on the outcome of the condition we can follow the appropriate path. We have a true path as well as a false path.

Syntax :

if(expression)
{
   statement1;
   statement2;
}
else
{
   statement1;
   statement2;
}

Explanation :
If the expression is True then Statement1 and Statement2 are executed Otherwise,
Statement3 and Statement4 are executed. Sample Program on if-else Statement :

void main()
{
    int marks=50;
    if(marks>=40)
    {
       printf("Student is Pass");
    }
    else
    {
       printf("Student is Fail");
    }
}

Output :
Student is Pass
Flowchart: If Else Statement

Consider Following Example –

int num = 20;
if(num == 20)
{
printf("True Block");
}
else
{
printf("False Block");
}
If part Executed if Condition Statement is True.
if(num == 20)
{
printf("True Block");
}
True Block will be executed if condition is True.
Else Part executed if Condition Statement is False.
else
{
printf("False Block");
}

Switch statement:-

Why we should use Switch Case?

  • One of the classic problems encountered in the nested if-else / else-if ladder is called the problem of Confusion.
  • It occurs when no matching else is available for if.
  • As the number of alternatives increases the Complexity of the program increases drastically.
  • To overcome this, C Provide a multi-way decision statement called ‗Switch Statement‗

See how difficult is this scenario?
if(Condition 1)
Statement 1
else
{
Statement 2
if(condition 2)
{
if(condition 3)

statement 3
else
if(condition 4)
{
statement 4
}
}
else
{
statement 5
}
}

switch(expression)
{
case value1 : 
body1 
break;
case value2 : 
body2 
break;
case value3 : 
body3 
break;
default :
default-body 
break;
}

How does it work?

  • Switch case checks the value of expression/variable against the list of case values and when the match is found, the block of statement associated with that case is executed.
  • The expression should be Integer Expression / Character
  • Break statement takes control out of the case.
  • Break Statement is Optional.
#include<stdio.h>
void main()
{
int roll = 3 ; 
switch ( roll )
{
case 1:
printf ( " I am Pankaj "); 
break;
case 2:
printf ( " I am Nikhil "); 
break;
case 3:
printf ( " I am John "); 
break;
default :
printf ( "No student found"); 
break;
}
}

As explained earlier –
3 is assigned to integer variable roll
On line 5 switch case decides We have to execute a block of code specified in 3rd case
Switch Case executes code from top to bottom.
It will now enter into the first Case [i.e case 1:]
It will validate the Case number with variable Roll.
If no match is found then it will jump to Next Case.
When it finds a matching case it will execute a block of code specified in that case.

Prev: OPERATORS IN C

One comment on “Decision Statements”

Write a comment