C language: Decision Making in C programming language

decision making in c language

The basic decision statement in computers is the selection structure. The decision is described to the computer as a conditional statement that can be answered as True or False in C language.

In some situations have to change the execution order of statements based on conditions or repeat a set of statements until certain conditions are met. In such situations, Conditional and Control statements are very useful. C language provides four general categories of control structures.

⊕ Sequential structure, in which instructions are executed in sequence. Example:

 i = i + 1;
 j = j + 1;

⊕ Selection structure, here the sequence of the instructions is determined by using the result of the condition. Example:

if(x>y)
 i = i + 1;
else
 j = j + 1;

if the condition is true, then the statement i=i+1 will be executed otherwise it executes j=j+1;

⊕ Iteration structure, in which statements are repeatedly executed otherwise forms program loops. Example:

for(i=1;i<=5;i++)
{
   i = i + 1;
}

the statement i=i+1 will be executed 5 times and the value of i will change from 1,2,3,4 and 5.

⊕ Encapsulation structure, in which other compound structures are included.

Example:

You can include an if statement in a for loop or a for loop in an if statement.

The if statement

The if statement is a decision-making statement. It is used to control the flow of execution of the statements and also used to test logically whether the condition is true or false.

if statement in c programming language
Syntax: if(condition is true)
{
True statements;
}

If the condition is true, then the True statements are executed. The ‘True statements’ may be a single statement or group of statements.

If the condition is false then the true statements are not executed, instead, the program skips past it. The condition is given by the relational operator like ==, !=,<=,>=, etc.

Example program

Program to illustrate the use of the if statement.

Output:
Enter the number < 10...5
The entered numbeer 5 is < 10

Explanation: In this program, the value of variable i is entered through the keyword. The value of i is checked with if statement i<10.

If the i value is less than 10 then print the message “The entered number 5 is < 10”.

The if-else statement in c language

It is basically a two-way decision-making statement and is always used in conjunction with conditions.

It is used to control the flow of execution and also used to carry out the logical test and then pick up one of the two possible actions depending on the logical test.

It is used to execute some statements when the condition is true and execute some other statements when the condition is false.

if-else statements in C language
Syntax: if(condition)
{
True statements;
}
else
{
False statements;
}

If there is only one statement in the if block (or) else block, then the braces are optional. But if there is more than one statement, the braces are compulsory.

Example program

Program to find the biggest among the two numbers.

Output:

Enter two values:34 98
biggest of two numbers is 98
biggest of two numbers(using else) is 98

Explanation: This program is used to find the biggest in two numbers which are taken from the input using an if-else statement.

Nested if….else statement in C language

When a series of if..else statements occur in a program, we can write an entire if..else statement in another if..else statement called nesting, and the statement is called nested if.

nested if-else statements in c languagein c programming
Syntax:
if(condition 1)
{
if(condition 2)
{
True statement2;
}
else
{
False statement2;
}
}
else
{
false statement 1;
}

Example program

Program to demonstrate nested if..else statement

Output:

Enter a number...10
Play Cricket

The if…else Ladder in c language

Nested if statements can become quite complex. If there are more than three alternatives and indentation is not consistent, it may be different for you to determine the logical structure of the if statement.

In situations, you can use the nested if as the else if ladder.

if-else ladder statement in c programming
Syntax:
if(condition 1)
{
Statement 1;
}
else if(condition 2)
{
Statement 2;
}
else if(condition 3)
{
Statement 3;
}
else
{
default statements;
}

The switch statement in C

In C language switch statement is used to pick up or execute a particular group of statements from several available groups of statements. It allows us to make a decision from the number of choices.

It is a multiway decision statement, it tests the value of a given variable or expression against a list of case values and when a match is found, a block of statements associated with that case is executed.

Switch statement in c programming language
Syntax:
switch(expression)
{
case constant 1:
block1;
break;
case constant 2:
block2;
break;
.
.
.
default :
default block;
break;
}

Rules for writing switch() statement:

  • The expression in the switch statement must be an integer value or a character constant.
  • No real numbers are used in an expression.
  • Each case block and default block must be terminated with break statements.
  • The default is optional and can be placed anywhere, but usually placed at the end.
  • The case keyword must terminate with a colon (:).
  • No two case constants are identical.
  • The case labels must be constants.
  • The switch can be nested.
  • The value of the switch is compared with the case constant expression in the order specified.
  • In the absence of a break statement, all statements that are followed by matched cases are executed.

Example program

The program to print the given number is odd/even using a switch case statement.

Output:

Enter a Number : 7
the number 1 is odd
The number 2 is even
The number 3 is odd
The number 4 is even
The number 5 is odd
The number 6 is even
The number 7 is odd

Explanation: 

This program is used to find out whether the given number is even or odd and it checks for the n numbers.

If the number is divided by 2 then it is even, if the number is not divided by 2 then it is an odd number.