{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

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

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

View Full Document Right Arrow Icon
Engineering 101 Selection
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
Quote of the Day - Confucius The essence of knowledge is, having it, to apply  it; not having it, to confess your ignorance.
Image of page 2
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.
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
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
Image of page 4
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.
Image of page 5

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

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

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

View Full Document Right Arrow Icon
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.
Image of page 8
Selection n Allows the selective execution of a portion  of your code depending on some condition. n Syntax in C++: if ( condition )   statement_t The else can be entirely removed.
Image of page 9

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

View Full Document Right Arrow Icon
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 statement_f Or you can add more else conditions in the form of ‘else if’.
Image of page 10
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;
Image of page 11

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

View Full Document Right Arrow Icon
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
Image of page 12
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
Image of page 13

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

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

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

View Full Document Right Arrow Icon
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
Image of page 16
Simple Example
Image of page 17

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

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