Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009.

Similar presentations


Presentation on theme: "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009."— Presentation transcript:

1 1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009

2 2 Programming Gets Tougher Need rules for thinking about more difficult programming problems Take your time – think first. Make sure you understand what it is you are trying to do before you try to do it.

3 3 Rules 1.Think before you code –Use some abstract short-hand design Flowcharts/activity diagrams Pseudocode – informal language for writing “algorithms” –Set of actions to be executed –Specified order for the actions

4 4 More Rules for Thinking 2.Know the tools available to you –Control structures of the language

5  2003 Prentice Hall, Inc. All rights reserved. 5 2.4Control Structures Sequential execution –Statements executed in order Transfer of control –Next statement executed not next one in sequence 3 control structures (Bohm and Jacopini) –Sequence structure Programs executed sequentially by default –Selection structures if, if/else, switch –Repetition structures while, do/while, for

6  2003 Prentice Hall, Inc. All rights reserved. 6 2.4Control Structures

7  2003 Prentice Hall, Inc. All rights reserved. 7 2.5if Selection Structure Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true

8  2003 Prentice Hall, Inc. All rights reserved. 8 2.6 if/else Selection Structure

9  2003 Prentice Hall, Inc. All rights reserved. 9 2.7The while Repetition Structure

10 10 Control Structures and Programming Each C++ Program made up of these 7 control structures combined appropriately (turns out that we can make the last of these more specific, but we’ll see that later) –Sequentially –Nested

11  2003 Prentice Hall, Inc. All rights reserved. 11 2.5if Selection Structure Selection structure –Choose among 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 continues to next statement –If the condition is false Print statement ignored, program continues –Indenting makes programs easier to read C++ ignores whitespace characters (tabs, spaces, etc.)

12  2003 Prentice Hall, Inc. All rights reserved. 12 2.5if Selection Structure Flowchart of pseudocode statement A decision can be made on any expression. zero - false nonzero - true Example: 3 - 4 is true

13  2003 Prentice Hall, Inc. All rights reserved. 13 2.5if Selection Structure Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; Diamond symbol (decision symbol) –Indicates decision is to be made –Contains an expression that can be true or false Test condition, follow path if structure –Single-entry/single-exit

14  2003 Prentice Hall, Inc. All rights reserved. 14 2.6 if/else Selection Structure

15  2003 Prentice Hall, Inc. All rights reserved. 15 2.6 if/else Selection Structure if –Performs action if condition true if/else –Different actions if conditions true or false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

16 if-else Statement Syntax Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement 2-16 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

17 Branching Mechanisms if-else statements – Choice of two alternate statements based on condition expression – Example: if (hrs > 40) grossPay = rate*40 + 1.5*rate*(hrs-40); else grossPay = rate*hrs; 2-17 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

18 if-else Statement Syntax Formal syntax: if ( ) else Note each alternative is only ONE statement! To have multiple statements execute in either branch  use compound statement 2-18 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

19 Compound/Block Statement Only "get" one statement per branch Must use compound statement { } for multiples – Also called a "block" stmt Each block should have block statement – Even if just one statement – Enhances readability 2-19 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

20 Compound Statement in Action Note indenting in this example: if (myScore > yourScore) { cout << "I win!\n"; wager = wager + 100; } else { cout << "I wish these were golf scores.\n"; wager = 0; } 2-20 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

21 Common Pitfalls Operator "=" vs. operator "==" One means "assignment" (=) One means "equality" (==) – VERY different in C++! – Example: if (x = 12)  Note operator used! Do_Something else Do_Something_Else 2-21 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

22 The Optional else else clause is optional – If, in the false branch (else), you want "nothing" to happen, leave it out – Example: if (sales >= minimum) salary = salary + bonus; cout << "Salary = %" << salary; – Note: nothing to do for false condition, so there is no else clause! – Execution continues with cout statement 2-22 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

23 Nested Statements if-else statements contain smaller statements – Compound or simple statements (we’ve seen) – Can also contain any statement at all, including another if- else stmt! – Example: if (speed > 55) if (speed > 80) cout << "You’re really speeding!"; else cout << "You’re speeding."; Note proper indenting! 2-23 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

24 Multiway if-else Not new, just different indenting Avoids "excessive" indenting – Syntax: 2-24 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

25 Multiway if-else Example 2-25 Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

26  2003 Prentice Hall, Inc. All rights reserved. 26 2.6 if/else Selection Structure Ternary conditional operator ( ?: ) –Three arguments (condition, value if true, value if false ) Code could be written: cout = 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

27 27 What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a;

28 28 What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b); min = a; else min = b;

29 29 What is wrong, if anything, with the following? int a, b, c, max, med, min; if (a < b) min = a; max = b; else min = b; max = a;

30 30 What is wrong, if anything, with the following? Go To File ex2-26-and-more.cc Exercise 2.26 – Dangling-Else Problem Decide what prints Also, another messed-up if statement!


Download ppt "1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 3 (2) & 4 September 8 & 10, 2009."

Similar presentations


Ads by Google