{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

# 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 ring2 Programs should be able to adapt to user input and branch in different directions. ring2 A popular 1960s war protest slogan was "Don't trust anyone over 30." ring2 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? ring2 A boolean statement is a true / false statement. circle6 (2<3) is a true statement circle6 (2>3) is a false statement ring2 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; ring2 Named after logician George Boole (1815-1864). All the same. Sec 4.1 : The if statement ring2 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 { } ring2 The braces { } tell us what statements to run if the statement is true. ring2 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” ring2

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 ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern