{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

06 - Selection - Engineering 101 Quote of the Day The...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
1 Engineering 101 Selection Quote of the Day - Confucius The essence of knowledge is, having it, to apply it; not having it, to confess your ignorance. Selection square6 So far we have discussed sequence and iteration. square6 Selection allows the execution of a portion of your code depending on some condition. Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t else statement_f Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t else statement_f The condition is similar to the while condition. It evaluates to true or false. Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t else statement_f If the condition evaluates to true then statement_t is executed.
Background image of page 1

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

View Full Document Right Arrow Icon
2 Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t else statement_f If the condition evaluates to false then statement_f is executed. Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t else statement_f The statements can either be single line expressions or multiple expressions enclosed in braces. Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition) statement_t The else can be entirely removed. Selection square6 Allows the selective execution of a portion of your code depending on some condition. square6 Syntax in C++: if (condition1) statement_t1 else if (condition2) statement_t2 else statement_f Or you can add more else conditions in the form of ‘else if’. Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 14
Background image of page 2
3 Simple Example int x; cin >> x; if (14 % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 14 Simple Example int x; cin >> x; if (0 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 14 Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 14 14 is an even number Simple Example int x; cin >> x; if (x % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 27 Simple Example int x; cin >> x; if (27 % 2 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 27 Simple Example int x; cin >> x; if (1 == 0) cout << x << “is an even number” << endl; else cout << x << “is an odd number” << endl; x 27
Background image of page 3

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

View Full Document Right Arrow Icon
4 Simple Example int x; cin >> x; if (x % 2 == 0)
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}