1. Control statement:
- All the instructions are executed sequentially by default, when no repetitions of some calculations are necessary.
- In some situation, we may have to change the execution order of statements based on condition or to repeat set of the statement until certain conditions are met.
Tyes:
- If statement
- If – else statement
- Nested if....else statement
- If ....else ladder
- Switch case
1. If statement:
It is used to check the condition and if true the condition then it executes.
Syntax:
if (condition)
{
True
statement;
}
if (condition)
{
True
statement;
}
Example program for if:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value:");
scanf("%d",&a);
if (a>=5)
{
printf("The
entered number %d is greater than 5",a);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value:");
scanf("%d",&a);
if (a>=5)
{
printf("The
entered number %d is greater than 5",a);
}
getch();
}
if condition:
False case:
2. If – else statement:
It is used to check both conditions i.e. true and
false.
It executes only one condition at a time.
Syntax:
if (condition)
{
True
statement
}
else
{
false
statement;
}
if (condition)
{
True
statement
}
else
{
false
statement;
}
Example program for if-else:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value:");
scanf("%d",&a);
if (a>5)
{
printf("The entered number %d is greater than 5",a);
}
else
{
printf("The entered number %d is less than 5",a);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("Enter a value:");
scanf("%d",&a);
if (a>5)
{
printf("The entered number %d is greater than 5",a);
}
else
{
printf("The entered number %d is less than 5",a);
}
getch();
}
if condition:
else condition:
3. Nested if....else statement:
Multiple if-else conditions are validating i.e.
within condition check first after that execute.
Syntax:
if(condition 1)
{
if(condition
2)
{
True
statement2;
}
else
{
False
statement2;
}
}
else
{
False
statement1;
}
if(condition 1)
{
if(condition
2)
{
True
statement2;
}
else
{
False
statement2;
}
}
else
{
False
statement1;
}
Example program for nested if...else:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a result:\n 1.pass\n 2.fail\n press 1 or 2\n");
scanf("%d",&a);
if (a==1)
{
printf("Enter total mark:\n");
scanf("%d",&b);
if(b>450)
{
printf("You can get first group i.e. maths group");
}
else
{
printf("You can get all group except first group");
}
}
else
{
printf("Passed studens only can join the 11th standard");
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf("Enter a result:\n 1.pass\n 2.fail\n press 1 or 2\n");
scanf("%d",&a);
if (a==1)
{
printf("Enter total mark:\n");
scanf("%d",&b);
if(b>450)
{
printf("You can get first group i.e. maths group");
}
else
{
printf("You can get all group except first group");
}
}
else
{
printf("Passed studens only can join the 11th standard");
}
getch();
}
if-if condition
if-if-else case:
else condition:
4. The if...else ladder:
It is similar to nested if but it checks
continually.
Syntax:
if(condition)
{
Statement1;
}
else if(condition 2)
{
Statement2;
}
else if(condition 3)
{
Statement3;
}
else
{
Statement4;
}
if(condition)
{
Statement1;
}
else if(condition 2)
{
Statement2;
}
else if(condition 3)
{
Statement3;
}
else
{
Statement4;
}
Example program for if...else ladder:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 values: \n");
scanf("%d %d %d",&a,&b,&c);
if( (a>b) &&(a>c))
{
printf("Biggest value %d is a",a);
}
else if(b>c)
{
printf("Biggest value %d is b",b);
}
else
{
printf("Biggest value %d is c",c);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
clrscr();
printf("Enter 3 values: \n");
scanf("%d %d %d",&a,&b,&c);
if( (a>b) &&(a>c))
{
printf("Biggest value %d is a",a);
}
else if(b>c)
{
printf("Biggest value %d is b",b);
}
else
{
printf("Biggest value %d is c",c);
}
getch();
}
if condition:
else condition:
5. Switch case:
It is used to execute a particular group of statement
.
Syntax:
switch(expression)
{
case
condition 1:
statement1;
break;
case
condition 2:
statement2;
break;
.........
.........
default
statements
break;
}
switch(expression)
{
case
condition 1:
statement1;
break;
case
condition 2:
statement2;
break;
.........
.........
default
statements
break;
}
Example program for switch case:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
clrscr();
printf("Enter a choice 1 or 2\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("You are chooses option 1 ");
break;
case 2:
printf("You are chooses option 2 ");
break;
default:
printf("You can only entered 1 or 2 choice ");
break;
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int choice;
clrscr();
printf("Enter a choice 1 or 2\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("You are chooses option 1 ");
break;
case 2:
printf("You are chooses option 2 ");
break;
default:
printf("You can only entered 1 or 2 choice ");
break;
}
getch();
}
Case 1:
Case 2:
Default case:
2. Branching and looping:
It is used to execute continually until the condition
becomes false.
Types:
- While
- Do while
- For
1. While:
It first checks condition then execute result until
become false.
Syntax:
while (condition)
{
Statements;
}
while (condition)
{
Statements;
}
Example program for while condition:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
while(i<=5)
{
sum=sum+i;
i++;
}
printf("The sum of numbers upto 5 is %d",sum);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
while(i<=5)
{
sum=sum+i;
i++;
}
printf("The sum of numbers upto 5 is %d",sum);
getch();
}
2. do while:
It is first to perform some operation after that check
the conditions.
It's opposite to the while condition.
Syntax:
do
{
Statements;
}
while (condition);
do
{
Statements;
}
while (condition);
Example program for do while condition:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
do
{
sum=sum+i;
i++;
}
while(i<=5);
printf("The sum of numbers upto 5 is %d",sum);
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,sum=0;
clrscr();
do
{
sum=sum+i;
i++;
}
while(i<=5);
printf("The sum of numbers upto 5 is %d",sum);
getch();
}
3. for loop:
It is used to execute a set of instructions repeatedly
until the condition becomes false.
Syntax:
for(initialization ;condition ;increment or
decrement)
{
Statement;
}
for(initialization ;condition ;increment or
decrement)
{
Statement;
}
Example program for for loop:
Output:
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d \n",i);
}
getch();
}
#include<stdio.h>
#include<conio.h>
void main()
{
int i;
clrscr();
for(i=1;i<=10;i++)
{
printf("%d \n",i);
}
getch();
}
No comments:
Post a Comment