06 Selection - Click to edit Master subtitle style...

Info iconThis preview shows pages 1–16. 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

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

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

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: Click to edit Master subtitle style 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 n So far we have discussed sequence and iteration . n Selection allows the execution of a portion of your code depending on some condition. Selection n Allows the selective execution of a portion of your code depending on some condition. n Syntax in C++: if (condition) statement_t else statement_f Selection n Allows the selective execution of a portion of your code depending on some condition. n 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 n Allows the selective execution of a portion of your code depending on some condition. n Syntax in C++: if ( condition ) statement_t else statement_f If the condition evaluates to true then statement_t is executed. Selection n Allows the selective execution of a portion of your code depending on some condition. n Syntax in C++: if ( condition ) statement_t else statement_f If the condition evaluates to false then statement_f is executed. Selection n Allows the selective execution of a portion of your code depending on some condition. n 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 n Allows the selective execution of a portion of your code depending on some condition. n Syntax in C++: if ( condition ) statement_t else statement_f The else can be entirely removed. Selection n Allows the selective execution of a portion of your code depending on some condition. n Syntax in C++: if ( condition1 ) statement_t1 else if ( condition2 ) statement_t2 else 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 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...
View Full Document

This note was uploaded on 04/07/2010 for the course ENGR engr 101 taught by Professor Rinenberg during the Spring '10 term at University of Michigan.

Page1 / 48

06 Selection - Click to edit Master subtitle style...

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

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