Control Structures
Control Structures
Program control
• Specify order in which statements are to executed
• A control structure is a control statement and the statements whose execution it controls
• The following are the three control structures used to control program execution: sequence, selection and
iteration
I) SEQUENCE
Here, each statement in the program is executed once and in the same order in which they are listed.
II) SELECTION
It occurs when the program is able to evaluate conditions and select alternative courses of action. Conditions are
usually inform of expression. There are two ways in which selection may be made: -
i) if statements
ii) switch statements.
i) if statements
General format for if a statement is as follows:
if (Expression)
Statement;
else
Statement;
With else being option – single selection. If there are more than one statements in the if or else, then braces
must be used to define the scope of the statement.
Example
Write a C program that inputs three subjects marks for a student and computes the average mark. Program
displays pass if the student average mark is more than or equal to 50 and fail when the student average mark is
below 50.
# include <stdio.h>
int main ( )
{
float mark1, mark2, mark3, average;
printf("Enter the three subject marks\n");
scanf("%f%f%f", &mark1, &mark2, &mark3);
average = ( mark1+mark2+mark3)/3;
if (average > = 50)
printf ("pass\");
else
printf ("fail\n");
return 0;
}
if (Expression)
{
Statement(s);
}
else if (Expression)
{
Statement(s) ;
}
………..
…………
else
{
Statement(s);
}
Example 1
Write a program that inputs three different integer numbers and output the largest
# include <stdio.h>
int main ( )
{
int number1, number2, number3;
printf ("Enter three integer numbers\n");
scanf ("%d%d%", &number1, &number2 ,&number3);
if (number1 > number2 && number1 > number3)
{
printf("The largest number is %d\n", number1);
}
else if ( number2 > number1 && number2 > number3)
{
printf("The largest number is %d\n", number2);
}
else
{
printf("The largest number is %d\n", number3);
}
return 0;
}
Example
Write a program that simulates a simple calculation. The program reads two numbers and a character (operator).
If the character is “+ ” then sum is displayed. If it is “–“ then difference is displayed. If it is “*” then product is
displayed. If it is “/” then quotient is displayed. Use a switch statement.
# include <stdio.h>
int main ()
{
int number1, number2, answer1;
float answer2;
char op;
Example
Write a program that simulates a simple calculation. The program reads two numbers and a character (operator).
If the character is “+ ” then sum is displayed. If it is “–“ then difference is displayed. If it is “*” then product is
displayed. If it is “/” then quotient is displayed. Use a switch statement.
# include <stdio.h>
int main ()
{
int number1, number2, answer1, choice;
float answer2;
/* menu of operation*/
printf("1 Multiplication \n");
printf("2 Division \n");
printf("3 Addition \n");
printf("4 Subtraction \n");
/*end of menu*/
printf("Enter two integer numbers \n");
scanf("%d%d", &number1, &number2);
printf("Enter your choice\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
answer1 = number1 * number2;
printf("%d*%d = %d\n", number1, number2, answer1);
break;
case 2:
answer2 = (float) number1/number2;
printf("%d/%d = %f\n", number1, number2, answer2);
break;
case 3:
answer1 = number1 + number2;
printf("%d+%d=%d\n", number1, number2, answer1);
break;
case 4:
default:
printf("Invalid operators entry! \n");
}
return 0;
}
III) ITERATION
This occurs when the program is able to repeat a given task as long as the condition is true and in C is achieved
in three different ways:
i) The for loop
ii) The while loop
iii) The do…while loop
In each case, there’s a condition, which allows the loop to terminate
Syntax
for ( initialise_expr; test_expr; increment_expr )
{
statement(s);
}
Example
Write a program that generates and displays all integers numbers between 1 and 20
# include <stdio.h>
int main ( )
{
int counter;
for (counter = 1; counter <=20; counter++)
{
printf("%d\n", counter);
} /* end of loop */
return 0;
} /* end of main */
Example
Write a program that displays both the lower case and upper case alphabetical letters using the following format
Lowercase Uppercase
a A
b B
c C
# include <stdio.h>
int main ( )
{
char letter1, letter2;
printf("Lowcase\t Uppercase\n");
for(letter1 = 'a', letter2 = 'A'; letter1<='z', letter2<='Z';
letter++, letter2++)
printf("%c\t\t %c\n", letter1,letter2);
return 0;
}
Example
Write a program that will generate and compute the sum and average of numbers 1 to 50 using a for loop. The
program displays both the sum and average on the screen.
# include <stduio.h>
int main ( )
{
int number, sum = 0;
float average;
for(number = 1; number <= 50; ++number)
{
sum +=number;
}
average=(float) sum/50;
printf("sum = %d, average = %0.2f\n", sum, average);
return 0;
}
Example
Write a program that generates and displays a 10 by 10 multiplication table
1 2 3 4…………10
2 4 6 8……………
3………………….
……
……
10……………..100
# include <stdio.h>
int main ( )
{
int i,j,table;
for(i=1; i<=10; i++)
{
for(j=1; j<=10; j++)
{
table = i*j;
printf ("%d\t", table);
Example
Write a program that generates and displays numbers 1 to10 using the following control structures
i) while loop
ii) do while…loop
Solution
Example
Write a program that displays the following temperature conversion chart on the screen as follows below.
Hint: c = 5.0/9.0 * (f – 2)
C = degrees in Celsius
F = degree in Fahrenheit
Fahrenheit Celsius
*****************************
0 -17.68
20 -6.67
40 4.44
… …
… …
300 148.89
# include <stdio.h>
int main ( )
{
int fahrenheit = 0;
float celsius;
printf("fahrenheit \t \t celsius\n");
printf("*********************\n");
while(fahrenheit < = 300)
{
celsius = 5.0/9.0 * (fahrenheit - 32);
printf("% d \t \t % f\n", fahrenheit, celsius);
farhenheit +=20;
} /* end of while */
return 0;
} /* end of main */
Example
Write a program generates and sums all even numbers and product of all odd numbers between 20 and 50 using
a looping control structure.
# include <stdio.h>
int main ( )
Example
Write a program in C that uses a loop construct to compute the squares of all even numbers that fall between 26
and 97 (inclusive). The square of a number num is computed as square=num * num. The program then
outputs the results in the format shown below
Number Square
26 676
27 784
… …
… …
#include <stdio.h>
int main ( )
{
int number, square;
number = 26;
while(number <= 97)
{
if(num%2 ==0)
{
square = number * number;
printf ("%d\t\t%d/n", number, square);
}
number ++;
}
return 0;
}
Exercises
1. Rewrite the following segment of code using if… else statement
switch (x)
{
case 1:
case 2:
case 3:
printf("\n x is greater than y");
break;
default:
printf("\n The value %d is out of range", x);
break;
{
write a C program that prompts the user for his or her points (4, 3, 2 or 1) and then displays the order of
the merit.
Hint (make use of the switch case statements)
3. Write a complete C program to calculate either the area of triangle, rectangle or circle. The program should
have a decision making statement to decide: -
i) If the figure is a triangle then ask for base/height values to input
ii) If the figure is a rectangle, then ask for length & breadth values to input
iii) If the figure is a circle then asks for radius value to input the program should display output on
the screen.
4. Write a C program that inputs a year and then it determines whether it is a leap year or not. Your program
should display an appropriate message.
Hint:
A year is a leap year if it is divisible by 400 and not by 100 or it is divisible by 4