CONTROL
STRUCTURES
EST 102 PROGRAMMING IN C
MODULE II
CONTROL STRUCTURES
• When we run a program, the statements are executed in
the order in which they appear in the program.
BRANCHING
Nested if
• If we write an entire if-else construct within either the
body of the if statement or the body of an else statement.
• This is called ‘nesting’ of ifs.
Nested if
• 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:
• expression 1 ? expression 2 : expression 3
• “if expression 1 is true (that is, if its value is non-zero),
then the value returned will be expression 2, otherwise
the value returned will be expression 3”
Conditional Operator
• int x, y ;
• scanf ( "%d", &x ) ;
•y=(x>5?3:4);
• This statement will store 3 in y if x is greater than 5,
otherwise it will store 4 in y.
Conditional Operator
• char a ;
• int y ;
• scanf ( "%c", &a ) ;
• y = ( a >= 65 && a <= 90 ? 1 : 0 ) ;
• Here 1 would be assigned to y if a >=65 && a <=90
evaluates to true, otherwise 0 would be assigned.
Conditional Operator
• int i ;
• scanf ( "%d", &i ) ;
• ( i == 1 ? printf ( "Amit" ) : printf ( "All and sundry" ) ) ;
• 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
entered through the keyboard using conditional operators.
SWITCH
The Case Control Structure
Decisions Using switch
• The control statement that allows us to make a decision
from the number of choices is called a switch, or more
correctly a switch-case-default.
Switch case
Switch case - break
Flowchart
Switch
LOOPING
This involves repeating some portion of the program either a
specified number of times or until a particular condition is
being satisfied.
LOOPING
• The C language provides three constructs for performing
loop operations.
• They are:
1. The while statement
2. The do statement
3. The for statement.
THE while STATEMENT
• entry-controlled loop statement.
• The test-condition is evaluated and if the condition is true,
then the body of the loop is executed.
• This process of repeated execution of the body continues
until the test-condition finally becomes false and the
control is transferred out of the loop.
• On exit, the program continues with the statement
immediately after the body of the loop.
THE while STATEMENT
Flowchart Example
THE while STATEMENT
Syntax Example
• 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?
infinite while loop
Increment ++ and Decrement -- Operator
• 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
• If you use the + • If you use the +
+ operator as a prefix + operator as a postfix
like: ++var, the value like: var++, the original
of var is incremented value of var is returned
by 1; then it returns the first; then var is
value. incremented by 1.
var++ vs ++var?
THE do while STATEMENT
• exit-controlled loop statement.
• the do-while tests the condition after having executed the
statements within the loop.
• It is executed till the condition remains true and the control
comes out of the loop when the condition becomes flase.
• do-while would execute its statements at least once, even if
the condition fails for the first time.
• There must be some loop terminating condition inside the body
of the loop to avoid infinite looping.
do while
Syntax Example
do while
• Flowchart
Example
Example
While vs do..while loop
Program?
• To find the sum of digits of a number.
• To print week days using do while and switch.
FOR LOOP
General Syntax and Flowchart
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.
• The for allows us to specify three things about a loop in a
single line:
• (a) Setting a loop counter to an initial value.
• (b) Testing the loop counter to determine whether its value
has reached the number of repetitions desired.
• (c) Increasing the value of loop counter each time the
program segment within the loop has been executed.
Program
Nested for loops
• Use for loop inside other for loops
• For loops can be used inside other control structures also
Factorial
• #include <stdio.h>int main(){ int num,fact,i; printf("\n Enter
the number to find the factorial:"); scanf("%d",&num);
if(num<0) printf("\n Factorial is not defined"); else {
fact=1; for(i=1 ;i<=num;i++) fact=fact*i;
printf("\n The factorial of %d is %d",num,fact);} return
0;}