Documente Academic
Documente Profesional
Documente Cultură
Objectives
After this Session, you will be able to understand control statements make use of conditional statements get ideas about switch statement know unconditional statements make decisions and use looping statements.
Control statements
C language supports the following statements known as control or decision making statements.
1. 2. 3. 4. if statement switch statement conditional operator statement goto statement
Conditional statements
IF STATEMENT The if statement is used to control the flow of execution of statements and is of the form if(test expression) It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is true or false, it transfers the control to a particular statement.
if statement
if(bank balance is zero) Borrow money The if statement may be implemented in different forms depending on the complexity of conditions to be tested. 1. Simple if statement 2. if..else statement 3. Nested if..else statement 4. elseif ladder
Simple if statement
if(test exprn) { statement-block; } statement-x;
if(category = = SPORTS) { marks = marks + bonus_marks; } printf(%f ,marks);
If the test expression is true, then the true block statements are executed; otherwise the false block statement will be executed.
4 nested if-else statements if e1 s1; else if e2 s2; else if e3 s3; else if e4 s4; else s5;
OUTPUT Enter customer no. and units consumed 101 150 Customer no=101 charges = 75.00
switch statement
switch(expression) { case value-1: block-1 break; case value-2: block-2 break; . . default: default-block break; } statement-x;
Eg: #include <stdio.h> void main() { int grade; printf ("Input grade :"); scanf("%d", & grade); switch( grade ) { case 'A' : printf( "Excellent" ); break; case 'B' : printf( "Good" ); break; case 'C' : printf( "OK" ); break; case 'D' : printf( "Mmmmm...." ); break; case 'F' : printf( "You must do better than this" ); break; default : printf( "What is your grade anyway?" ); break; } }
UNCONDITIONAL STATEMENTS
THE GOTO STATEMENT C supports the goto statement to branch unconditionally from one point of the program to another. The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and must be followed by a colon.
Looping Constructs
In looping, a sequence of statements are executed until some conditions for termination of the loop are satisfied. A program loop therefore consists of two segments, one known as the body of the loop and the other known as the control statements. Depending on the position of the control statements in the loop, a control structure may be classified either as an entrycontrolled loop or as the exit-controlled loop.
Looping Constructs
Eg: main() { double x, y; read: scanf(%f,&x); if(x < 0) goto read; y = sqrt(x); printf(%f %f \n,x, y); }
Looping Constructs
Looping Constructs
The C language provides three loop constructs for performing loop operations. They are
The while statement The do statement The for statement
The do statement
In while loop the body of the loop may not be executed at all if the condition is not satisfied at the very first attempt. Such situations can be handled with the help of the do statement.
do { body of the loop } while(test condition);
The do statement
Eg:
----------do { printf(Input a number\n); number = getnum(); } while(number > 0); -----------
continue
Skipping a part of a Loop The continue statement tells the compiler, SKIP THE FOLLOWING STATEMENTS AND CONTINUE WITH THE NEXT ITERATION. The format of the Continue;
continue