Decision Making Branching Introduction o C program is
Decision Making & Branching
Introduction o C program is a set of statements which are normally executed sequentially in the order in which they appear. o However, we have a number of situations where we may have to change the order of execution of statements based on certain conditions, or repeat a group of statements until certain specified conditions are met. o Such statements are known as decision-making statements. Since these statements also control the flow of execution, they are also known as control January 3, 2022 Chapter 5 - Decision Making & 2 Branching statements.
Introduction o C possess a number of decision-making statements such as: n if statement n switch statement n Conditional operator statement n goto statement January 3, 2022 Chapter 5 - Decision Making & Branching 3
The if statement o The if statement is a powerful decision-making statement. o 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. o The point of program has two paths to follow, one for the true condition and the other for the false condition. January 3, 2022 Chapter 5 - Decision Making & Branching 4
Types of if statement o The if statement is implemented in different forms depending on the complexity of conditions to be tested: n Simple if statement n if…. . else statement n Nested if…. . else statement n Else if ladder January 3, 2022 Chapter 5 - Decision Making & Branching 5
Simple if statement The general form of a simple if statement is: Entry if (test-expression) { statement-block; Test expression? } False True Statement-x; January 3, 2022 Chapter 5 - Decision Making & Branching 6
Simple if statement Entry True Test expression? False Statement - Block Statement-x Next Statement January 3, 2022 Chapter 5 - Decision Making & Branching 7
The if-else statement The if…. else statement is an extension of the simple if statement. The general form is: if (test-expression) { True-block statement(s); } else { False-block statement(s); } Statement-x; January 3, 2022 Chapter 5 - Decision Making & Branching 8
The if-else statement Entry True False Test expression? False-Block Statements True-Block Statements Statement-x Next Statement January 3, 2022 Chapter 5 - Decision Making & Branching 9
The Nested if-else statement if (test condition 1) { if (test condition 2) { statement(s)-1; } else { statement(s)-2; } } else { statement(s)-3; } Statement-x; January 3, 2022 Chapter 5 - Decision Making & Branching 10
The Nested if-else statement Entry False Test condition 1? Statement-3 False True Test condition 2? Statement-2 True Statement-1 Statement-x Next Statement January 3, 2022 Chapter 5 - Decision Making & Branching 11
The else-if Ladder if (test condition 1) { statement(s) – 1 ; } ……… else if (test condition n) { statement(s) – n ; } else { default-statement(s); } statement – x ; January 3, 2022 Chapter 5 - Decision Making & Branching 12
The else-if Ladder True False Condition-1 Statement-1 False True Condition-n Statement-n Default Statement-x January 3, 2022 Chapter 5 - Decision Making & Branching 13
The switch statement switch ( expression ) { case value-1 : block-1; break; case value-2 : block-2; break; default : default-block; break; } Statement-x; January 3, 2022 Chapter 5 - Decision Making & Branching 14
The switch statement Entry switch exp 1 2 3 default Block 1 Block 2 Block 3 Default block statement x January 3, 2022 Chapter 5 - Decision Making & Branching 15
The switch statement o o Case Labels must be constants or constant expressions. No two case labels should have the same value. Case labels must end with semicolon. Break statement is optional. That is, two or more case labels may belong to the same statement. o The default label is optional and at most one default label can be present which can be placed anywhere. o It is permitted to nest switch statements up to 15 levels. January 3, 2022 Chapter 5 - Decision Making & Branching 16
Guidelines for writing Multi-way Selection Statements o Avoid compound negative statements. Use positive statements wherever possible. o Keep logical expressions short, simple and easy to understand. o Try to write the normal and more probable condition before less probable ones. o Use proper indentations. o Have a habit of using the default clause in switch. o Group the case labels that have similar actions. January 3, 2022 Chapter 5 - Decision Making & Branching 17
The goto statement If you want to jump somewhere without checking any condition then goto is used. goto label 1; statements; label 1: statements; January 3, 2022 Chapter 5 - Decision Making & Branching 18
The goto statement o Thus, goto statement is used to branch unconditionally. o The goto requires a label in order to identify the place where the branch is to be made. o A label is any valid variable name, and must be followed by a colon. o The label is placed immediately before the statement where the control is to be transferred. January 3, 2022 Chapter 5 - Decision Making & Branching 19
The goto statement o The label: can be anywhere in the program either before or after the goto label; statement. o The goto breaks the normal sequential execution of the program. o If the label: is before the goto label; statement, it is called backward jump, whereas it is called forward jump if it is placed after the same. o Thus, due to the unconditional goto statement, the program puts the computer in a permanent loop known as infinite loop, which has to be taken care of. January 3, 2022 Chapter 5 - Decision Making & Branching 20
- Slides: 20