Control Structures 1
Control Structures 1
STRUCTURES
EST 102 PROGRAMMING IN C
MODULE II
CONTROL STRUCTURES
• When we run a program, the statements are executed in
• It is indented to add
clarity to the program
Question?(Use Nested if)
• The marks obtained by a student in 5 different subjects
are input through the keyboard. The student gets a
division as per the following rules:
• Percentage above or equal to 60 - First division
• Percentage between 50 and 59 - Second division
• Percentage between 40 and 49 - Third division
• Percentage less than 40 - Fail
• Write a program to calculate the division obtained by the
student.
Tutorial Question?
• A company insures its drivers in the following cases:
• − If the driver is married.
• − If the driver is unmarried, male & above 30 years of age.
• − If the driver is unmarried, female & above 25 years of
age.
• In all other cases the driver is not insured. If the marital
status, sex and age of the driver are the inputs, write a
program to determine whether the driver is to be insured
or not.
Tutorial Question?
• Write a program to calculate the salary as per the
following table:
Conditional Operators
• They are called Ternary operators since they take three
arguments.
• General form is:
• char a = 'z' ;
• printf ( "%c" , ( a >= 'a' ? a : '!' ) ) ;
• int big, a, b, c ;
• big = ( a > b ? ( a > c ? 3: 4 ) : ( b > c ? 6: 8 ) ) ;
Question?
• Write a program to find the greatest of the three numbers
loop operations.
• They are:
• int main()
• {
• int i=1;
• while(i<=10)
• {
• printf("%d \n",i);
• i++;
• }
• return 0;
• }
Question?
• W.A.P to print numbers
from 1 to 5?
Output?
• the increment
operator ++ increases
the value of a variable
by 1.
• x=x+1;
Increment ++ and Decrement -- Operator
• the decrement
operator -- decreases
the value of a variable
by 1.
• x=x-1;
++ and -- operator as prefix and postfix
Flowchart
Example
/* Calculation of simple interest for 3 sets of p, n
and r */
/* Calculation of simple interest for 3 sets of p, n
and r */
For loop
• The most popular looping instruction.
single line:
• (a) Setting a loop counter to an initial value.