Control and conditional statements

rajshreemuthiah 3,961 views 19 slides Oct 23, 2019
Slide 1
Slide 1 of 19
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19

About This Presentation

control and conditional


Slide Content

Control and Conditional statement in c M.Rajshree M.Sc IT Nadar saraswathi college of arts&science

CONDITIONAL STATEMENT(DECISION MAKING) The decision is describe to computer as a conditional statement that can be answered true or false Since these statement control the flow of execution they also known as control statements The if statement have different forms they are: Simple if statement If else statement Nested if else statement Else if ladder

SIMPLE IF STATEMENT if test expression is true the statement block will be executed otherwise the statement block will be skipped and the execution will jump to next statement Syntax if(test expression ) { statement block; } statement x;

Example program : i = int (input(“Enter the number:”)); if ( i <=10) printf (“Condition is true” ); Output : Enter the number:9 Condition is true IF ELSE STATEMENT If test expression is true then true block statement are executed otherwise false block statement are executed not both block are executed at same time.

Syntax : if(test expression) { true block statement; } else { false block statement; } statement x;

printf ("Enter your age:"); scanf ("% d",&age ); if(age >=18) { printf ("You are eligible for voting"); } else { printf ("You are not eligible for voting"); } Output : Enter your age:14 You are not eligible for voting Example program :

NESTED IF ELSE : If condition 1 is false the statement 3 will be executed otherwise it continue to perform second test if condition 2 is true the statement 1 will be evaluated otherwise statement 2 will be evaluated and then transferred to statement x

Syntax : if(test condition 1) { if(test condition 2) { statement 1; } else { statement 2; } } else { statement 3; } statement x;

Example program : n= int (input(“Enter number:”)); if(n<=15) if(n==10) print(“Play volleyball”); else print(“Play cricket”); else print(“don’t play game”); Output : Enter number:10 Play volleyball

ELSE IF LADDER : The condition are evaluated from top of ladder downwards. As soon as a true condition is found the statement associated with it is executed and the control is transferred to the statement x. When all n conditions become false then final else containing the default statement will be executed .

Syntax : if(condition 1) statement 1; else if(condition 2) statement 2; else if(condition 3); statement 3; else if(condition n); statement n; else default statement; statement x;

Example program a= int (input(“enter 1st number:”)) b= int (input(“enter 2 nd number:”)) c= int (input(“enter 3 rd number:”)) If(a>b)and(a>c) print(“a is greater”) Elseif (b<a)and(b<c) print(“b is greater”) Else print(“c is greater”) Output enter 1 st number:10 enter 2 nd number:25 enter 3 rd number:15 b is greater

DECISION MAKING AND LOOPING The test may be either to determine whether loop has been repeated specified number of times or to determine whether a particular condition has been met The c language provides three constructs for performing loop operations they are: While statement Do while statement For statement

While statement The while is an entry controlled loop statement The test condition is evaluated and if condition is true then the body of loop is executed After execution of the body the test condition is once again evaluated and if it is true the body is executed once again The body of the loop may have one or more statement

SYNTAX while(test condition) { body of the loop } Example program int count=1; while(count<=4) { printf (“% d”,count ); count++; }

OUTPUT 1 2 3 4 The body of the loop may not be executed at all if the condition is not satisfied at the very first attempt On some occasion to execute body of the loop before the test is performed such situations can be handled with the help of do statement Dowhile

SYNTAX Do { body of the loop } while(test condition); Example program int j=0; do { printf ("Value of variable j is: %d\n", j); j++; } while (j<=3);

OUTPUT Value of variable j is: 0 Value of variable j is: 1 Value of variable j is: 2 Value of variable j is: 3 FOR LOOP The for loop is another entry controlled loop that provides a more concise loop control structure initialization of control variables is done first using assignment statement The value of control variable is tested using test condition

SYNTAX for( initialization;testcondition;increment ) { body of the loop } Example program int i; for (i=1; i<=3; i++) { printf("%d\n", i); } Output 1 2 3
Tags