Chapter 7 Control Statement (Conditional).pptx

# Chapter 7 Control Statement (Conditional).pptx - Chapter 7...

• 66

This preview shows page 1 - 15 out of 66 pages.

Chapter 7 Control Statements (Conditional Statement/Selection)

Subscribe to view the full document.

Objectives At the end of this chapter, student should be able to: understand various concepts of selection control structure be able to develop a program containing selection control structure 2
Introduction Program control specifies the order the statements are executed in a program Program execute statements in sequence How to by pass the sequences selection statements Or How to repeat the sequences loop statements 3

Subscribe to view the full document.

Selection Statements Altering the flow of program execution by making a selection or choice Single if If…else Nested if switch statement 4
5 Recall Temperature Converter ( F2C) 1. Start 2. Get Fahrenheit value 3. Convert to Celsius Celsius = 5.0/9.0 * (Fahrenheit – 32) 4. Display temperature in Celsius 5. End

Subscribe to view the full document.

6 Recall Temperature Converter ( C2F) 1. Start 2. Get Celsius value 3. Convert to Fahrenheit Fahrenheit = 9.0/5.0 * (Celcius + 32) 4. Display temperature in Fahrenheit 5. End
Single if Syntax: if (booleanExpression){ statement(s) } booleanExpression referred to a value true or false or result of a comparison, i.e: (5>1) = true statement referred to as action statement. 7

Subscribe to view the full document.

Single if flow chart 8 true false Step n Step x Step a Boolean Expression
Single if 9 Pseudocode Structure step a if <booelan expression> start step n end_if step x Pseudocode Structure step a if <booelan expression> start step n end_if step x

Subscribe to view the full document.

if Statement The structure is similar to single selection (flowchart) 10 Syntax: if (expression) statement; or if (expression) { statement1; statement2; } Don’t forget the curly brackets, if there are more than ONE statements Don’t forget the brackets !!
Single if (case true) int gaji =1200; int cola = 0; if (gaji<2000) cola=10/100 * gaji; System.out.println(“Jumlah Gaji = RM“+gaji+cola); true false Step n Step x Step a Boolean Expression gaji 1200 cola 120 Jumlah Gaji = RM1320 _

Subscribe to view the full document.

Single if (case false) gaji 2145 cola 0 Jumlah Gaji = RM2145 _ int gaji =2145; int cola = 0; if (gaji<2000){ cola=10/100 * gaji; } System.out.println(“Jumlah Gaji = RM“+gaji+cola); Step n false Step x true Step a Boolean Expression
13 if Statement ……. . int mark; System.out.println(“Mark: “); mark=in.nextInt(); if (mark > 80) { System.out.println(“Category: Excellent\n”); System.out.println(“Congratulations!”); } System.out.println(“Keep Work Hard”); Mark: _ mark 92 > 80? 92 Mark: 92 _ Category: Excellent Congratulations! Keep Work Hard

Subscribe to view the full document.

14 if Statement ……. . int mark; System.out.println(“Mark: “); mark=in.nextInt(); if (mark > 80) { System.out.println(“Category: Excellent\n”); System.out.println(“Congratulations!”); } System.out.println(“Keep Work Hard”); Mark: _ mark 66 > 80? 66 Mark: 66 _ Keep Work Hard
15 if Statement public static void main(String[] args) { int mark; Scanner in = new Scanner(System.in); System.out.println(“Mark: “); mark=in.nextInt(); if (mark >= 50) System.out.println(“Pass\n”); System.out.println(“Your mark is ”+ mark); } public static void main(String[] args) { int mark; Scanner in = new Scanner(System.in); System.out.println(“Mark: “); mark=in.nextInt(); if (mark >= 50) System.out.println(“Pass\n”);
You've reached the end of this preview.
• Spring '17
• DR. NOORHAFHIZAH