C Decision Making Statement In easy way part 1

C Decision Making Statement

As the name says decision making statements helps us to take decision based on certain condition. In C programming we have following decision making statements.

  1. if statement
  2. if-else statement
  3. else-if statement
  4. nested if-else statement
  5. switch statement

if statement

decision-making-statement-if-itvoyagers

This is the most simple and easy decision making statement in C program. In this, if statement will check for certain condition and if the condition is true then it will execute block of code written in it. If the condition is false then it will not execute block of code.

Like while loop if block also accepts boolean value which means if we enter 0 as condition then it will be considered as false and non-zero value will be considered as true.

Syntax:

    if(condition)
{
  statements;
}

Example:

    int i = 2;
if(i<5)

{
  printf("Value of i is less than 5");
}

Output:

    Value of i is less than 5
ITVoyagers

if-else statement

decision-making-statement-if-else-itvoyagers

In this is we have two separate blocks of code and one of them get executed based of certain condition. We have if block and else two separate blocks, if block check the condition and if the condition is true then if block will get executed and if the condition is false then else block will get executed.

In simple word there are two separate blocks of code and which block will get executed is depend on condition mention in if’s parameter.

In below example we have set value of i variable to 12, next in if parameter we are checking if value of i is less than 5, now we can see that condition is false, hence else block will get executed instead of if block.

Like while loop if block also accepts boolean value which means if we enter 0 as condition then it will be considered as false and non-zero value will be considered as true.

Syntax:

    if(condition)
{
  statements;
}
else
{
statements;
}

Example:

    int i = 12;
if(i<5)

{
  printf("Value of i is less than 5");
}
else
{
printf("Value of i is greater than 5");
}

Output:

    Value of i is greater than 5
ITVoyagers

else-if statement

decision-making-statement-else-if-itvoyagers

We use else-if block for multipath decision making.

In this is we have many separate blocks of code and one of them get executed based of certain condition. We have three block starting with if block, else if block and else block. First if block will check if mention condition is true or not, if the condition is true then if block will get executed and if the condition is false then pointer shifts to very next else if block.

else if block will check for its condition, if the condition is true then else if block will get executed and if the condition is false then pointer shifts to very next else if block.

And this goes on until one condition get satisfied or last block i.e. else block will get executed. 

In simple words if block and else if blocks will check for certain condition one by one and who ever’s condition get satisfied first then respective block is executed, and if no condition is satisfied then last block i.e. else block will get executed.

There is no limit on count of else if block we can have as many as we want.

We cannot use else if block and else block without if block.

Like while loop if block and else if block also accepts boolean value which means if we enter 0 as condition then it will be considered as false and non-zero value will be considered as true.

Syntax:

    if(condition)
{
  statements;
}
else if(condition)
{
statements;
}
else if(condition)
{
statements;
}
else if(condition)
{
statements;
}
.
.
.
else
{
statements;
}

Example:

    int i = 3;
if(i==1)

{
  printf("Value of i is equal to 1");
}
else if(i==2)
{
printf("Value of i is equal to 2");
}
else if(i==3)
{
printf("Value of i is equal to 3");
}
else if(i==4)
{
printf("Value of i is equal to 4");
}
else
{
printf("Value of i is not 1, 2, 3 or 4");
}

Output:

    Value of i is equal to 3
ITVoyagers

Try this fun quiz

CHECKOUT OTHER RELATED TOPICS

CHECKOUT OTHER QUIZZES

PRACTICALS IN C PROGRAM

We are aiming to explain all concepts of C in easiest terms as possible.
ITVoyagers-logo
ITVoyagers
Author

Leave a Comment