Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Program control
• Specify order in which statements are to executed
• A control structure is a control statement and the statements whose execution it controls
• Three structures control program execution:
i) Sequence
ii) Selection
iii) 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
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;
}
General format
if (Expression)
{
statement 1;
……….
}
else if (Expression)
{
statement ;
……….
}
………..
…………
else
{
statement 1;
……….
}
Example
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;
}
A switch case structure works with a single test expression that is evaluated once, at the top of the structure.
The value obtained is then compared with the values for each case in the structure. If there is a match, it
executes the block of statements associated with that case. The following is the basic format.
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
… …
… …
z Z
# include <stdio.h>
int main ( )
{
char letter1, letter2;
printf("Lowcase\t Uppercase\n");
for(letter1 = ‘a’, letter2 = ‘A’; letter1<=’z’, letter2<=’Z’;
letter++, letter2++)
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);
}
printf("\n");
}
return 0;
}
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 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;
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;
printf("Enter the two integer numbers\n")
scanf("%d%d", &number, &number2);
printf(" Enter the arithmetic operator\n");
scanf("%c", &op);
/* make use of switch statements to make decisions */
switch(op)
{
case ’*’:
answer1= number1*number2;
printf"%d*%d = %d/n", number1, number2, answer1);
break;
case ‘/’:
answer2 = (float)number1/ number2;
printf("%d/%d = %d/n", number1, number2, answer2);
break;
case ‘+’:
answer1 = number1+ number2:
printf (“%d + %d = %d\n”, number1, number2, answer1;)
break;
case ‘-‘:
answer1 = number1 – number2;
printf("%d -%d = %d\n", number1, number2, answer1);
break;
default:
printf("Invalid operator!\n");
} /* end of switch */
retun 0;
} /* end of main function */
/* menu of operation*/
printf(“1 Multiplication \n”);
printf(“2 Division \n”);
printf(“3 Addition \n”);
printf(“4 Subtraction \n”);
/*end of menu*/
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, answer2);
break;
case 4:
answer1 = number1 – number2
printf("%d-%d=%d\n", number1, number2, answer1);
break;
default:
printf("Invalid operators entry! \n");
}
return 0;
}
Example
#include <stdio.h>
#include <stdlib.h>
int main ( )
{
int counter;
counter = 1;
while(counter <=10)
{
printf("% d\n", counter);
The continue keyword is used to loop back to the while, do ... while and for control.
Example
#include <stdio.h>
#include <stdlib.h>
int main ( )
{
int counter;
counter = 0;
while(counter <10)
{
counter++;
if (counter==5)
continue;
printf("% d\n", counter);
} /* end of while loop*/
return 0;
} /* end of main function*/
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)
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