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

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

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

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

1

CISC181 Introduction to Computer Science

Dr. McCoy

Lecture 3 (2) & 4September 8 & 10, 2009

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

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.

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

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

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

4

More Rules for Thinking

2. Know the tools available to you– Control structures of the language

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

2003 Prentice Hall, Inc. All rights reserved.

5

2.4 Control 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

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

2003 Prentice Hall, Inc. All rights reserved.

6

2.4 Control Structures

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

2003 Prentice Hall, Inc. All rights reserved.

7

2.5 if Selection Structure

• Flowchart of pseudocode statement

A decision can be made on any expression.

zero - false

nonzero - true

Example:

3 - 4 is true

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

2003 Prentice Hall, Inc. All rights reserved.

8

2.6 if/else Selection Structure

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

2003 Prentice Hall, Inc. All rights reserved.

9

2.7 The while Repetition Structure

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

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

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

2003 Prentice Hall, Inc. All rights reserved.

11

2.5 if 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.)

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

2003 Prentice Hall, Inc. All rights reserved.

12

2.5 if Selection Structure

• Flowchart of pseudocode statement

A decision can be made on any expression.

zero - false

nonzero - true

Example:

3 - 4 is true

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

2003 Prentice Hall, Inc. All rights reserved.

13

2.5 if 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

 

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

2003 Prentice Hall, Inc. All rights reserved.

14

2.6 if/else Selection Structure

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

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

• Pseudocodeif student’s grade is greater than or equal to 60

print “Passed”else

print “Failed”

• C++ codeif ( grade >= 60 ) cout << "Passed";else cout << "Failed";

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

if-else Statement Syntax

• Formal syntax:if (<boolean_expression>)

<yes_statement>else

<no_statement>

• Note each alternative is only ONE statement!

• To have multiple statements execute ineither branch use compound statement

2-16Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Branching Mechanisms

• if-else statements

– Choice of two alternate statements basedon condition expression

– Example:if (hrs > 40)

grossPay = rate*40 + 1.5*rate*(hrs-40);else

grossPay = rate*hrs;

2-17Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

if-else Statement Syntax

• Formal syntax:if (<boolean_expression>)

<yes_statement>else

<no_statement>

• Note each alternative is only ONE statement!

• To have multiple statements execute ineither branch use compound statement

2-18Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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-19Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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-20Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Common Pitfalls

• Operator "=" vs. operator "=="• One means "assignment" (=)• One means "equality" (==)

– VERY different in C++!– Example:

if (x = 12) Note operator used! Do_Somethingelse Do_Something_Else

2-21Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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-22Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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-23Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Multiway if-else

• Not new, just different indenting• Avoids "excessive" indenting

– Syntax:

2-24Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

Multiway if-else Example

2-25Copyright © 2010 Pearson Addison-Wesley. All rights reserved.

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

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 << ( grade >= 60 ? “Passed” : “Failed” );

Condition Value if true Value if false

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

27

What is wrong, if anything, with the following?

int a, b, c, max, med, min;

if (a < b);

min = a;

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

28

What is wrong, if anything, with the following?

int a, b, c, max, med, min;

if (a < b);

min = a;

else

min = b;

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

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;

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

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!