ch5 - Chapter 5 Selection Statements Topics Controlling...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Chapter 5 Selection Statements
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Topics Controlling program flow selection if switch Boolean expressions boolean primitive type comparison operators boolean operators
Background image of page 2
Control Statements Statements in programs are normally executed in sequence. Method calls transfer control to another part of the code (and back again) A statement that alters the control flow is called a control statement . Selection (branching) allows program to decide whether to do a part of the code Repetition (Chapter 6) allows sections of code to be repeated Exceptions (Chapter 8) also alter the flow of a program
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
The if Statement The if statement is one type of selection statement. BufferedReader kbd = new BufferedReader( new InputStreamReader( System.in)); System.out.println(“Enter test score:”); String input = kbd.readLine(); int testScore = Integer.parseInt( input); if (testScore < 70) System.out.println(“You did not pass”); else System.out.println(“You passed”);
Background image of page 4
The if Statement The if statement specifies which block of code to execute, depending on the result of evaluating a test condition called a boolean expression . if (<boolean expression>) <then block> else <else block> The <boolean expression> is an expression that evaluates to either true or false.
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Syntax of an if statement Mapping of the sample if statement to the general format.
Background image of page 6
Boolean Expressions and Variables A boolean expression is either true or false . true and false are reserved words
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 27

ch5 - Chapter 5 Selection Statements Topics Controlling...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online