You are on page 1of 11

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 () {

/* local variable definition */

int a = 10;

/* check the boolean condition using if statement */

if( a < 20 ) {

/* if condition is true then print the following */

printf("a is less than 20\n" );

printf("value of a is : %d\n", a);

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) {

/* statement(s) will execute if the boolean expression is true */


} else {

/* statement(s) will execute if the boolean expression is false */

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 () {

/* local variable definition */

int a = 100;

/* check the boolean condition */

if( a < 20 ) {

/* if condition is true then print the following */

printf("a is less than 20\n" );

} else {

/* if condition is false then print the following */

printf("a is not less than 20\n" );


}

printf("value of a is : %d\n", a);

return 0;

Ladder if...else...if statements

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.

Else if I have at least 100,000, then I will purchase HP ultra-book.

Else I will purchase some mid-level developer laptop.

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)

// If expression 1 is true then execute

// this and skip other if

else if (boolean_expression_2)

// If expression 1 is false and

// expression 2 is true then execute

// this and skip other if

else if (boolean_expression_n)
{

// If expression 1 is false,

// expression 2 is also false,

// expression n-1 is also false,

// and expression n is true then execute

// this and skip else.

else

// If no expressions are true then

// execute this skipping all other.

Example

/**

* C program to check negative, zero or positive.

*/

#include <stdio.h>

int main()

/* Declare integer variable */

int num;

/* Input an integer from user */

printf("Enter any number: ");

scanf("%d", &num);

if(num < 0)
{

/* If number is less than zero, then it is negative */

printf("NUMBER IS NEGATIVE.");

else if(num == 0)

/* If number equal to 0, then it is zero */

printf("NUMBER IS ZERO.");

else

/* If number is greater then zero, then it is positive */

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) {

/* Executes when the boolean expression 1 is true */

if(boolean_expression 2) {

/* Executes when the boolean expression 2 is true */


}

Example

#include <stdio.h>

int main()

int a;

printf(" Enter your current Age Here:\n");

scanf("%d",&a);

if ( a < 18 )

printf("Consider as minor \n");

printf("Not fit for Working");

else

if (a >= 18 && a <= 65)

printf("He/She is successfully eligible for Working \n");

printf("Fill all the details and apply for it\n");

else

printf("Age is not satisfactory according to the organization norms\n");

printf("Ready for retirement and can collect pension \n");

return 0;

}
Example 2

Write a C program to check if a certain number X is even and divisible by 4

#include <stdio.h>

int main() {

// variable to store the given number

int n;

//take input from the user

scanf("%d", &n);

//if else condition to check whether the number is even or odd

if (n % 2 == 0) {

//the number is even

printf("Even ");

//nested if else condition to check if n is divisible by 4 or not

if (n % 4 == 0) {

//the number is divisible by 4

printf("and divisible by 4");

} else {

//the number is not divisible by 4

printf("and not divisible by 4");

} else {

//the number is odd

printf("Odd ");

//nested if else condition to check if n is divisible by 3 or not

if (n % 3 == 0) {

//the number is divisible by 3

printf("and divisible by 3");


} else {

//the number is not divisible by 3

printf("and not divisible by 3");

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

The syntax for a switch statement in C programming language is as follows −

switch(expression) {

case constant-expression :

statement(s);

break; /* optional */

case constant-expression :

statement(s);

break; /* optional */

/* you can have any number of case statements */

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 () {

/* local variable definition */

char grade = 'B';

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' :

printf("Better try again\n" );

break;

default :
printf("Invalid grade\n" );

printf("Your grade is %c\n", grade );

return 0;

You might also like