CONTROL STATEMENTS frAluF
CONTROL STATEMENTS frAluF
if(condition) if(condition)
{ {
Statement 1; Statement 1;
} Statement 2;
----------------
Statement 3;
}
Examples:-
Program to check whether accepted number is greater than 20 or not.
#include<stdio.h>
#include<conio.h>
main()
{
int a;
printf(“\n Enter the number:”);
scanf(“%d”, &a);
if(a>20)
printf(“\n The number is greater than 20”);
getch();
}
Output:-
Enter the number: 40
The number is greater than 20
Program to check equivalence of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int a,b;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &a, &b);
if(a==b)
printf(“\n Two numbers are equal”);
}
Output:-
Enter two numbers: 10 10
Two numbers are equal
Program to find largest of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
if(num1> num2)
printf(“\n %d is largest number”, num1);
if(num2> num1)
printf(“\n %d is largest number”, num2);
}
Output:-
1) Enter two numbers: 10 20
20 is largest number
2) Enter two numbers: 30 15
30 is largest number
If Else Statement
• if else is a two-way branching statement.
• When the condition of if is true, the statements within the if block are executed
otherwise the else block are executed.
• Syntax:-
if(condition)
{
True-block statement(s); if block
}
else
{
False-block statement(s); else block
}
Flowchart
Different Forms of if-else statement:-
if (condition) if (condition) if (condition) if (condition)
statement 1; { statement 1; {
else statement 1; else statement 1;
statement 2; statement 2; { statement 2;
} statement 2; }
else statement 3; else
statement 3; } {
statement 3;
statement 4;
}
Examples:-
Program to find largest of two numbers.
#include<stdio.h>
#include<conio.h>
main()
{
int num1,num2;
printf(“\n Enter two numbers: “);
scanf(“%d %d”, &num1, &num2);
if(num1> num2)
printf(“\n %d is largest number”, num1);
else
printf(“\n %d is largest number”, num2);
}
Output:-
1) Enter two numbers: 10 20
20 is largest number
Program to check whether a number is even or odd.
#include<stdio.h>
#include<conio.h>
main()
{
int num;
printf(“\n Enter a number:”);
scanf(“%d”, &num);
if(num % 2==0)
printf(“\n Number is Even”);
else
printf(“\n Number is Odd”);
}
Output:-
Enter a number: 5
Number is Odd
Nesting of If Else Statements
• The else if ladder is a multi way decision maker which contains two or
more else-if , from which one block is executed.
Syntax:-
if ( condition 1)
{
statement 1;
}
else If ( condition 2)
{
statement 2;
}
else If ( condition 2)
{
statement 3;
}
----------------
----------------
else if ( condition n-1)
{
statement n-1;
}
else
}
statement n;
----------------
}
Flowchart:-
Example:-
#include<stdio.h>
#include<conio.h>
void main()
{
float percentage;
printf(“\n Enter the percentage: “);
scanf(“%f”, &percentage);
if(percent >= 75)
printf(“\n Distinction”);
else if(percent >= 60)
printf(“\n First Class”);
else if(percent >= 50)
printf(“\n Second class”);
else if(percent >= 40)
printf(“\n Third Class”);
else
printf(“\n Fail”);
}
Output:-
1) Enter the percentage: 44
Third Class
2) Enter the percentage: 90
Distinction
Switch Statement:-
• The switch statement provides multi-way branching.
• The switch allows the user to select any one of the alternatives
depending on the value of the expression.
• The expression present within a parenthesis of switch statement must
be of the type int or char.
• Based on the expression the control is transferred to a particular case
label and the statement associated with that label are executed.
• The case label must be int or char type.
• The case default is executed only when none of the cases are true to
the value of the expression present in switch construct.
Syntax:-
switch(expression)
{
case label 1: statement 1;
………………
break;
case label 2: statement 2;
………………
break;
case label 3: statement 3;
………………
break;
………………………………………..
………………………………………..
case label n: statement n;
………………
break;
default: default statements;
…………………………
}
statement-x;
Flowchart:-
Examples:-
1) int a=2;
switch(a)
{
case 1: printf(“Number is 1”);
break;
case 2: printf(“Number is 2”);
break;
case 3: printf(“Number is 3”);
break;
default: printf(“Invalid number”);
}
Program to check whether the input character is vowel or not.
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;
printf(“\n Enter a character:”);
scanf(“%c”, &ch);
switch(ch)
{
case ‘A’:
case ‘a’:
case ‘E’:
case ‘e’:
case ‘I’:
case ‘i’:
case ‘O’:
case ‘o’:
case ‘U’:
case ‘u’: printf(“%c is vowel”, ch);
break;
default: printf(“%c is not a vowel”, ch);
}
}
Program to perform the operations Add, Subtract, Multiplication, Division, Largest of two
numbers.
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b, c, ch;
printf(“\n 1. Addition”);
printf(“\n 2. Subtraction”);
printf(“\n 3. Multiplication”);
printf(“\n 4. Division”);
printf(“\n 5. Largest of two numbers”);
printf(“Enter your choice:”);
scanf(“%d”, &ch);
if(ch <= 5 && ch > 0)
{
printf(“ Enter two numbers:”);
scanf(“%d %d”, &a, &b);
}
switch(ch)
{
case 1: c = a+b;
printf(“\n Addition: %d”, c);
break;
case 2: c = a-b;
printf(“\n Subtraction: %d”, c);
break;
case 3: c = a*b;
printf(“\n Multiplication: %d”, c);
break;
case 4: c = a/b;
printf(“\n Division: %d”, c);
break;
case 5: if(a>b)
printf(“\n a is larger”);
else if (b> a)
printf(“\n b is larger”);
break;
default: printf(“Invalid Choice”);
}
Output:-
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Largest of two numbers
1) Enter your choice: 5
Enter two number : 8 9
b is larger
2) Enter your choice: 4
Enter two number : 20 10
Division : 2