Chapter 4 Selection Control Structure PDF
Document Details
Uploaded by Deleted User
Universiti Kuala Lumpur
Tags
Summary
This document presents a lecture on selection control structures in programming, explaining concepts like pseudocode, flowcharts, and Java implementations. It covers relational operators, conditional operators, logical operators, as well as 'if...else' and 'switch' statements. Includes practical examples and problem-solving exercises.
Full Transcript
Chapter 4 Selection Control Structure Topics of discussion : i. how to write pseudocode for selection structure ii. How to draw flow chart for selection structure iii. Solve selection problem using Java 2 Relational Operators...
Chapter 4 Selection Control Structure Topics of discussion : i. how to write pseudocode for selection structure ii. How to draw flow chart for selection structure iii. Solve selection problem using Java 2 Relational Operators 3 Conditional Operators Conditional operators can be used to evaluate complex conditions as a whole 4 Logical Operators Table 4-1 Conditional logical AND && Table 4-2 Conditional logical OR || Table 4-3 Logical NOT 5 Evaluate the following Boolean expression i. 2015 >= 2014 ii. !(12 < 6) iii. 8 != 8 iv. int a = 4, b = 5, c = 6; a < b || b >= c b < = c && c == 6 6 Problem # 1 7 Phase 1 : Problem Analysis Input Process Output base area ← 0.5 * base * height message height IF area > 100 THEN Display “This is a BIG triangle” ELSE Display “This is a SMALL triangle” 8 Phase 2 : Program Design Find_big_triangle Prompt height , base Get height , base area ← 0.5 * base * height IF area > 100 THEN Display “This is a BIG triangle” ELSE Display “This is a SMALL triangle” END IF END 9 Simple if syntax Syntax: if (boolean expression) statement(s); Where the boolean expression must be enclosed by the pair of bracket, ( ). 10 if … else Syntax It is often necessary to execute a statements if a particular condition is true, and another different statement or statement block if the condition is false. In Java we did it with the keyword else. Syntax: if ( boolean expression ) statement(s); else statement(s); 11 Nested if It is possible to nest one if statement inside another. Syntax: if ( boolean expression ) statement(s); if ( boolean expression ) statement(s); 12 The case structure The case structure in pseudocode is another way of expressing a linear nested IF statement It can be translated into many high-level languages Makes pseudocode easier to write and understand The word CASE OF and END CASE serve to identify the structure Eg : CASE OF record_code ‘A’ : increment counter A ‘B’ : increment counter B ‘C’ : increment counter C END CASE 13 Switch statement (case) Another way of doing the if…else statement is by using the switch command. This is a better way of doing if … else if it involves a complex nested if… else statement. Syntax: switch( variable ) { case value1: statement(s); break; case value2 : statement(s); break; default : statement(s); } 14 Switch statement (case) Another way of doing the if…else statement is by using the switch command. This is a better way of doing if … else if it involves a complex nested if… else statement. Syntax: switch( variable ) { case value1: statement(s); break; case value2 : statement(s); break; default : statement(s); } 15 Create a Lesson class. This class offers three lessons; Swim, Snorkel and Dive. Each lesson is divided to three skill levels; Beginner, Intermediate and Advanced. Prompt the user to choose a lesson and enter a skill level. Fees for each lesson will be calculated based on lesson and skill levels as follows: Display total fees for each lesson. Test your program with different lessonLesson and levelLesson Price (RM) ID 1 Swim 50 2 Snorkel 25 3 Dive 100 Level ID Level Price (RM) B Beginner + 10 I Intermediate No extra charges A Advanced - 15 16