AOP Unit 2 Chapter 2
AOP Unit 2 Chapter 2
Module B Compute
Sum a+b
Module C
Output sum
stop
stop
Decision making and branching-
The concept of decision making includes
Statements
Compound statements
Control flow
Control statements
The order of execution of the statements is called control flow. The statements that are used
to change the sequential execution of the instructions are called control statements. There
are two types of control statements – Selection construct and Iteration construct.
Types of control statements-
2.Selection construct(decision or conditional) – It is the ability of the program to allow the user
to decide on the execution of certain set of statements based on a condition. It allows to decide
as to which instruction is to be executed next. This construct helps us to jump from one part of
the program to another. There are five types of selection statements-
a. simple if - This is called one-way branch decision structure. This performs a set of
instructions if the condition is true and if it is false it skips those instructions and moves on
to the next set of statements. Multiple statements are enclosed within a pair of braces after
the ‘if’ condition. The general form is
If(condition)
{ If(condition)
StatementsA; Statement;
}
start
Test T
conditio
n?
F
statement
stop
Eg. Program to find whether the entered age is greater then 18 and to print eligible for
voting.
#include<stdio.h>
void main()
{
int n;
printf(“Enter the age”);
scanf(“%d”,&n);
if(n>18)
printf(“%d is eligible for voting”, n);
}
statementB statementA
stop
If(condition)
{
statementsA;
}
else
{
if(condition)
StatementB;
else
statementC;
} stop
F Test T
conditio
n?
Eg.: Program to find the greatest of 3 numbers using if-else-if.
#include<stdio.h>
void main()
{
int a,b,c;
printf(“Enter the 3 numbers”);
scanf(“%d %d %d”,&a,&b,&c);
if(a>b)&&(a>c) if(a>b)&&(a>c)
printf(“a is greater”); printf(“a is greater”);
else or else if(b>c)
{ printf(“b is greater”);
if(b>c) else
printf(“b is greater”); printf(“c is greater”);
else
printf(“c is greater”);
}
If(condition)
{
b.
Statement 1;
Statement 2;
}
If(condition)
c. Statement 1;
else
Statement 2;
;
d.
If(condition)
{
Statement 1;
Statement 2;
}
else
{
Statement 1;
Statement 2;
}
e. If(condition)
Statement 1;
else
{
if(condition)
Statement 2;
else
Statement 3;
}
If(condition)
{
f.
if(condition)
Statement 1;
else
{
Statement 2;
Statement 3;
}
}
else
Statement 4;
case constant2:
statementB;
break;
case constant3:
statementC;
break;
case constant4:
statementD;
break;
default :
default statement;
} start
Test
conditi
on?
Test
conditi
on?
case
constant1
statementA
case
constant2
statementB
case
constant3
statementC
case
constant4
statementD
default
Default
statement
Statement N
stop
Eg.: 1) Program to select the choice.
#include<stdio.h>
void main()
{
int choice;
printf(“Enter your choice(1,2,3 or 4)\n”);
scanf(“%d”,&choice);
switch(choice)
{
case 1: printf(“You have selected 1\n”);
break;
case 2: printf(“You have selected 2\n”);
break;
case 3: printf(“You have selected 3\n”);
break;
case 4: printf(“You have selected 4\n”);
break;
default: printf(“You have not selected 1,2,3 or 4\n”); } }
EXERCISE : 3) Program to select the MONTH and print the month name.
Note: 1.In if-else multiple statements should be enclosed within braces whereas in switch-case
it is not required.
2. If a statement does not belong to any case, the compiler will not report an error but the
statement will never be executed.
3. The break statement in switch takes the control outside the switch.
4. A float expression cannot be tested using a switch.
5. Cases can never have variable expressions i.e. case x+2: is wrong.
6. Multiple cases cannot use same expressions.
7. Switch-case works faster than if-else if there are more number of cases, as this structure
decides which case should be execute.0d.
highest= m1;
if(highest <m2)
highest = m2;
if(highest <m3)
highest = m3;
if(highest <m4)
highest = m4;
printf("\n highest of four subjects %d" , highest);
}
3. Iteration construct(looping) – The versatility of the programme lies in its ability to perform a set
of instructions repeatedly. This ability of the programming language to allow the user to repeat
the execution of a set of statements again and again until a requirement or condition is satisfied
is called as iteration or looping. There are three types of looping-
1. while statement
2. do-while statement
3. for statement
i. While loop- This structure is also called as pre-tested looping structure. It is an Entry-
Controlled loop. In this structure the testing the condition is done at the beginning of the
structure and a set of statements is executed again and again until the condition is true. Once
the condition becomes false the control is moved out of the loop and will continue executing the
next set of statements.The while() loop may not be executed at all.
The general form is
start
initialise loop counter;
while(condition)
{ initialise
statement1;
statement2;
increment loop counter;
} test F
statement3; condit
ion?
Statement3
E.g.: Program to print the numbers from 1 to 10. T
#include<stdio.h>
Statement1
void main() Statement2 stop
{
int i =0;
while(i<=10) increment
{
printf(“%d\t”,i);
i++;
}
}
In this example, first i is initialized to 0. Next the loop is executed 10 times , each time
incrementing the of i by 1. When i becomes 11, the condition is false and the loop is terminated.
Output:
Assignment Questions
1) /* Program to generate sum of natural numbers */
#include<stdio.h>
void main()
{
int sum=0;
int i = 1;
while(i<=10)
{
sum=sum+i;
i++;
}
printf("Total sum = %d ", sum);
}
2) /* Program to reverse an integer number */
#include<stdio.h>
#include<conio.h>
void main()
{
int n,rev,rem;
clrscr();
printf("\n Enter a Number : ");
scanf("%d",&n);
rev=0;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("\n Reversed number is = %d", rev);
return;
}
/* Program to check whether a given 5 digit integer is a palindrome */
#include<stdio.h>
#include<conio.h>
void main()
{
long n,rev,orig;
int rem;
clrscr();
printf("\n Enter a Number : ");
scanf("%ld",&orig);
rev=0;
n=orig;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
if(orig==rev)
printf("\n %ld is a palindrome",orig);
else
printf("\n %ld is not a palindrome",orig);
getch();
return;
}
Rec. 8 (b) : Programe to find the GCD of the given two numbers
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
void main()
{ int n,m,a,b,gcd;
printf("\n Enter two numbers");
scanf("%d%d", &a,&b);
n = a;
m = b;
while (n!=m)
{
if(n>m)
n = n-m;
else
m = m-n;
}
gcd = n;
printf("\n GCD of %d %d is = %d", a,b,gcd);
}
/*Program to find the the LCM two numbers*/
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
void main()
{ int n,m,a,b,lcm,gcd;
printf("\n Enter two numbers");
scanf("%d%d", &a,&b);
n = a;
m = b;
while (n!=m)
{ if(n>m)
n = n-m;
else m = m-n;
}
gcd = n;
lcm = (a*b) / gcd ;
printf("\n LCM of %d %d is = %d", a,b,lcm);
}
ii. do-while loop- This structure is also called as post-tested looping structure. It is an Exit-
controlled loop In this structure the testing the condition is done after executing the statements
within the loop at least for once. If the test condition evaluates to true then the statements inside
the loop are executed repeatedly until the condition becomes false. Even if the condition is false
the execution takes place at least once, before the control comes out of the loop.
} while(condition);
statement3; increment
test F
conditi
on?
Statement3
T
E.g.: Program to print the numbers from 1 to 10.
#include<stdio.h>
void main() stop
{
int i=11;
do
{
printf(“%d\t”,i);
i++;
} while(i<=10);
output
iii. for loop – It is also called as fixed looping construct. It is normally used when we
know exactly the number of times a particular set of statements is to be repeated in
the loop. The statement can either be used as an incrementing or decrementing loop
structure.
The general form is start
statement1 stop
statement2
Sequence of execution –
for(initialization;test counter;increment or decrement)
initialization – This statement is used to initialize the variable and will be executed for once i.e.
during the starting of for loop.
test counter – This can either be a logical statement or a relational statement or an arithmetic
statement that will result in true or false. It tells the program when to stop the execution of the body
of the for loop.
After initialization the condition is checked and if the condition is true, then the body of for
is executed.
Nested loops: If one looping statement is enclosed in another looping statement then such a
sequence of statements are called as nested loops.
Whenever nested loops are used the inner loop should be completely enclosed by the outer
loop. The inside loop is completely repeated for each repetition of the outside loop.
A for loop can be nested inside a for or a while loop and a while loop can be nested inside a
while or a for loop.
E.g.: for(outer=1;outer<=10;outer++)
{
for(inner=1;inner<=10;inner++)
printf(“*”);
}
printf(“\n”);
i = increment or decrement
----