Decision Making Statement in C Programming Language

Programming

Decision making statement: a statement that changes the flow of the program is known as decision making statement. there are following types of decision making statement are there in C programming language.

Types of Decision Making Statement

  • simple if
  • if else
  • nested if else
  • else if ladder
  • switch case

simple if: this is decision making statement, generally used when there is only one possibility.

Syntax:
if(expression)
{
//statement
}

Example:
int a=18;
int age;
if(age>=18)
printf(“eligible for vote”);

as per the above syntax if is a reserved word which considered a conditional statement that returns either true or false. it must have an argument known as expression, generally, the expression may be a relational expression or logical expression. it can not be terminated using semicolon ;. if there is more then one statement then it must be written between opening curly bracket and ending curly bracket but the default scope is only a single statement.

As in the above example, if age>=18 then displays voting eligible, otherwise the rest of the program will be executed.

if else: it is also known as two-way decision making statement. generally used when there are two possibility where if condition is false.

Syntax:
if(expression)
{
//if block (statement)
}
else
{
//else block
}

Example:
int a=20;
if(a%==0)
printf(“even”);
else
printf(“odd”);

Since the above syntax if else is a keyword and can not be finished with semicolon ;. If the condition is false then execute the else part. We can’t write block else without if. Generally used when there is a group of decision making statements.

nested if else:  An if else statement is written in a different if else statement. Where there is a series of decisions we can use nested if else statements at that time.

Decision Making Statement

Syntax:
if(condition 1)
{
if(condition 2)
{
//statement
}
else
{
//statement
}
}
else
{
if(condition 3)
{
//statement
}
else
{
//statement
}
}

as per the above example condition 1 is true then control transfer to check condition 2, if condition 2 is false then else part will be evaluated. if first if condition is false then else part evaluated. in else part again condition 3 will be stared if it is true the if part will be evaluated control transfer to the rest of the program.

else if ladder: if else statements together is known as else if where the sequence of else if statements are known as else if ladder. generally, use when there are more than two possibilities. if there are a series of decisions making a statement at that time we can use else if ladder statement.

else if ladder

Syntax:
if(condition 1)
{
//if statement
}
else if(condition 2)
{
//else if block 1
}
else if(condition 3)
{
//else if block 2
}
else if(condition n)
{
//else if block n
}
else
{
//default statement
}

Example:
int a=20, b=30, c=40;
if(a>b&&a>c)
printf(“a is greater”);
else if(b>c)
printf(“b is greater”);
else
printf(“c is greater”);

As per the above syntax else if statement, it must be known as a condition by a single parameter. Not ending with a semicolon ;. The flow of the program is from top to bottom. else is the default part, It will be executed if all condition are false.if Check first if condition, it’s false then check else if part, it’s false check another else if condition again, if any condition is true then that part will be checked and control transferred to the rest of the program.

switch case: switch case statement is a multiway branch statement. a group of statements where the statement will be executed base on selection. it is faster then else if ladder.

switch case

Syntax:
switch(expression)
{
case ex1: statemente;
break;
case ex2: statemente;
break;
case ex3: statemente;
break;
case exn: statemente;
break;
default: statement;
}

Example:
int choice;
scanf(“%d”,&choice);
switch(choice)
{
case1: printf(“Monday”);
break;
case1: printf(“Tuesday”);
break;
case1: printf(“Wednesday”);
break;
case1: printf(“Thursday”);
break;
case1: printf(“Friday”);
break;
case1: printf(“Saturday”);
break;
case1: printf(“Sunday”);
break;
default: printf(“invalid choice”);
}

as per the above syntax, the switch is a keyword that takes a single argument based on that argument case statement that will be executed. opening curly bracket { are necessary which define the scope of the switch statement. the case is a keyword that refers to which statement will be evaluated. the expression refers to the name of the variable. the variable must be types of integer or character but float and string are not allowed. the semicolon ; is required which specifies the scope of the case statement. the break statement is required to terminate the switch statement if the break is missing then one by one case statements are checked. default is a keyword which work same as else statement, if no case matches then the default part will be evaluated. there is not necessary to write a break statement here. switch statement not end with a semicolon ;.