{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
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";
Image of page 1

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

View Full Document Right Arrow Icon
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.
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern