0% found this document useful (0 votes)
26 views17 pages

AOP Unit 2 Chapter 2

Uploaded by

meghanar2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views17 pages

AOP Unit 2 Chapter 2

Uploaded by

meghanar2910
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Decision Making, Branching and Looping

Basic Programming Construct or Structure (Control instructions in C)-


Each program contains a simple or a complex logic to design the solution for the given problem.
The ability to design a proper logic can be easily done with the help of different programming
constructs.
The Control instructions in C enable us to specify the order in which the various instructions in
a program are to be executed by the computer. They determine the flow of control in a program. There
are three types of control instructions in C. They are
1.Sequence construct
2.Selection construct (decision or conditional)
3.Iteration construct (looping)
1.Sequence construct – The ability of the program to execute the statements or modules one after
the other from the beginning to the end is known as sequence construct. In this construct the
instructions are executed in the same order in which they appear in the program.This is the
simplest of all structures. The structure is as follows-
E.g.: To find the sum of two numbers.
start
start

Module A Input a,b

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);
}

b. if-else – It is used as a two-way branch decision structure. A set of statements are


executed if the condition is true and another set of statements are executed if the condition
is false. The general form is
start
if(condition) if(condition)
{ StatementA;
StatementsA; else
} statementB;
else F T
{ Test
statementsB; conditio
n?
}

statementB statementA

stop

Eg.: Program to find the biggest of two numbers.


#include<stdio.h>
void main()
{
int a,b;
printf(“Enter the two numbers”);
scanf(“%d %d”,&a,&b);
if(a>b)
printf(“a is greater”);
else
printf(“b is greater”);
}

c. if-else-if – This is an expansion of if-else structure also known as multiple-way


branching or nested if-else. There will be another set of if-else construct within the body
of the if statement or the else statement. The general form is

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”);
}

Forms of if - If(condition or expression)


a. Statement 1;

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;

Multiple-selection statement (Multi way decision-switch-case) – The control


statement that allows us to choose from a number of alternatives (choices) is called
a switch-case-default statement. It is used when the number of alternatives (choices)
to be checked increases. The complicated process of if-else-if structure is simplified
with the usage of switch statement. In this, the statement tests the value of the variable
or expression against a list of values and when a match is found the corresponding set
of statements is executed. The general form is
switch(test condition or Integer Expression)
{
case constant1:
statementA;
break;

case constant2:
statementB;
break;
case constant3:
statementC;
break;
case constant4:
statementD;
break;
default :
default statement;
} start

Test
conditi
on?

case case case case


constant1 constant2 constant3 constant4 default

statement statement statement statementD Default


OR
start

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”); } }

2) Program to select the day print the day name.


#include<stdio.h>
void main()
{ int day;
printf(“Enter your choice<=7\n”);
scanf(“%d”,&day);
switch(day)
{
case 1: printf(“MONDAY \n”);
break;
case 2: printf(“TUESDAY \n”);
break;
case 3: printf(“WEDNESDAY \n”);
break;
case 4: printf(“THURSDAY\n”);
break;
case 5: printf(“FRIDAY \n”);
break;
case 6: printf(“SATURDAY \n”);
break;
case 7: printf(“SUNDAY \n”);
break;

default: printf(“Wrong choice \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.

Difference between switch and if statements-


i. Switch can only test for equality whereas if can evaluate a relational or logical expression i.e.,
multiple conditions.
ii. Each switch case label must be a single value or cases never have variable expression for ex:
case a+3: not allowed , whereas if-else can handle ranges.
iii. Switch can handle an integer or character constant and cannot handle floating point tests whereas
if-else statements can handle integer, character or floating point tests.
iv. Switch statement selects its branches by testing the value of same variable against a set of
constants whereas if-else statement uses expressions that may contain variables and complex
expression.

Sequential Execution Statements Example :


Record Prgm 2 (a) A student has taken 4 examination papers valued for 100 marks.
Find the total marks and average marks.
#include<conio.h>
void main()
{
int m1,m2,m3,m4,total;
float average;
printf("enter the maths Marks = ");
scanf("%d",&m1);
printf("enter the Science Marks = ");
scanf("%d",&m2);
printf("enter the Social Marks = ");
scanf("%d",&m3);
printf("enter the English Marks = ");
scanf("%d",&m4);
total=m1+m2+m3+m4;
average=total/4;
printf("total marks =%d",total);
printf("\naverage marks of the student= %f",average);
}

Selection construct(decision or conditional) Statements Example


Record Prgm. 2 b) Find the highest Marks.
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<math.h>
void main()
{
int m1,m2,m3,m4, highest ;
printf("\n Enter the four subject Marks = “);
scanf("%d%d%d%d", &m1,&m2,&m3,&m4);

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) Write a C program to find whether given number is Odd or Even


#include<stdio.h>
void main()
{
int n;
printf(“Enter a number “);
scanf(“%d”,&n);
if(n%2==0)
printf(“Number is even”);
else
printf(“Number is odd”);
}
Iteration construct(looping)

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.

The general form is


start
initialise loop counter;
do
initialise
{ Body of
statement1; Loop
statement2; Statement1
increment loop counter; Statement2

} 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

/* Program to print multiplication table of a given number */


#include<stdio.h>
void main()
{
int i,n,prod;
printf(“\n Enter a number = “);
scanf(“%d”,&n);
i=1;
printf(“\n Multiplication Table \n \n”);
do
{
prod=i * n;
printf(“\n %d x %d = %d”,n,i,prod);
i++;
}
while(i<=10);
}

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

for(initialization;test counter;increment or decrement)


{ initialise
statement1;
statement2;
}
statement3; for(test
counter)

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.

increment or decrement - This is either used to increment or decrement the counter.


After checking the condition the increment or decrement takes place and the statements are
executed until the condition is false. Once the condition becomes false the control comes out of the
loop.

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”);

E.g.: Program to print numbers from 1 to 10.


#include<stdio.h>
void main()
{
int i;
for(i=0;i<=10;i++)
printf(“%d\n”,i); }

i = increment or decrement

----

You might also like