# lec05 - Lecture 5 Selection Boolean expressions and...

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

CS125 Course Notes Lecture 5, Slide 1 Lecture 5 • Selection • Boolean expressions and operators (Savitch 3.1)

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

View Full Document
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.
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).

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

View Full Document
CS125 Course Notes Lecture 5, Slide 4 if-else statements continued • Syntax: if ( Boolean_Expression ) { Statement_1 ; } else { Statement_2 ; }
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.”); }

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

View Full Document
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:
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
Ask a homework question - tutors are online