Lec03 - CS1101Y: Programming Methodology Control Statements...

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

View Full Document Right Arrow Icon
Control Statements (part 1) Lecture 3 — August 25, 2008 CS1101Y: Programming Methodology
Background image of page 1

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

View Full DocumentRight Arrow Icon
August 25, 2008 CS1101Y--group2, Lecture 3 2 Outline • Conditions if Statement && Logical Operator || Logical Operator ! Logical Operator switch Statement
Background image of page 2
August 25, 2008 CS1101Y--group2, Lecture 3 3 Conditions Throughout this chapter, you’ll see if statements and loop statements where conditions appear within a pair of parentheses, like this: if ( <condition> ) { ... } while ( <condition> ) { ... } Typically, each condition involves some type of comparison and the comparisons use comparison operators….
Background image of page 3

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

View Full DocumentRight Arrow Icon
August 25, 2008 CS1101Y--group2, Lecture 3 4 Conditions Here are Java's comparison operators: ==, !=, <, >, <=, >= Each comparison operator evaluates to either true or false. •= = – Tests two operands for equality. – 3 == 3 evaluates to true – 3 == 4 evaluates to false – Note that == uses two equal signs, not one! •! = – Tests two operands for inequality. –T h e != operator is pronounced “not equal.” The < , > , <= , and >= operators work as expected.
Background image of page 4
August 25, 2008 CS1101Y--group2, Lecture 3 5 if Statement • Use an if statement if you need to ask a question in order to determine what to do next. • There are three forms for an if statement: if by itself • Use for problems where you want to do something or nothing . if , else • Use for problems where you want to do one thing or another thing. if , else if • Use for problems where you want to do one thing out of three or more choices.
Background image of page 5

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

View Full DocumentRight Arrow Icon
August 25, 2008 CS1101Y--group2, Lecture 3 6 if Statement pseudocode syntax if by itself: if <condition> <statement(s)> if , else : if <condition> <statement(s)> else <statement(s)> Java syntax ± if by itself: if ( <condition> ) { <statement(s)> } ± if , else : if ( <condition> ) { <statement(s)> } else { <statement(s)> }
Background image of page 6
August 25, 2008 CS1101Y--group2, Lecture 3 7 if Statement pseudocode syntax if , else if : if <condition> <statement(s)> else if <condition> <statement(s)> .
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 / 20

Lec03 - CS1101Y: Programming Methodology Control Statements...

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