You are on page 1of 24

Control Flow Structures

Dr. S. Usha Kiruthika


Dept. of CSE
NITT
Bohm-Jacopini Theorem
• A class of flowcharts can compute any computable function if it
combines subprograms in only three specific ways. These are
• Executing one subprogram, and then another subprogram (sequence)
• Executing one of two subprograms according to the value of
a Boolean expression (selection)
• Repeatedly executing a subprogram as long as a Boolean expression is true
(iteration)
Statements and Blocks
• An expression such as x = 0 or i++ or printf (...) becomes a statement when
it is followed by a semicolon, as in
x = 0;
i++;
printf(“hello”);
• Braces { and } are used to group declarations and statements together into
a compound statement, or block
{
i = i * 5;
i = i + 2;
}
Control Flow Statements
• if .. else statement
Syntax:
if (expression) {
statements1;
}
else {
statements2;
} Eg.
if (x > 0){
a = 1;
}
else {
a = 0;
}
Example Program – if statement
#include<stdio.h>

int main()
{
int honours;
printf(“Enter 1 if you have Honours: ”);
scanf(“%d”,&honours);

printf(“You passed the exam ”);

if (honours == 1) {
printf(“with Honours”);
}
return 0;
}
Example Program – if..else
#include<stdio.h>

int main()
{
int age;
printf(“Enter age: ”);
scanf(“%d”,&age);

if (age >= 18 )
printf(“Eligible to vote”);

else {
printf(“Not eligible to vote”);
}
return 0;
}
Example Program – if..else if..else
#include<stdio.h>
int main()
{
int a,b;
printf(“Enter 2 numbers: ”);
scanf(“%d%d”,&a,&b);
if (a > b)
printf(“First number is larger”);
else if (a < b)
printf(“Second number is larger”);
else
printf(“The numbers are equal”);
return 0;
}
Example Nested if Code
if (var1 != var2)
{
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
Exercise
• Given the length of three sides of a triangle, write a C program to find
if a triangle is equilateral, isosceles or scalene triangle.
• Write a C program to calculate the commission payable and print it
along with the D.D amount. Commission rates are as follows:
Up to Rs.5,000 - Rs.25.
Rs.5,000 to Rs.10,000 - Rs.50.
Rs.10,000 to Rs.1 lakh - Rs.5 per Rs.1,000 (Min. Rs.60)
Above Rs.1 lakh - Rs.4 per Rs.1,000 (Min. Rs.600; Max. Rs.2,000)
Conditional Operator
• Also called ternary operator (3 operands)
• Allows coding of simple if..else construct
expr1 ? expr2 : expr3
eg.
(age > 18) ? printf(“Eligible”) : printf(“Not eligible”);

max = (a > b) ? a : b;
Iterative Structures
Iterative Structures
• Repeat a set of statements until a condition becomes false
• Type:
• Logic Controlled loops (while, do..while)
• Counter Controlled loops (for)
While loop
• Syntax:
while (condition)
{
statements;
}
• The statements are repeated while the condition is true
• Loop terminates when the condition becomes false
Program to print all perfect Squares below
100
#include <stdio.h>

int main(void)
{
int i=1;
printf("The perfect squares below 100 are:\n");
while(i*i<100)
{
printf("%d",i*i);
i++;
}
return 0;
}

The perfect squares below 100 are: 1 4 9 16 25 36 49 64 81


Do..while loop
• Syntax:
do {
statements;
} while(condition);

• Statements are executed at least once


• Then they are repeated while the condition is true
• Loop terminates when condition becomes false
Example program – do..while
do {
printf("Enter a number: ");
scanf("%d", &number);
sum += number;
}
while(number != 0);

printf("Sum = %d",sum);
Differences between while and do..while
while do..while
 Pre-tested or top-tested  Post-tested or bottom tested

 Loop never executes if condition is  Loop executes at least once


false during the first check irrespective of whether condition is
true or false
For Loop
• Syntax:
for(expression1;expression2;expression3)
{
statements;
}
expression1 – initializes counter
expression2 – checks condition
expression3 – increments or decrements counter
Example Program using for loop
#include <stdio.h>

int main(){
int fact=1,num;
int i;
printf("Enter a number: ");
scanf("%d",&num);
for(i=1;i<=num;i++){
fact = fact*i;
}
printf("Factorial=%d\n",fact);
return 0;
}
Examples
sum=1; index=9; for(i=0;i<=25;i++)
do{ {
index = index-1; i++;
sum=2*sum; i++;
} while(index>9) }

sum=0; i=20;
for(i=0;i<5;i++) for(;i;){
sum=sum+i; i--;
printf(“%d ”,sum); printf(“%d ”,i);
}
Practice Programs
• Write a program to print out the integers 1 to 500, five per line.
• Write a program to print the largest of n given numbers.
• Write a program to print the sum of first 50 even natural numbers
• Write a program to print all perfect squares < 1000
• Write a program to print the following sequence until last value is less
than 1000
1, 3, 7, 15, 31, …
Nested Loops
• A loop construct appears within another loop construct
• The loop variables must not overlap
eg. for(i=10;i>=1;i--)
{
for(j=i;j>=1;j--)
{
printf(“%d”,i);
}
}
Caveat

You might also like