Selection Control Statement in C
Selection Control Structures
Introduction
In most of the programs that we discussed till now, program statements were executed in the same order in which they were written. Each instruction was executed only once. This is not enough in programming. Sometimes we may have to execute program statements based on the given condition, sometimes we may have to execute program statements repeatedly, sometime we may have to choose an option and perform the task accordingly. To carry out all these tasks and other similar tasks, program statements must be executed in a controlled way and it can be done using control structure.
Control Structures are those programming constructs which control the flow of program statements execution in a program. It also specifies the order of statements in the program.
Mainly controls structures are classified into the following three categories:
Branching (Selective Control Structure)
Looping (Repetitive Control Structure)
Jumping (Unconditional Control Structure)
Besides these, if there is no looping/branching/jumping then the structure is called sequential structure. In sequential control structure, program statements are executed in a sequence that is one after another.
POINTS TO REMEMBER
Control Structures are those programming constructs which control the flow of program statements execution in a program.
Decisions (Selective Control Structure)
In selective control structure, selection is made on the basis of condition. We have options to go when the given condition is true or false. The flow of program statement execution is totally directed by the result obtained from the checking condition. Hence, program statements using selective control structures are also called conditional Statements. This type of control structure is mainly used for decision making. It can mainly be categorized into two types.
Conditional Statement
Switch-Case Statement
Conditional Statement
It is the most common decision making control structure which controls the flow of program statement execution based on the condition checked. It can be used in different forms as:
if statement
if else statement
if else if statement (Multipath Conditional Statement/ if-else ladder)
nested if else statement
if statement
This is the simplest form of conditional statement in which statements are executed if the test expression (condition) is true. When the condition is false there is no option within this structure; in such a situation control must get out from the structure and statements outside this structure will be executed.
Syntax of if statement:
if(condition)
{
statements;
}
Braces are optional for only
one statement as shown below:
if(condition)
statement;
Flowchart of if statement:
Program 8 Write a program to demonstrate the use of if statement.
#include<stdio.h>
int main()
{
int n;
printf("\n Enter a number");
scanf("%d",&n);
if(n==2)
printf("\n Entered number is 2");
return 0;
}
if else statement
This is another form of selective control structure which can handle both expected as well as unexpected situations. In this control structure, statements written in the body part of if are executed if the condition is true otherwise statements written in the body part of else are executed. This is appropriate where we have to check only one condition.
Syntax of if else statement: if(condition) statement1; else statement2; In this case if the condition is true then statement1 is executed otherwise statement2 is executed. The else portion of the if-else statement is optional. | Flowchart of if else statement: |
Program 9 Write a program to which reads any two numbers and displays the largest one.
#include <stdio.h>
int main()
{
int num1,num2;
printf("Input two numbers");
scanf("%d%d",&num1,&num2);
if(num1>num2)
printf("%d is greater than %d",num1,num2);
else
printf("%d is greater than %d",num2,num1);
return 0;
}
if-else if statement
When we have two or more conditions to be checked in a series we can use if-else if Statement. It is also known as multiple conditional statement / multipath conditional statement / if-else ladder.
Syntax of if-else if statement: if(condition1) statement1; else if(condition2) statemen2; ........ else if(condition n-1) statement n-1; else statement n; In this statement, conditions are checked from the top of the ladder to downwards. As soon as the true condition is found, the statement associated with it are executed and the control is transferred to outside of the ladder skipping the remaining part of the ladder. When most of the conditions (condition-1 - condition n-1) are evaluated false then statements associated with the else part will be executed. | Flowchart of if-else-if statement: |
Program 10 Write a program to find the largest among three numbers given by the user.
#include<stdio.h>
int main()
{
int a,b,c;
printf("\n Enter any three numbers:");
scanf("%d%d%d",&a,&b,&c);
if(a>b && a>c)
printf("\n%d is the largest number.",a);
else if(b>a && b>c)
printf("\n%d is the largest number.",b);
else
printf("\n%d is the largest number.",c);
return 0;
}
Nested if else statement
An entire if else statement written within the body of if part or else part of another if else statement is called nested if else statement. It is used when a condition is to be checked inside another condition at a time in the same program to make a decision.
Syntax of if-else if statement: if(condition1) { if(condition2) statement1; else statement 2; } else statement 3; | Flowchart of if-else ladder statement: |
Percentage | Division |
p>=75 | Distinction |
p>=60 and <75 | First Division |
p>=45 and <60 | Second Division |
p>=35 and <45 | Third Division |
Otherwise | Failed |
[Hints: Pass marks and full marks for each subject are 35 and 100 respectively.]
#include<stdio.h>
int main()
{
int s1,s2,s3,s4,s5,total;
float p;
printf("Enter the marks of five subjects");
scanf("%d%d%d%d%d",&s1,&s2,&s3,&s4,&s5);
total=s1+s2+s3+s4+s5;
p=(float)total/5;
printf("Total marks=%d Percentage=%0.2f\n",total,p);
if(s1>=35 && s2>=35 && s3>=35 && s4>=35 && s5>=35)
{
if(p>=75)
printf("Distinction");
else if(p>=60 && p<75)
printf("First Division");
else if(p>=45 && p<60)
printf("Second Division");
else
printf("Third Division");
}
else
{
printf("Failed");
}
return 0;
}
Switch Case Statement
C switch case statement is a multipath decision making statement that allows selection and execution of a particular block of statements from several blocks of statement based upon the value of expression which is included within the switch statement and branches accordingly. The expression must be of an integral value (“integral” values are simply values that can be expressed as an integer, such as the value of a char).
The same task can be performed using an if-else ladder as well but as the number of alternatives increases, the selection process becomes more complex (more time consuming). The main difference between if else ladder and switch statement is - In if-else ladder selection of appropriate option is made in serial fashion whereas in switch-case statement it is done in parallel fashion. So switch statement is much faster than if-else ladder. The switch statement body consists of a series of case labels and an optional default label. The default label can appear only once and anywhere in the body of the switch statement.
Syntax | Syntax Description |
switch(expression) { case constant 1: statement 1; break; case constant 2: statement 2; break; -------------- statement n-1; break; default: statement n; } |
|
Program 12 Write a program which reads any two integer values from the user and calculates sum, difference and product using switch statement.
#include<stdio.h>
int main()
{
int num1,num2,sum,diff,pro,choice;
printf("Main-Menu\n");
printf("Press 1 for Sum\n");
printf("Press 2 for Difference\n");
printf("Press 3 for Product\n");
printf("Enter two numbers");
scanf("%d%d",&num1,&num2);
printf("Enter your choice");
scanf("%d",&choice);
switch(choice)
{
case 1:
sum=num1+num2;
printf("%d + %d =%d ",num1,num2,sum);
break;
case 2:
diff=num1-num2;
printf("%d - %d =%d ",num1,num2,diff);
break;
case 3:
pro=num1*num2;
printf("%d * %d =%d ",num1,num2,pro);
break;
default:
printf("Enter the numbers between 1 to 3");
}
return 0;
}
NEB Past Question
Write a program to display the name of the day in a week, depending on the number entered through the keyboard using the switch statements.
Answer:
#include<stdio.h>
int main()
{
int ch;
printf("Enter choice between 1 to 7");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Wrong choice");
}
return 0;
}
Netra Koirala
Computer Science Educator
Passionate computer science educator and author. Provides free study notes, practical guides, and tutorials for Class 9, 10, 11, 12, and B.Sc CSIT students in Nepal. Years of teaching experience in computer science fundamentals.
LinkedIn ProfileRelated Posts
Loading related posts…
Computer Science notes, tutorials, MCQs, and educational resources for Nepal students. Covering Class 9, SEE preparation, Class 11, Class 12, SLC, programming, DBMS, networking, HTML, JavaScript, PHP, OOP and more.
Featured Post
Grade 10 Computer Science: Specification Grid & Model Questions
Specification Grid & Model Questions of Computer Science | Grade 10 📚 Examination Resource Specification Grid & M...