CS161_Topic#5

CS161_Topic#5 - CS161 CS161 Introduction to Computer...

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

View Full Document Right Arrow Icon
1 CS161 CS161 Introduction to Introduction to Computer Science Computer Science Topic #5
Background image of page 1

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

View Full DocumentRight Arrow Icon
CS161 Topic #5 2 Today in CS161 Selective Execution if statements Conditional Statements Input Stream Input Buffer, whitespace Program Style
Background image of page 2
CS161 Topic #5 3 Selective Execution Most programs are not as simple as converting inches to mm! We need to select from alternatives. .. think of the ATM example. .. this can be done using an if statement an if allows us to select between 2 choices for example, we can select one thing or another, depending on the user
Background image of page 3

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

View Full DocumentRight Arrow Icon
CS161 Topic #5 4 if Statements For example, we can change our inches to mm conversion program, allowing the user to select whether they want to convert from inches to mm, or mm to inches! We will give the user a choice. .. type ‘m’ to convert to mm type ‘i’ to convert to inches
Background image of page 4
CS161 Topic #5 5 if Statements have the form. .. 1) One alternative: if (logical expression) single C++ statement; char selection; cout << “Enter a selection (m or i): “; cin >> selection; if (selection == ‘q’) cout << “Your selection was incorrect” << endl;
Background image of page 5

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

View Full DocumentRight Arrow Icon
CS161 Topic #5 6 if Statements have the form. .. 2) Two alternatives: if (logical expression) single C++ statement; else single C++ statement; if (selection == ‘m’) cout <<“Converting inches -> mm”; else cout <<“Converting mm -> inches”;
Background image of page 6
7 if Statements have the form. .. This means that either the first statement is executed  when running your program OR the second statement is  executed. BOTH sets of statements are NEVER used.  ONE OR THE OTHER! If the comparison is true - the first set is used;
Background image of page 7

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

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

Page1 / 28

CS161_Topic#5 - CS161 CS161 Introduction to Computer...

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

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