Selection Statements

10 – Decision Making or Selection Statements In C Language

In this chapter, we will learn Selection Statements.

Decision Making

Decision making structure requires that the programmer specifies one or more conditions to be evaluated or tested by the program along with statements to be executed, if the condition is determined to be true & Optionally, if the condition is false other statements will be executed.

The typical decision making structure found in most programming languages is shown below:

Types

There are different types of decision making/Selection statements in C:

SR.NoStatementDescription
1if statementIt consists of a boolean expression which is followed by one or more statement.
2if else statementIt executes on statement if condition is true, but if the condition is false then it will execute the other statement.
3nested if statementWe can use if or if else statement inside another if or if else statement
4switch statementIt is used when multiple choice are given & one choice is to be selected.
5nested switch statementWe can use one switch statement inside another switch statement

if statement

if statement is the simplest form of selection constructs. It is used to execute or skip a statement or statements by checking a condition. The condition is given as a relational expression. If the condition is true, the statement or set of statements after if statement is executed otherwise not executed.

Syntax:
The syntax of if statement is:

if (condition)
statement;

The above is for single statement. A set of statements can also be made conditional by writing the statements in braces { }. A set of statement is also called block of statements. The syntax for block of statements in if statements is:

if (condition)
{
statement 1;
statement 2;
.
.
statement n;

Flowchart

Program as an Example

The following program will demonstrate the use of if statement:


1
2
3
4
5
6
7
8
9
10
11
12
#include<stdio.h>
#include<conio.h>
void main()
{
       int x;
       clrscr();
       print("\n Enter an integer = ");
       scanf("%d",&x);
       if(x>50)
       printf("%d",x);
       getch();
}

(ii) if else statement

It executes one statement or block of statements when the condition is true but if condition is false then it will execute another statement or block of statements. In any situation, one block is executed & the other is skipped.

Syntax:

The syntax for single statement:

if (condition)
statement;

else

statement;

The syntax for Block of statements:

if (condition)
{
statement 1;
statement 2;
.
.
statement n;

}
else
{
statement 1;
statement 2;
.
.
statement n;
}

Flowchart

Program as an Example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<stdio.h>
#include<conio.h>
void main()
{
       int n;
       clrscr();
       print("\n Enter a number = ");
       scanf("%d",&x);
       if(n%2==0)                 // % operator returns reminder after division
       printf("%d is even",n);
       else
       printf("%d is odd",n);
       getch();
}

Switch Statement

Switch statement is used for multiple choice or selection. It is used as a substitute of if-else statements. It is used when multiple choice are given & one choice is to be selected.

Syntax

switch(expression)

{

case const-1:

statements;

break;

case const-2:

statement;

break;

.

.

case const-n:

statement;

break;

default:

statements:

}

Flowchart

Program as an Example


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include<stdio.h>
#include<conio.h>
void main()
{
       char ch;
       clrscr();
       print("\n Enter a character = ");
       scanf("%c",&ch);
       switch(ch)
       {
       case 'a':  
       printf("vowel a");
       break;
       case 'e':
       printf("vowel e");
       break;
       case 'i':
       printf("vowel i");
       break;
       case 'o':
       printf("vowel o");
       break;
       case 'u':
       printf("vowel u");
       break;
       default:
       printf("not a vowel");
       }
       getch();

     
}

Subscribe our channels:

Click Here for more Articles:

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.