In the term software or computer programming, it has a set of instructions (in simple or complex form) called a program. These instructions are also called statements, which occur sequentially or in either a conditional way or in an iterative way. To handle such types of statements some flow controls are required. These flow controls are called Control Statements.
The control flow statements of a language specify the order in which computations are performed. They determine the “Flow of Control” in a program. C programming language provides two types of control statements.
- Selection or Decision Control Statements: The decision and case control statements allow selective processing of a statement of a group of statements. These are also called as Conditional Statements.
- Repetition or Loop Control Statements: The Loop control statement executes a group of statements repeatedly till a condition is satisfied.
In other words, the control statements are used to control the cursor in a program according to the condition or according to the requirement in a loop. There are mainly three types of control statements or flow controls. These are illustrated as below:
- Branching
- Looping
- Jumping
Branching
if statement
The if statement is a powerful decision-making statement that can handle a single condition or group of statements. These have either true or false actions. When only one condition occurs in a statement, then a simple if statement is used having one block.
/*Any Number is input through the keyboard. write a If program.*/ #include <stdio.h> #include <conio.h> void main() { int n; n=1; clrscr(); //clrscr() is the function of #include header file which will clear previous output of program printf("Enter the Number"); scanf("%d",&n); if(n>0) { printf("It is If Statement"); } getch(); }
if-else statement
This statement also has a single condition with two different blocks. One is true block and other is false block.
/*Any Number is input through the keyboard. write a program to find out whether It is an Odd Number or Even Number.*/ #include <stdio.h> #include <conio.h> void main() { int n; n=1; clrscr(); printf("Enter the Number"); scanf("%d",&n); if(n%2==0) { printf("This is Even Number"); } else { printf("This is Odd Number"); } getch(); }
Output is as follows:
Enter the Number 4 This is Even Number
nested if statement
When an if statement occurs within another if statement, then such type is called nested if statement.
/*If the ages of Ram, sham, and Ajay are input through the keyboard, write a program to determine the youngest of the three*/ #include <stdio.h> #include <conio.h> void main() { int ram,sham,ajay; clrscr(); printf("Enter the Three Ages of Ram,Sham and Ajay\n"); scanf("%d%d%d",&ram,&sham,&ajay); if(ram
Output is as follows:
Enter the three Ages of Ram,Sham and Ajay 14 17 19 Ram is Youngest
Ladder if or else if statement
When in a complex problem, number of conditions arise in a sequence, then we can use Ladder-if or else if statement to solve the problem in a simple manner.
The marks obtained by a student in 5 different subjects are input through the keyboard. The student gets a division as per the following rules:
=> Percentage above or equal to 60 - First Division
=> Percentage between 50 and 59 - Second Division
=> Percentage between 40 and 49 - Third Division
=> Percentage less than 40 - Fail
Method 1
/*Write a program to calculate the division obtained by the student. There are two ways in which we can write a program for this example. These methods are given below: */ //Method-1 #include #include void main() { int eng,math,com,sci,ss,total; float per; clrscr(); printf("Enter Five Subjects Marks\n"); scanf("%d%d%d%d%d",&eng,&math,&com,&sci,&ss); total = eng + math + com + sci + ss ; printf("Toal Marks : %d",total); per = total * 100.00 / 500; printf("\n Percentage : %f", per); if(per >= 60) { printf("\n 1st Division"); } else if(per >= 50) { printf("\n 2nd Division"); } else if(per >= 40) { printf("\n 3rd Division"); } else { printf("\n Sorry Fail"); } getch(); }
Method 2
//Method-2 #include #include void main() { int eng,math,com,sci,ss,total; float per; clrscr(); printf("Enter Five Subjects Marks\n"); scanf("%d%d%d%d%d",&eng,&math,&com,&sci,&ss); total = eng + math + com + sci + ss ; printf("Toal Marks : %d",total); per = total * 100.00 / 500; printf("\n Percentage : %f", per); if(per >= 60) { printf("\n 1st Division"); } if(per >= 50 && per = 40 && per
switch statement
When a number of conditions (multiple conditions) occurs in a problem and it is very difficult to solve such type of complex problem with the help of a ladder if statement, then there is a need for such type of statement which should have different alternatives or different cases to solve the problem in a simple and easy way. For this purpose switch statement is used.
/*WAP to print the four-days of week from monday to thrusday which works upon the choice as S,M,T,H using switch case*/ #include <stdio.h> #include <conio.h> void main() { char n; clrscr(); printf("Enter the Choice from Four Days...\n") printf("S = Sunday \n") printf("M = Monday \n") printf("T = Tuesday \n") printf("H = Thursday \n\n") scanf("%c",&n); switch(n) { case 'S': printf("Sunday"); break; case 'M': printf("Monday"); break; case 'T': printf("Tuesday"); break; case 'H': printf("Thursday"); break; default: printf("Out of Choice"); break; } getch(); }
Output is as follows:
Enter the Choice from Four Days... S = Sunday M = Monday T = Tuesday H = Thursday S Sunday
Conditional Control Statement
This statement is based on a conditional operator. This statement solves the problem's condition in a single line and is a fast executable operation. For this purpose, we can take a combination of ? and :
/*The easiest way to use conditional control statement*/ #include <stdio.h> #include <conio.h> void main() { int n; clrscr(); n%4==0 ? printf("Leap Year") : printf("Not Leap Year"); //OR //printf(n%4==0 ? "Leap Year" : "Not Leap Year"); getch(); }