Decision Making in C
Decision Making in C
Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the condition is
determined to be false.
if statement
If the Boolean expression evaluates to true, then the block of code inside the 'if' statement will be
executed. If the Boolean expression evaluates to false, then the first set of code after the end of the 'if'
statement (after the closing curly brace) will be executed.
C programming language assumes any non-zero and non-null values as true and if it is either zero or
null, then it is assumed as false value.
Flow Diagram
#include <stdio.h>
int main () {
int a = 10;
if( a < 20 ) {
return 0;
if else statement
An if statement can be followed by an optional else statement, which executes when the Boolean
expression is false.
if(condition) {
If the Boolean expression evaluates to true, then the if block will be executed, otherwise, the else block
will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either zero or
null, then it is assumed as false value.
#include <stdio.h>
int main () {
int a = 100;
if( a < 20 ) {
} else {
return 0;
Simple if statement gives ability to execute tasks based on some condition. Its extension if...else takes
both sides of the condition and execute some statements if conditions is true or if the condition is false
then, execute some other statement.
However, things in real life is not simple. Let us consider a real life conditional problem –
If I have at least 150,000 KSH. then I will purchase Microsoft Surface Pro 4.
Else if I have at least 120,000, then I will purchase Apple Mac book Air.
Each condition in the scenario above is dependent on a parent condition and must be checked
sequentially. Also if a condition is true then all other conditions must be ignored. These situations in C
programming are handled using a combination of if...else...if statement.
if...else...if is an extension of if...else statement. It specifies “If some condition is true then execute
some task; otherwise if some other condition is true, then execute some different task; if none
conditions are true then execute some default task.”
if (boolean_expression_1)
else if (boolean_expression_2)
else if (boolean_expression_n)
{
// If expression 1 is false,
else
Example
/**
*/
#include <stdio.h>
int main()
int num;
scanf("%d", &num);
if(num < 0)
{
printf("NUMBER IS NEGATIVE.");
else if(num == 0)
printf("NUMBER IS ZERO.");
else
printf("NUMBER IS POSITIVE.");
return 0;
Nested If
In C programming, we are allowed to nest if-else statements, which means you can use one if or else if
statement inside another if or else if statement(s).
Nested If in C is helpful if you want to check the condition inside a condition. If Else Statement prints
different statements based on the expression result (TRUE, FALSE). Sometimes we have to check even
further when the condition is TRUE.
For example, every person is eligible to work if he is 18 years old or above else he is not eligible.
However, companies will not give a job to every person. So, we use another IF Statement, also called as
Nested If Statement in C, to check his education qualifications or any specific company requirements.
Syntax
if( boolean_expression 1) {
if(boolean_expression 2) {
Example
#include <stdio.h>
int main()
int a;
scanf("%d",&a);
if ( a < 18 )
else
else
return 0;
}
Example 2
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
if (n % 2 == 0) {
printf("Even ");
if (n % 4 == 0) {
} else {
} else {
printf("Odd ");
if (n % 3 == 0) {
return 0;
switch statement
A switch statement allows a variable to be tested for equality against a list of values. Each value is called
a case, and the variable being switched on is checked for each switch case.
Syntax
switch(expression) {
case constant-expression :
statement(s);
break; /* optional */
case constant-expression :
statement(s);
break; /* optional */
default : /* Optional */
statement(s);
Rules
The expression used in a switch statement must have an integral or enumerated type, or be of a
class type in which the class has a single conversion function to an integral or enumerated type.
You can have any number of case statements within a switch. Each case is followed by the value
to be compared to and a colon.
The constant-expression for a case must be the same data type as the variable in the switch, and
it must be a constant or a literal.
When the variable being switched on is equal to a case, the statements following that case will
execute until a break statement is reached.
When a break statement is reached, the switch terminates, and the flow of control jumps to the
next line following the switch statement.
Not every case needs to contain a break. If no break appears, the flow of control will fall through
to subsequent cases until a break is reached.
A switch statement can have an optional default case, which must appear at the end of the
switch. The default case can be used for performing a task when none of the cases is true. No
break is needed in the default case.
#include <stdio.h>
int main () {
switch(grade) {
case 'A' :
printf("Excellent!\n" );
break;
case 'B' :
case 'C' :
printf("Well done\n" );
break;
case 'D' :
printf("You passed\n" );
break;
case 'F' :
break;
default :
printf("Invalid grade\n" );
return 0;