Presentation is loading. Please wait.

Presentation is loading. Please wait.

Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.

Similar presentations


Presentation on theme: "Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The."— Presentation transcript:

1 Decision making statements

2 Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The decision making statements are, – simple if statement – if…else statement – nested if – switch statement

3 Simple if statement The if statement is a powerful decision making statement and is used to control the flow of execution of statements. It is basically two ways decision statement is of the form if (test condition) It allows a computer to evaluate the expression first and depending upon whether the value of condition is true or false. It will transfer the control to a particular statement

4 Syntax The general form of simple if statement is if (condition) { statement-block } Example: if(x>40) { printf(“x is greater”); }

5 Flow chart for if statement

6 Rules The brackets around the test condition are must. The test condition must be relational or logical expression. Statement block is called body of the if statement and it contains one or more statements. The opening and closing brackets {} are must if the statement block contains more than one statement. else optional

7 If else statement If else statement is used to execute one group of statements, if the test condition is true or other group if the test condition is false.

8 Syntax if (condition) { true block statement } else { false block statement }

9 Example #include main() { int a,b; printf(“enter the value of a:”); scanf(“%d”,&a); printf(“enter the value of b:”); scanf(“%d”,&b); if(a>40) { printf(“ a is greater”); } else { printf(“b is greater”); }

10 Flow chart for if else

11 Rules The brackets around the test condition are must. The test condition must be relational or logical expression. Statement block is called body of the if statement. It can contains one or more statements. The opening and closing brackets {} are must if the statement block contains more than one statement. else optional. No statements other than the statements in the statement blocks are allowed between if…else.

12 Nested if If within if is called nested if and it is used when we have multiple conditions to check and when any if condition contains another if statement then that is called ‘nested if’. If the external condition is true, then the internal if condition or condition are executed and if the condition is false then the else portion is executed of the external if statement

13 Syntax if (condition) { } if (condition) { ……. } else if (condition) { ……. } else if (condition) { ……. } else { ……. } } else { ……. }

14 Example if (num < 0) { result = abs(num); printf("%d", result); } else { if (num == 0) printf("the number is zero"); else { result = num * num; printf("%d", result); } }

15 Flow chart for nested if

16 Rules The baby IF may be in the TRUE part or the FALSE part of the mother IF. Or, both the TRUE part and the FALSE part of the mother IF may contain baby IFs. Each ELSE belongs to the NEAREST IF, whether mother or baby. Therefore, we need to study carefully what each else means. Be very careful opening and closing the braces of each IF. Do you really want to finish this IF here? What happens if this IF is finished and some other statement comes after the brace: We need to trace what we are doing very carefully.

17 Switch statement C has a built-in multiway decision statement known as a switch. The switch statement tests the value of a given variable against a list of case values and when a match is found a block of statements associated with that case is executed. The break statement at the end of each block signals the end of a particular case and cause exit from switch statement transferring the control to the statement-x The default is optional case. It will be executed if the value of the expression does not match with any of the case values.

18 Syntax switch (expression) { case value-1; block-1 break; case value-2; block-2 break; ……….. default: default-block break; } statement-x;

19 Example switch(x) { case 'A': Print("CASE A"); break; case 'B': case 'C': Print("CASE B or C"); break; default: Print("NOT A, B or C"); break; }

20 Flow chart for switch statement

21 Rules The expression should be placed in parentheses. The value of the expression should be an integer. The body of the switch statement should be enclosed within {} brackets. Case label should terminate with a colon. Break statement is a must and causes immediate exit from switch statement. If it is not included the statements below the matched case will be executed.

22 End Thank you


Download ppt "Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The."

Similar presentations


Ads by Google