Decision Control Instructions

Decision control instruction is also known as selection control instruction. C language provides three ways to implement this instruction:

  • if
  • if else
  • Conditional operator






if

Following is the syntax of if statement:

int main()
{
..…
…..
if(some condition)
{
Statement1;
Statement2;
….
}

}

if is a keyword which let compiler know the decision control statement begins. Immediately after if keyword some condition is there in the parenthesis. This condition can be any valid expression of C language. Condition is always evaluated as true or false. If the result of expression is non-zero it is considered as TRUE otherwise FALSE.
Immediately after this condition there is a block of code. This block is known as if block. Whatever we write in if block will be execute only when condition is TRUE.
When condition is false control skip if block and execute statements written after if block.
Example
:
int main()
{
int marks;
printf(“Enter marks “);
scanf(“%d”,&marks);
if(marks>=33)
{
printf(“You are PASS”);
}
if(marks<33) { printf(“You are FAIL”); } return(0); }

Sample Output:

Enter marks 45
You are PASS

Sample Output:

Enter marks 23
You are FAIL

In this program output depends on the value given by user. Variable marks hold the value entered by user. We have used two if statements. In the first if statement we use the condition marks>=33, thus if the marks is greater than or equal to 33 condition becomes TRUE and if block executed, otherwise if block is skipped.
Whatever may the result of first if condition, control has to reach second if statement.
If marks is less than 33 condition is TRUE and execute if block otherwise if block is skipped.

if else

Following is the syntax of if else statement:

int main()
{
..…
…..
if(some condition)
{
Statement1;
Statement2;
….
}
else
{
Statement1;
Statement2;
….
}

}

This is similar to if but the else block is new add on. If the condition of if is TRUE if block is executed and if the condition of if is FALSE else block is executed.
You can use if statement without else block but else must have paired with if.
Else block should appear immediately after if block otherwise an error occurred during compilation
Example

int main()
{
int marks;
printf(“Enter marks “);
scanf(“%d”,&marks);
if(marks>=33)
{
printf(“You are PASS”);
}
else
{
printf(“You are FAIL”);
}
return(0);
}

Conditional Operator ( ?: )

Conditional operator is the only operator in C language which requires three operands, hence known as ternary operator. Following is the syntax of conditional operator:
Expression1 ? expression2 : expression3;

Expression 1 is condition, evaluated as true or false. When expression 1 is true expression 2 is selected otherwise expression 3 is selected.
Example

int main()
{
int x,y;
printf(“Enter two numbers”);
scanf(“%d%d”,&x,&y);
x>y ? printf(“%d is greater”,x) : printf(“%d is greater”,y);
return(0);
}

Output:
Enter two numbers45
33
45 is greater

Example

main()
{
int x,y,max;
printf(“Enter two numbers”);
scanf(“%d%d”,&x,&y);
max=x>y ? x : y;
printf(“Greater number is %d”,max);
}

Nested if else

When if-else statements written in if block or else block, it is called nested if-else
Example: Program to print greater among three numbers

int main()
{
int a,b,c;
printf(“Enter three numbers: “);
scanf(“%d%d%d”,&a,&b,&c);
if(a>b)
{
if( a>c)
printf(“%d is greater”,a);
else
printf(“%d is greater”,c);
}
else
{
if( b>c)
printf(“%d is greater”,b);
else
printf(“%d is greater”,c);
}
return(0);
}

else if ladder

Else if ladder is nothing but a special case of nesting where if else resides only in else block.
Syntax of else if ladder:

if()
{
—————
————
}
else if( )

{
—————
————
}
else if( )
{
—————
————
}
else
{
—————
————
}

Only one block is selected for execution.
Example: Program to check whether an year is leap year or not:

int main()
{
int year;
printf(“Enter a year”);
scanf(“%d”,&year);
if(year%4!=0)
printf(“Not a Leap year”);
else if(year%100!=0)
printf(“Leap year”);
else if(year%400!=0)
printf(“Not a Leap Year”);
else
printf(“Leap Year”);
return(0);
}

Decision Control Instruction
Tagged on: