control statements

Control statements

Control statements are used to control the flow of execution of the instructions written in a program.

Various control statements present in C :

  • Decision or selection control structure or branching.
  • Case control structure.
  • Looping control structure or repetition .
  • Jumping statements.

Decision control structure:
Group of instruction are executed depending upon the result of a decision .

Types of Decision control structure:

  • If statement
  • If-else statement
  • Nested if else statement
  • Else if ladder
  • Case control structure
  • Conditional operator (? : )

if statement

If statement : If statement is most simple decision control statement.
Syntax :

if(condition)
Statement-block

In if statement if the condition is true then the statement block will be executed otherwise statement block  will be ignored i.e condition is false.
Demonstration of  if statement

Program to check the entered number is greater than zero.
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
printf(“enter any number negative or positive”);
scanf(“%d”,&a);
if(a>0)
printf(“entered number is greater than zero”);
getch();
}

Output:
Enter any number: 5
Entered number is greater than zero

If – else statement
If else statement execute a single statement or a group of statements when the condition is true.
i.e if the condition is true then the part or statement block of if statement will be executed otherwise the statement block of else part will be executed if condition is false.

if (condition)
Statement block 1
else
Statement block 2

Nested if else statement
The construction of entire if else statement with in the body of either if statement or in the body of else statement then this kind of construction is known as nested if else statement.
General form of nested if else statement:

if(condition)
{
if(condition)
Statement block 1;
else
Statement block 2;
}
else
Statement block 3;
}
if(condition)
{
if(condition)
Statement block 1;
else
Statement block 2;
}
else
if(condition)
Statement block 3;
else
Statement block 4;
}

In nested if else statement  if the condition is true then the block of if statement will be executed and then the if else inside the if statement will be executed  ,  and the same will be for else statement.

Else if ladder statement
In this statement  each conditional expression is tested and if found true only its corresponding
Statement is executed.
If the condition of if statement is false then the condition of else if statement will be executed and this one is also false then the next, If no one condition is true then the block of else statement will be executed .

if (condition 2)
Statement block 1;
else if(condition 2)
Statement block 2;
:
else if (condtion n)
Statement block n;
else
Statement block m;

Case control statement :

Switch statement : is used to select one out of multiple alternatives present in a program.
General form of case control statement :

Switch (expression)
{
Case constant 1:
Statement block 1;
Break;
Case control 2:
Statement block 2;
Break;
:
Case control n:
Statement block n;
Break;
}

Expression: it must evaluate to an integer or character value.
Constant : it must be an int or chat compatible value.
Statement:  it is a simple statement.

Demonstration of switch statement.

#include<stdio.h>
#include<conio.h>
void main()
{
int day;
clrscr();
printf(enter day in number 1 to 7);
scanf(“%d”,&day);
switch(day){
case 1:
printf(“monday”);
break;
case 2:
printf(“Tuesday”);
break;
case 3:
printf(“wednesday”);
break;
case 4:
printf(“thursday”);
break;
case 5:
printf(“friday”);
break;
case 6:
printf(“saturday”);
break;
case 7:
printf(“sunday”);
break;
default :
printf(“wrong input”);
}
getch();
}

conditional operator:

C provides us a conditional operator (? : ) which can help the programmer in performing simple conditional operations.
eg. E1 ? E2 :E3
where E1, E2 and E3 are expression.
Demonstration of conditional operator .
Program to find the bigger of two given numbers.

#include<stdio.h>
#include<conio.h>
void main(){
int a, b, big;
clrscr();
printf(“\n enter two numbers”);
scanf(“%d%d”,&a, &b);
big = a > b ? a : b;
printf(“\n bigger number =%d”, big);
getch();
}