Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.

Similar presentations


Presentation on theme: "1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10."— Presentation transcript:

1 1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10

2 2 Announcements Read Sections 4.1 – 4.6 Read Lab 3 –For lab on Thursday Homework 3 –Due Wednesday, 9/28/05 –Page 222-228 –4.1, 4.2, 4.3, 4.10, 4.11, 4.12, 4.33, 4.37, 4.39, 4.47

3 3 Review Incrementing Defining a constant Conversions Nonfundamental types –Classes –Strings

4 4 Outline Control Construct –Conditional or selection constructs

5 5 Conditional Constructs Provide –Ability to control whether a statement list is executed Two constructs –If statement if if-else if-else-if –Switch statement Left for reading

6 6 Flowchart –Graphical representation of an algorithm –Drawn using certain special-purpose symbols connected by arrows called flowlines –Rectangle symbol (action symbol) Indicates any type of action –Oval symbol Indicates beginning or end of a program, or a section of code (circles)

7 7 The If Selection Structure Selection structure –Used to choose alternative courses of action –Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” –If the condition is true Print statement executed Program goes to the next statement –If the condition is false Print statement is ignored Program goes to the next statement

8 8 The if Selection Structure Translation of PseudoCode into C++ if (grade >= 60) cout << "Passed"; –Indentation to make the program easier to read Diamond symbol (decision symbol) –Indicates decision is to be made –Contains an expression that can be true or false Boolean expression Test the condition, follow appropriate path

9 9 The If Selection Structure grade >= 60 print passed true false

10 10 The If Selection Structure Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression Action truefalse

11 11 Sorting Two Numbers cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; if (value1 > value2) { int rememberValue1 = value1; value1 = value2; value2 = rememberValue1; } cout << "The input in sorted order: " << value1 << " " << value2 << endl;

12 12 The if/else Selection Structure if –Only performs an action if the condition is true if/else –A different action is performed when condition is true and when condition is false Psuedocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed”

13 13 The if/else Selection Structure Translation of PseudoCode into C++ if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

14 14 The if/else Selection Structure Flowchart grade >= 60 print passed print failed true false

15 15 The if/else Selection Structure Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2

16 16 Finding the Max cout << "Enter two integers: "; int value1; int value2; cin >> value1 >> value2; int max; if (value1 < value2) { max = value2; } else { max = value1; } cout << "Maximum of inputs is: " << max << endl;

17 17 The if/else Selection Structure Ternary conditional operator ( ?: ) –Takes three arguments –(condition, value if true, value if false ) –The only ternary C++ operator Using the ternary conditional operator cout = 60 ? “Passed” : “Failed” );

18 18 The if/else Selection Structure Nested if/else structures –Test for multiple cases by placing if/else selection structures inside if/else selection structures –Once a condition is met, the rest of the statements are skipped

19 19 The if/else Selection Structure if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

20 20 The if/else Selection Structure Compound statement: –Set of statements within a pair of braces –Example: if ( grade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } –Without the braces, cout << "You must take this course again.\n"; would be automatically executed

21 21 The if/else Selection Structure Block –Compound statements –All statements between {} Suggestion –Always use {} to denote the action blocks of an if/else selection structure

22 22 Errors I make Syntax errors –Errors caught by compiler Logic errors –Errors which have their effect at execution time Non-fatal logic errors –Program runs, but has incorrect output Fatal logic errors –Program exits prematurely

23 23 Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice –if-else-if statement if-else statements “glued” together –Switch statement An advanced construct

24 24 A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; }

25 25 cout << "Enter simple expression: "; int left; int right; char operator; cin >> left >> operator >> right; cout << left << " " << operator << " " << right << " = "; switch (operator) { case '+' : cout << left + right << endl; break; case '-' : cout << left - right << endl; break; case '*' : cout << left * right << endl; break; case '/' : cout << left / right << endl; break; default: cout << "Illegal operation" << endl; }


Download ppt "1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10."

Similar presentations


Ads by Google