Decision making in C


In decision making structure we decide that in which order the statements of the program will be executed based on certain condition. Decision making is also called selection structure in which statements will be executed on the basis of condition. For example a program displays pass if the student gets 40 or more than 40 marks. It displays fail when the marks are below 40. The program checks the marks before displaying the message. This process is known as decision making or selection. The following are the decision making statements that are supported by C programming language:

  • if statement
  • switch statement
  • conditional operator statement
  • goto statement


Decision making with if statement:

In C programming language, we have different types of ‘if statements’ according to the complexity of the problem. The following are the types of ‘if statements’ that are used in C programming language:

  • Simple ‘if statement’
  • ‘if else’ statement
  • ‘Nested if else’ statement
  • ‘else if’ statement


Simple ‘if statement’:

‘if’ is a keyword in C programming language. Simple ‘if’ statement is a decision making statement. It is the simplest form of selection construct. It is used to execute or skip a statement by checking a condition.

The condition is given as a relational expression. If the condition is true, the statement after ‘if statement’ is executed. If the condition is false, the statements after ‘if statement’ is not executed.

SYNTAX:

The syntax of ‘if statement’ is as follows:

If (condition)

Statement;

The above syntax is used for single statement. A set of statements can also be made conditional. In this case, these statements are written in curly braces. The set of statements are also called compound statement.

EXAMPLE:

# include <stdio. h>

# include < conio. h>you have passed

void main ()

{

int marks;

printf (“Enter your marks”);

scanf (“%d”, &marks);

If (marks > = 40)

printf (“you have passed”);

}


If else statement:

It is another type of if statement. It executes one block of statement when the condition is true and the other when it is false. In any situation, one block is executed and the other is skipped. In if else statement:

  • Both blocks of statement can never be executed.
  • Both blocks of statements can never be skipped.

SYNTAX:

if (condition)

statement;

else

statement;

Two or more statements are written in curly brackets { }.

EXAMPLE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int n;condition

printf (“enter a number:”);

scanf (“%d”, &n);

if (n % 2== 0)

printf (“n is even”);

else

printf (“n is odd”);

}


‘Nested if else’ statement:

An ‘if else’ statement within another ‘if else’ statement is called ‘nested if else’ statement. In the nested structure, the control enters into the inner ‘if else statement’ only when the outer condition is true. Only one block of statements are executed and the remaining blocks are skipped automatically.

The user can use as many ‘if else’ statements inside another ‘if else statement’ as required. The increase in the level of nesting increases the complexity of ‘nested if statement’.

SYNTAX:

The following is the syntax of ‘nested if else’ statement:

if (condition)

if (condition)

{

statements;

}

else

{

statements;

}

else

{

statements;

}


Working of Nested if else:

In ‘nested if else’ statement, the condition of outer ‘if else’ is evaluated first. If it is true, the control enters in the inner ‘if’ block. If the condition is false, the inner ‘if else’ is skipped and control directly moves to the ‘else’ part of outer ‘if else’. If the outer ‘if else’ is true then control enters in the inner ‘if statement’. The inner ‘if else’ evaluated according to simple ‘if’ statement.

Example:

Consider the following code that inputs three numbers and displays the smallest number:

CODE:

# include <stdio. h>

# include <conio. h>

void 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 smallest number”, a);

else

printf (“%d is smallest number”, c);

else

if (b < c)

printf (“%d is smallest number”, b);

else

printf (“%d is smallest number”, c);

getch ();

}


Working of above program:

The above program inputs three numbers and finds the smallest one. When the control enters the outer ‘if’, first condition if (a<b) is evaluated. If it is true, the control enters in the inner ‘if else’ and the condition if (a <c) is evaluated. If it is true, the value of ‘a’ is displayed, otherwise the value of ‘c’ is displayed.

If the first condition if (a < b) is false, the control shifts to else part of ‘if statement’. Now the condition if (b <c) is evaluated. If it is true the value of ‘b’ is displayed, otherwise the value of ‘c’ will be displayed.


‘else if’ ladder statement:

“If else if” ladder statement can be used to choose one block of statements from many blocks of statements. It is used when there are many options and only one block of statements should be executed on the basis of a condition.


Flowchart of if else if ladder statement:

Flowchart of if else if ladder statement

SYNTAX:

The syntax of this structure is:

if (condition 1)

{

Block 1;

}

else if (condition 2)

{

Block 2;

}

else if (condition 3)

{

Block 3;

}

:

:

else

{

Block n;

}


Working of If else if:

The test conditions in if else statement with multiple alternatives are executed in a sequence until a true condition is reached. If a condition is true, the block of statements following the condition is executed. The remaining blocks are skipped. If a condition is false, the block of statements following the condition is skipped. The statement after the last else are executed if all conditions are false.


Example of if else if ladder statement:

Consider the following example in which we have input a number and the code determines whether the number is positive, negative or zero:

CODE:

# include <stdio. h>

# include <conio. h>

void main ()

{

int n;

printf (“Enter a number”);

scanf (“%d”, &n);

if (n > 0)

printf (“The number is positive”);

else if (n <0)

printf (“The number is negative”);

else

printf (“The number is zero”);

getch ();

}


Points to remember:

The following are some of the points that should be kept in mind when using decision making structures:

  1. If the ‘simple if’ statement has only one statement then there is no need of using curly braces. Multiple statements in ‘if statement’ will require curly braces.
  2. To compare values you should use (==), if the user uses (=) only then only true will be returned.
  3. All the values in the ‘if statement’ are considered as true other than 0. For example: if (34) is true and the control will enter the body of statement but if (0) is false and the control will not enter the body of the statement.