# Lec8 - Lecture 8 The if Statement PIC 10A Todd Wittman if...

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

1 Lecture 8: The if Statement PIC 10A Todd Wittman if Statement Example r Programs should be able to adapt to user input and branch in different directions. r A popular 1960s war protest slogan was "Don't trust anyone over 30." r How do we check if we can trust someone? int age; cout << "Enter your age: "; cin >> age; if (age < 31) cout << "I trust you.\n"; if (age > 30) cout << "I DON'T trust you.\n";

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

View Full Document
2 What is a boolean? r A boolean statement is a true / false statement. c (2<3) is a true statement c (2>3) is a false statement r Later we’ll learn about boolean ( bool ) variables, which take on only true or false values. Could also do 1(T) / 0(F). bool my_boolean = (2 < 3); bool my_boolean = true; bool my_boolean = 1; r Named after logician George Boole (1815-1864). All the same. Sec 4.1 : The if statement r An if statement will execute the statements that follow only if the boolean condition in parentheses is TRUE. if ( statement is TRUE ) { **STATEMENTS** } int x = 2; int x = 4; if (x < 3) { if (x < 3) { cout << "hi"; cout << "hi"; } } Outputs "hi" Outputs nothing nada zip Notice the indentation of the closing }. Lines up with the if statement.
3 The braces { } r The braces { } tell us what statements to run if the statement is true. r Without the braces, only the first line following the if statement is executed. int x = 2; int x = 2; if (x > 3) { if ( x > 3) cout << "My "; cout << "My"; cout << "precious!"; cout << "precious"; } Outputs nothing. Outputs “precious” r On the right, would have been better not to indent

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.

## This note was uploaded on 06/30/2009 for the course PIC 10A taught by Professor Wittman during the Fall '08 term at UCLA.

### Page1 / 9

Lec8 - Lecture 8 The if Statement PIC 10A Todd Wittman if...

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

View Full Document
Ask a homework question - tutors are online