Chapter 3 Control Statements Prerequisites for Part I Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Chapter 1 Introduction to Computers, Programs hapter 2 Primitive Data Types and Operations 选择是艰难的 pter 3 Control Statements 谁能控制自己的命运? Chapter 4 Methods apter 5 arrays Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 1 Chapter 3 Control Statements Chapter 1 Introduction to Computers, Programs, and Java Chapter 2 Primitive Data Types and Operations Chapter 3 Control Statements Chapter 5 Arrays Chapter 4 Methods Basic computer skills such as using Windows, Internet Explorer, and Microsoft Word Prerequisites for Part I 选择是艰难的 谁能控制自己的命运?
Obiectives To understand the flow of control in selection and loop statements (§32-3.7) To use Boolean expressions to control selection statements and loop statements(§3.2-37) To implement selection control using if and nested if statements (§3.2) To implement selection control using switch statements(s3. 2) To write expressions using the conditional operator(83. 2) To use while, do-while, and for loop statements to control the repetition of statements(s 3. 4) To write nested loops(8.4) To know the similarities and differences of three types of loops (§3.5) To implement program control with break and continue(s 3.6) Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 2 Objectives • To understand the flow of control in selection and loop statements (§3.2-3.7). • To use Boolean expressions to control selection statements and loop statements (§3.2-3.7). • To implement selection control using if and nested if statements (§3.2). • To implement selection control using switch statements (§3.2). • To write expressions using the conditional operator (§3.2). • To use while, do-while, and for loop statements to control the repetition of statements (§3.4). • To write nested loops (§3.4). • To know the similarities and differences of three types of loops (§3.5). • To implement program control with break and continue (§3.6)
Algorithms ● Algorithm O Series of actions in specific order o The actions executed The order in which actions execute ● Program control O Specifying the order in which actions execute o Control structures help specify this order Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 3 Algorithms ⚫Algorithm Series of actions in specific order ⚫The actions executed ⚫The order in which actions execute ⚫Program control Specifying the order in which actions execute ⚫Control structures help specify this order
Pseudocode o Pseudocode OInformal language for developing algorithms ONot executed on computers O Helps developers think out''algorithms O Normally describes only executable statements if student's grade is greater then or equal to 60 Print Passed (grade>=60) System. out. printIn Passed") Liang, Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 4 Pseudocode ⚫Pseudocode Informal language for developing algorithms Not executed on computers Helps developers “think out” algorithms Normally describes only executable statements if student’s grade is greater then or equal to 60 Print " Passed" if(grade>=60) System.out.println(“Passed");
Control structures o Sequential execution O Program statements execute one after the other ● Transfer of control o Three control statements can specify order of statements o Sequence structure(built in Java) Selection structure Repetition structure ●F| owchart O Graphical representation of algorithm o flowines indicate order in which actions execute Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 5 Control Structures ⚫ Sequential execution Program statements execute one after the other ⚫ Transfer of control Three control statements can specify order of statements ⚫Sequence structure (built in Java) ⚫Selection structure ⚫Repetition structure ⚫ Flowchart Graphical representation of algorithm ⚫Flowlines indicate order in which actions execute
Flowlines Action Symbols total= total+ grade; counter= counter+ 1 Connector Symbols Flowcharting Javas sequence structure. Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 6 add grade to total total = total + grade; add 1 to counter counter = counter + 1 ; Flowcharting Java’s sequence structure. Flowlines Action Symbols Connector Symbols
Decision Symbol true grade >=60 print Passed false Flowcharting the single-selection if structure Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 7 grade >= 60 true false print “Passed” Flowcharting the single-selection if structure. Decision Symbol
Selection Statements >if Statements switch statements Conditional Operators Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 8 Selection Statements ➢ if Statements ➢ switch Statements ➢ Conditional Operators
Simple if Statements if (radius >=0)3 area radius radius PI if(booleanExpression)i System. out. println("The area"+ statement(s) for the circle of radi radius +iS"+ area false false E Statement(s) System. out println("The area for the circle of " radius"+radius +"is"+ area (A) Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 9 Simple if Statements Boolean Expression true Statement(s) false (radius >= 0) true area = radius * radius * PI; System.out.println("The area for the circle of " + "radius " + radius + " is " + area); false (A) (B) if (booleanExpression) { statement(s); } if (radius >= 0) { area = radius * radius * PI; System.out.println("The area" + “ for the circle of radius " + radius + " is " + area); }
Note Outer parentheses required Braces can be omitted if the block contains a single f((i>0)&&(i0)&&(i 10)) System. out. printin("i isan integer between 0 and 10")i integer between 0 and 10)i Introduction to Java Programming, revised by Dai-kaiyu
Liang,Introduction to Java Programming,revised by Dai-kaiyu 10 Note if ((i > 0) && (i 0) && (i < 10)) System.out.println("i is an " + + "integer between 0 and 10"); Outer parentheses required Braces can be omitted if the block contains a single statement