05_Conditionals_and_Loops

# 05_Conditionals_and_Loops - 5 Conditionals and Loops...

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

COMP 1210 – Fundamentals of Computing I Slide 5 - 1 5. Conditionals and Loops • Objectives - when we have completed this chapter, you should be familiar with: flow of control: sequence, selection, iteration boolean expressions selection: if and if-else statements iteration: while loops equality, relational, and logical operators block statements comparing objects nested while loops Iterators the ArrayList class

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

View Full Document
COMP 1210 – Fundamentals of Computing I Slide 5 - 2 Flow of Control Sequence - Unless specified otherwise, the order of statement execution through a method is sequential, one statement after another Selection - statements that allow us to decide whether or not to execute a particular statement (or block of statements); that is, select among alternatives Examples: if , if-else , switch statements Iteration (repetition) - statements that allow us to execute a statement (or block of statements) over and over, repetitively Examples: while , do-while , for statements (or loops) • Iteration and Selection statements (except switch ) use boolean expressions that evaluate to true or false to determine whether a statement (or block of statements) is executed • The order of statement execution in a method is called the flow of control
COMP 1210 – Fundamentals of Computing I Slide 5 - 3 Flow of Control Flow of control : the order in which statements are executed in a program When we read source code, the sequence, selection, and iteration is relative to the method we’re reading Example: In the main method we may have 10 statements that are executed in sequence. If one of the statements invokes/calls a method and we step in (or jump) to that method, we will encounter sequence and possibly selection and/or iteration in this method You can use the debugger to follow the detailed flow of control (see examples in later slides) Q

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

View Full Document
COMP 1210 – Fundamentals of Computing I Slide 5 - 4 Boolean Expressions • Boolean expression: an expression that evaluates to true or false. Example: (where num1 and num2 are int values) num1 > num2 + 5 • A boolean variable can be assigned the result of a boolean expression: Example: (where email references a String object) boolean validEmail = email.contains( "@" ); boolean expression
COMP 1210 – Fundamentals of Computing I Slide 5 - 5 Boolean Expressions • An if statement uses a boolean expression as its condition: Example: temp is a double value representing outdoor temperature if (temp > 80) { System.out.println( "Stay indoors." ); } boolean expression

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

View Full Document
COMP 1210 – Fundamentals of Computing I Slide 5 - 6 Operators • Equality and Relational Operators (review): Evaluate to true or false Have lower precedence than arithmetic operators Operator Meaning == equal to != not equal to < less than > greater than <= less than or equal to >= greater than or equal to
COMP 1210 – Fundamentals of Computing I Slide 5 - 7 Operators

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 37

05_Conditionals_and_Loops - 5 Conditionals and Loops...

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

View Full Document
Ask a homework question - tutors are online