Thinking Like A Computer: Part 2: Basic Structures
|
|
|
#include <iostream>
using namespace std; int main() { int age {0}; cout << "Enter your age: "; cin >> age; cout << "Bye."; } |
if you passed a class in school, then
clap cheer play a song else say, “Better luck next time, hang in there!” |
cout << "Enter your age: ";
cin >> age; if (age >= 18) cout << "You can vote." << endl; else cout << "Sorry, you can't vote yet." << endl; cout << "Bye."; |
Multi-Way Selection
|
CASEWHERE expression evaluates to
choice a : process a choice b : process b choice c : process c OTHERWISE : default process ENDCASE |
Symbol Meaning
< less than <= less than or equal to > greater than >= greater than or equal to == equal to != <> not equal to |
8 > 5 true
6 <= 3 false 9 == (3 X 3) true 4 > (8 / 2) false |
if (weather == raining) && (you have an umbrella)
walk to school else ask for a ride |
ask user to enter number between 0 and 25 inclusive
if (num >= 0) && (num <= 25) say "correct" else say "listen to instructions" |
ask user to enter number between 0 and 25 inclusive
if (num <0) || (num > 25) say "Number out of range. Try again!" else say "Thank you!" |
(13 < 9) or (4 X 2 == 72 / 9) true as one of the statements is true
(9 >= 3 X 3) or (5 < 8) true as both of the statements are true (7 <= 3) or (9 > 4 X 5) false as both of the statements are false |
while you continue to be hungry
take another bite of food Ask if you are still fill hungry |
for (int count = 1; count <= 3; count++) {
cout << "Enter your age: "; cin >> age; if (age >= 18) cout << "You can vote." << endl; else cout << "Sorry, you can't vote yet."<< endl; } |