Decision making in C
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. C language handles decision-making by supporting the
following statements,
• if statement
• switch statement
• conditional operator statement (? : operator)
• goto statement
Decision making with if statement
The if statement may be implemented in different forms depending on the
complexity of conditions to be tested. The different forms are,
1. Simple if statement
2. if....else statement
3. Nested if....else statement
4. Using else if statement
Simple if statement
The general form of a simple if statement is,
if(expression)
{
statement inside;
} statement
outside;
@ BY MR. CHERUIYOT K. PETER B SC.
If the expression returns true, then the statement-inside will be executed,
otherwise statement inside is skipped and only the statement-outside is
executed.
Example:
#include <stdio.h>
void main( )
{ int x,
y; x=
15; y=
13; if (x
>y)
{
printf("x is greater than y");
}
}
Output: x
is greater
than y
if...else statement
The general form of a simple if...else statement is,
if(expression)
{
statement block1;
}
else
{
@ BY MR. CHERUIYOT K. PETER B SC.
statement
block2;
If the expression is true, the statement-block1 is executed, else statement-
block1 is skipped and statement-block2 is executed.
Example:
#include <stdio.h>
void main( )
{ int x,
y; x=
15; y=
18; if (x
>y)
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Output: y
is greater
than x
@ BY MR. CHERUIYOT K. PETER B SC.
Nested if.... else statement
The general form of a nested if...else statement is,
if (expression)
{
if (expression1)
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement
block3;
if expression is false then statement-block3 will be executed, otherwise the
execution continues and enters inside the first if to perform the check for the
next if block, where if expression 1 is true the statement-block1 is executed
otherwise statement-block2 is executed.
Example:
#include <stdio.h>
void main( )
@ BY MR. CHERUIYOT K. PETER B SC.
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}
@ BY MR. CHERUIYOT K. PETER B SC.
else if ladder
The general form of else-if ladder is,
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if (expression3)
{
statement block3;
}
else
{
default statement;
}
The expression is tested from the top(of the ladder) downwards. As soon as a
true condition is found, the statement associated with it is executed.
Example:
#include <stdio.h>
void main ()
{
int a;
printf("Enter a number...");
@ BY MR. CHERUIYOT K. PETER B SC.
scanf("%d", &a);
if(a%5 == 0 && a%8
== 0)
{
printf("Divisible by both 5 and 8");
}
else if(a%8 == 0)
{
printf("Divisible by 8");
}
else if(a%5 == 0)
{
printf("Divisible by 5");
}
else
{
printf("Divisible by none");
}
}
The Coditional or Ternary Operator (? : )
We have covered conditional operator ? : in the previous chapter which can be
used to replace if...else statements. It has the following general form −
variable = Exp1 ? Exp2 : Exp3;
Where Exp1, Exp2, and Exp3 are expressions. Notice the use and placement of
the colon.
@ BY MR. CHERUIYOT K. PETER B SC.
The value of a ? expression is determined like this −
• Exp1 is evaluated. If it is true, then Exp2 is evaluated and becomes the
value of the entire ? expression.
• If Exp1 is false, then Exp3 is evaluated and its value becomes the value of
the expression.
It can be visualized into if-else statement as:
if(Expression1)
{
variable = Expression2;
}
else
{
variable = Expression3;
}
Since the Conditional Operator ‘?:’ takes three operands to work, hence they are
also called ternary operators.
Example :
// C program to find largest among two
// numbers using ternary operator
#include <stdio.h>
int main()
int m = 5, n = 4;
@ BY MR. CHERUIYOT K. PETER B SC.
(m > n) ? printf("m is greater than n that is %d > %d",
m, n)
: printf("n is greater than m that is %d > %d",
n, m);
return 0;
@ BY MR. CHERUIYOT K. PETER B SC.