lets see one example. suppose there are two numbers a & b and we want to find out greater number. Haw can we write code(program) for it? for this purpose we use control structure in C i.e. if else here the logic is that if a is grater than b then print a is greater else print b is greater. lets see how we will code this. Syntax of if else :
if (condition) { statements ; } else { statements ; }
Program to find greater number
#include<stdio.h> #include<conio.h> void main() { int a,b; clrscr(); printf("\nEnter the value for a"); scanf("%d",&a); printf("\nEnter the value for b"); scanf("%d",&b); if(a>b) { printf("\na is greater"); } else { printf("\nb is greater"); } getch(); }
Note : if there is only one statement inside if then it is not mandatory to keep braces {}.like in above program u could write
if(a>b) printf("\na is greater"); else printf("\nb is greater");
but if there are more than one statements inside if or else you have to keep those inside { } braces.
Nested if-else
we can write an entire if-else block within either the body of the if statement or the body of an else statement. This is called ‘nesting ‘of ifs. This is shown in the following program.
void main( ) { int a; printf ( "Enter or 2 " ) ; scanf ( "%d", &a ) ; if ( a == 1 ) printf ( "\n Inside first if" ) ; else { if ( a == 2 ) printf ( "\nInside first else under second if" ) ; else printf ( "\nInside first else under second else" ) ; } }
If with multiple else statements
syntax :
if (condition) { statements; } else if { statements; } else if { statements; } else { statements; }
Lets see an example. Suppose in an exam if student gets marks in between 40-49 including both he is pass. If he gets marks in between 50 -59 including both he has second class. If he gets marks in between 60 -74 including both he has first class.if he gets marks 75 or above he has distinction.lets see how we will code this.
#include<stdio.h> #include<conio.h> void main() { int marks; clrscr(); printf("\nEnter students marks"); scanf("%d",&marks); if(marks<40) printf("\nfail"); else if (marks >=40 && marks<50) printf("\npass"); else if (marks >=50 && marks>60) printf("\nSecond class"); else if (marks >=60 && marks<75) printf("\nFirst class"); else printf("\nDistinction"); getch(); }
The switch case
C/C++ has a built-in multiple-branch selection statement, called switch, which successively tests the value of an expression against a list of integer or character constant. When a match is found, the statements associated with that constant are executed.
Syntax :
switch (expression) { case constant1 :statement sequence break; case constant2 :statement sequence break; case constant3 :statement sequence break; . . . default statement sequence }
Lets see an example of switch case. following is a program that can do multiple operations like addition, subtraction, multiplication & division.
void main() { int a,b,c,ans; clrscr(); printf("\n Enter values for a & b"); scanf("%d%d",&a,&b); printf("\n1. Add"); printf("\n2. substract"); printf("\n3. Multiply"); printf("\n4. Divide"); printf("\nEnter your choice"); scanf("%d",&c); switch(c) { case 1 : ans=a+b; printf("\nAddition is : %d" ,ans); break; case 2 : ans=a-b; printf("\nSubstraction is : %d" ,ans); break; case 3 : ans=a*b; printf("\nMultiplication is : %d" ,ans); break; case 4 : ans=a/b; printf("\nDivision is : %d" ,ans); break; default : printf("\nWrong choice"); break; } getch(); }