.
Control Structures
The statement which alter the flow of execution of program is known as control statement/structure. The instruction are executed in same order in the program such type of construct is known as sequential constrict.
The collection of various control structure forms a complete solution in terms of computer programs. Control structures can be categorized into three subsections.
- Sequence
- Selection (Decision Making)
- Jumping
Sequence
The sequence control structure is a form of control structure in a structural programming language. It is the set of computer instructions which follow one another and are to be executed unconditionally (not dependent on any program conditions). Instructions are put in a predefined sequence (just like a queue in cinema hall) and the next instruction is executed by CPU only after the execution of previous instruction. The following chart of sequential structure is shown bellow:
Fig: Flow chart of Sequential Structure
Let's take a simple example of "preparing a tea" problem. To prepare a cup of tea, the sequence will be as follows:
- Light the gas.
- Put a glass of water + milk in kettle.
- Put the kettle on gas.
- Put a spoon of tea leaf in kettle.
- Pour the mixture into glass after enough boiling.
- Mix the needed amount of sugar in the tea.
Selection (Decision Making)
Selection control structures, conditional statements are features if a programming language which perform different computations of actions depending on whether a programmer-specified Boolean condition evaluates to true of false.
It is fall under the category:
- Simple If statement
- If else statement
- Else if statement
- Nested if else statement
- Switch case
1. Simple if statement
It is powerful decision making statement which is used to control flow of execution. It is one way decision making statement.
Syntax:
If(Test condition)
{statement;}
Example
Write a program to enter your age and check you can vote ?
#include<conio.h>
#include<stdio.h>
void main( )
{ int age;
Chrscr( );
printf("Enter your age: ");
scanf("%d", & age);
if(age>=18)
{printf("you can vote. ");}
getch();
}
2. If else statement
It is an extension of the simple if statement. It has basically two possible action first is true condition and second is false condition.
Syntax
If(Test condition)
{statement;}
else
{statement;}
Example
Write a program to enter any number and check number is even or odd.
#include<stdio.h>
#include<conio.h>
void main( )
{ int x; clrscr( );
printf("Enter any number: ");
scanf("%d", &x);
if(x%2 == 0)
printf("Number is even");
else
printf("Number is odd");
getch( );
}
3. Else if statement
The else if statement is used when there are more then two possible action depending upon the outcome test.
Syntax
if(Test condition)
{statement;}
else if(Test condition)
{statement;}
else
{statement;}
Example
Write a program to enter your age and find you are child, young and old.
#include<stdio.h>
#include<condi.h>
void main( )
{int age;
clrscr( );
printf("Enter your age: ");
scanf("%d", &age);
if(age<=17)
printf("You are Child");
else if (age>17 && age<=60)
printf("You are Young");
else
Printf("You are Old");
getch( );
}
4. Nested if else statement
If an entire if else statement is written under either the body of an if statement or the else statement.
Syntax
if(condition)
{ if(condition)
{statement;}
else
{statement;} }
else
{ if(condition)
{statement;}
else
{statement;} }
Example
Write a program to enter three number and find largest among them.
#include<stdio.h>
#include<conio.h>
void main( )
{ int x,y,z;
clrscr( );
printf("Enter three number:");
scanf("%d%d%d", &x,&y,&z);
if(x>y)
{ if (x>y)
{
printf("largest is x: ");
}else{
printf("largest is y:");
}
}else{
if(y>z)
{
printf("largest is y:");
}else{
printf("largest is z:")
}}
getch( );
}
5. Switch case statement
The switch case statement is that which is used for making a design when there are multiple choice option.
Syntax
switch (expression)
{
case 1 : expression;
statement;
case 2 : expression;
statement;
break;
.....................
case n : expression;
statement;
break;
default : statment;
}
Example
Write a program to enter 1-7 days and display one day.
#include<stdio.h>
# include<conio.h>
void main( )
{ int day;
clrscr( );
printf("Enter 1 to 7 : ");
scanf("%d", &day);
switch (day)
{
case 1 : printf("Sunday");
break;
case 2 : printf("Monday");
break;
case 3 : printf("Tueday");
break;
case 4 : printf("wednesday");
break;
case 5 : printf("Thusday");
break;
case 6 : printf("Friday");
break;
case 7 : printf("Saturday");
break;
default : printf("invalid day");
}
getch( );
}
Iteration (Loop)
Iteration (loop) structure allows a sequence of program statements to be executed several times even though even though statement appears only one in the program. These are the computer instructions which are to be preformed repeatedly and conditionally, that is the loop statement are driven by the loop condition: Few example of iteration in programming language are listed below:
- For loop
- While loop
- Do while loop
1. FOR loop
It is a pretest loop where conditions are evaluated before the statement execution. If the given conditions are true then statements are executed other wise not.
Syntax
For(initialization; Test condition; increment/decrement)
{statement;}
Example
Write a program to enter any number and find factorial.
#include<stdio.h>
#include<conio.h>
void main( )
{ int n, i, f=1;
clrscr( );
printf("Enter any number:");
scanf("%d",&n);
for(i=1; i<=n; i=i+1)
{ f = f*i; }
printf("factorial = %d", f);
getch( );
}
2. WHILE loop
It is a pretest loop where conditions are evaluated before the statement execution. If the given conditions are true then statements are true then statements are executed other wise not. For leading decision (at the top of the loop) condition must be true to stay in the loop.
Syntax
Example
3. DO WHILE loop
The body of loop is existed first without testing condition and at the end of loop test condition is evaluated if condition is have program executed continuously else once executed & terminated.
Syntax
Initialization;
do
{statement;
increment/decrement;}
While (condition);
Example
Write a program sum of number which is executed divisible by 7 between 1 to 100.
#include<stdio.h>
#include<conio.h>
void main( )
{ int i, sum =0;
clrscr( );
for( i=1; i<100; i++)
{ if (i%7 == 0)
{ sum = sum + i;}
}
printf("sum = %d", sum);
getch( );
}
Comments
Post a Comment