Decision making in C
Decision making in C programming language, allows a program to decide which statements should gets executed on the basis of true condition.
c language has following decision making
- if
- if else
- if else if ladder
- switch
if in c
if allows to execute statement, when given expression or condition is true.
Syntax
if(expression)
{
//set of statements
}
if else in c
Expression or condition of if is true, then statements of if block gets executed, if it is false, then else block statements will execute. Else block executes only when if condition becomes false.
Syntax
if(expression)
{
//set of statements
}
else
{
//set of statements
}
if elseif ladder in c
It is similar like if else, but if elseif ladder, allows to check multiple expression or condition.
- if always comes first.
- else if must comes after if.
- There can be any number of else if.
- else must comes at the end of else if.
Syntax
if(expression)
{
//set of statements
}
else if(expression)
{
//set of statements
}
else if(expression)
{
//set of statements
}
else
{
//set of statements
}
switch in c
switch statements, allows to execute case matched with expression value. If not any matched case found, It executes default block.
Syntax
switch(expression)
{
case:
// set of statements
break;
default:
// set of statements
}