lec05 - Lecture 5 Selection Boolean expressions and...

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

View Full Document Right Arrow Icon
CS125 Course Notes Lecture 5, Slide 1 Lecture 5 • Selection • Boolean expressions and operators (Savitch 3.1)
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 5, Slide 2 if-else statements Formal definition: A statement which allows a program to choose an action depending on the value of a boolean expression. • Example in plain English: If there are no clouds outside, it won’t rain. Otherwise, it might.
Background image of page 2
CS125 Course Notes Lecture 5, Slide 3 Boolean expressions • Must give a true or false value when evaluated. Recall a boolean variable can be either true or false . Named after George Boole (1815-1864), inventor of Boolean algebra (using AND, NOT, OR with true/false values).
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS125 Course Notes Lecture 5, Slide 4 if-else statements continued • Syntax: if ( Boolean_Expression ) { Statement_1 ; } else { Statement_2 ; }
Background image of page 4
CS125 Course Notes Lecture 5, Slide 5 if-else statement continued • Example: if (clouds) { System.out.println(“It might rain.”); } else { System.out.println(“It won’t rain.”); }
Background image of page 5

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

View Full DocumentRight Arrow Icon
Course Notes Lecture 5, Slide 6 Other forms of if-else statements No else part use if you want some code to be executed only if a condition is true. Same as having an empty else statement. Example:
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 07/27/2009 for the course MATH 135 taught by Professor Andrewchilds during the Fall '08 term at Waterloo.

Page1 / 18

lec05 - Lecture 5 Selection Boolean expressions and...

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

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