06 - Selection - 1 Engineering 101 Selection Quote of the...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 s So far we have discussed sequence and iteration. s Selection allows the execution of a portion of your code depending on some condition. Selection s Allows the selective execution of a portion of your code depending on some condition. s Syntax in C++: if (condition) statement_t else statement_f Selection s Allows the selective execution of a portion of your code depending on some condition. s 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 s Allows the selective execution of a portion of your code depending on some condition. s Syntax in C++: if (condition) statement_t else statement_f If the condition evaluates to true then statement_t is executed. 2 Selection s Allows the selective execution of a portion of your code depending on some condition. s Syntax in C++: if (condition) statement_t else statement_f If the condition evaluates to false then statement_f is executed. Selection s Allows the selective execution of a portion of your code depending on some condition. s 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 s Allows the selective execution of a portion of your code depending on some condition. s Syntax in C++: if (condition) statement_t The else can be entirely removed. Selection s Allows the selective execution of a portion of your code depending on some condition. s 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 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 4...
View Full Document

This note was uploaded on 01/30/2012 for the course ENGR 101 taught by Professor Ringenberg during the Winter '07 term at University of Michigan.

Page1 / 8

06 - Selection - 1 Engineering 101 Selection Quote of the...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online