Professional Documents
Culture Documents
Lecture III
Lecture III
Lecture III
Structured Program in C
Introduction
9
if statements
if else statements
nested if statements
10
The if Selection Statement
Selection structures are used to choose among alternative courses
of action. For example, suppose the passing grade on an exam is
60. The pseudocode statement
If student’s grade is greater than or equal to 60
Print “Passed”
determines if the condition “student’s grade is greater
than or equal to 60” is true or false.If the condition is
true, then “Passed” is printed, and the next pseudocode
statement in order is “performed” (remember that
pseudocode is not a real programming language). If
the condition is false, the printing is ignored
the if statement
12
Example
int main()
{
int m=40,
int n=40;
if (m == n)
{
printf("m and n are equal");
}
}
13
The if else statement
14
Example
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m == n)
{
printf("m and n are equal");
}
else
{
printf("m and n are not equal");
}
}
15
The nested if
16
Example
#include <stdio.h>
int main()
{
int m=40,n=20;
if (m>n) {
printf("m is greater than n");
}
else if(m<n) {
printf("m is less than n");
}
else {
printf("m is equal to n");
}}
17
Exercise 1: what would be the output of the
following programs
(a) main( )
{
int x = 10, y = 20 ;
if ( x == y ) ;
printf ( "\n%d %d", x, y ) ;
}
(b) main( )
{
int x = 3, y = 5 ;
if ( x == 3 )
printf ( "\n%d", x ) ;
else ;
printf ( "\n%d", y ) ;
}
18
(c) main( )
{
int x = 3 ;
float y = 3.0 ;
if ( x == y )
printf ( "\nx and y are equal" ) ;
else
printf ( "\nx and y are not equal" ) ;
}
19
(d)main( )
{
int i = 65 ;
char j = ‘A’ ;
if ( i == j )
printf ( “C is WOW” ) ;
else
printf( "C is a headache" ) ;
}
20
Exercise 2
21
Exercise 3
22
Exercise 4
If the ages of Mr.X, Mr.Y and Mr.Z are input through the keyboard,
write a program to determine the youngest of the three.
23
Exercise 5
24
Exercise 6
The current year and the year in which the employee joined the
organization are entered through the keyboard. If the number of
years for which the employee has served the organization is greater
than 3 then a bonus of $2500 is given to the employee. If the years
of service are not greater than 3, then the program should do
nothing.
Exercise 8
Note: A leap year has 366 days, as opposed to a common year, which
has 365. Nearly every 4 years is a Leap Year, and we add a Leap
Day, an extra – or intercalary – day on February 29
Loops
int i=1;
while(i<=10)
{
printf("%d\n",i);
i++;
}
Formulating Algorithms: Counter-
Controlled Repetition
main()
{
inti;
for(i=1;i<=10;i++)
printf(“%d\n”,i);
}
All the following increment can be used:
i=i+1
i++
i+=1
Do- while loop
do
statement
while ( condition );
Example
main()
{
int counter=1;
do{
printf(“%d”,counter);
} while(++counter<=10);
for(x=1;x<=10;x++)
{
if(i==5)
{
break;
}
printf ("%d\n",x);
}
The continue Statement
Exercise 1
a) Obtain two numbers from the keyboard, compute their sum and
display the result.
b) Obtain two numbers from the keyboard, and determine and display
which (if either) is the larger of the two numbers.
c) Obtain a series of positive numbers from the keyboard, and
determine and display their sum. Assume that the user types the
sentinel value -1 to indicate “end of data entry.”
Q &A