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

Post on 13-Jan-2016

213 views 0 download

Tags:

Transcript of 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

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

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

More Rules for Thinking

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

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

2003 Prentice Hall, Inc. All rights reserved.

6

2.4 Control Structures

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

2003 Prentice Hall, Inc. All rights reserved.

8

2.6 if/else Selection Structure

2003 Prentice Hall, Inc. All rights reserved.

9

2.7 The while Repetition Structure

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

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

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

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

 

2003 Prentice Hall, Inc. All rights reserved.

14

2.6 if/else Selection Structure

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";

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.

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.

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.

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.

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.

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.

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.

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.

Multiway if-else

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

– Syntax:

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

Multiway if-else Example

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

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

27

What is wrong, if anything, with the following?

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

if (a < b);

min = a;

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

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

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!