Engineering 1020

21
Engineering 1020 Introduction to Programming Peter King [email protected] www.engr.mun.ca/~peter Winter 2010

description

Engineering 1020. Introduction to Programming Peter King [email protected] www.engr.mun.ca/~peter Winter 2010. ENGI 1020: Update. Midterm Date: Feb 17 th (unchanged) Time: 7pm to 8:15pm This is the same as all other core courses Location: To Be Announced. ENGI 1020: Control Statements. - PowerPoint PPT Presentation

Transcript of Engineering 1020

Page 1: Engineering 1020

Engineering 1020

Introduction to Programming

Peter [email protected]

www.engr.mun.ca/~peter

Winter 2010

Page 2: Engineering 1020

ENGI 1020: Update

• Midterm– Date: Feb 17th (unchanged)

– Time: 7pm to 8:15pm• This is the same as all other core courses

– Location: To Be Announced

Page 3: Engineering 1020

ENGI 1020: Control Statements

• Up to now all our programming has followed the following flow:

– Start program at main– Execute each instruction once– For instructions that are function calls,

• go to the function declaration and start at first instruction

– When instruction is finished, go to next instruction

Page 4: Engineering 1020

ENGI 1020: Control Statements

• How can we solve problems like– Given two numbers, return the largest one

– Find the square root of a number, but only when it's greater than zero

– Given a person's age, output whether they are a child, teenager, adult, or senior

– Given the distance to a wall, tell a robot to stop when it is less than 4m from a wall

Page 5: Engineering 1020

ENGI 1020: Control Statements

• We need to enable the program to make “decisions”

• Or more formally

– Depending on some condition(s), execute alternative sections of code

• At some point in the code, we will choose to execute one block, instead of another

Page 6: Engineering 1020

ENGI 1020: Control Statements

• How is this done you ask?

• The If statement

– “if the door is locked, I will get a key”– “if traffic is bad, I will walk to work”– “if it's later than 9pm, I will go home”

Page 7: Engineering 1020

ENGI 1020: Control Statements

• You've all probably seen this:

CheckBank

Account

Have more than $10k?

Buy Car Get Job

Page 8: Engineering 1020

ENGI 1020: Control Statements

• This is an if statement

• Depending on some condition, we will take a particular path

Have more than $10k?

Page 9: Engineering 1020

ENGI 1020: Control Statements

• Let's see it in C++

• If (some condition) do something

• if (some condition){do somethingdo something… …do something}

Page 10: Engineering 1020

ENGI 1020: Control Statements

• Example

if (x > 0) cout << “x is positive.” << endl;

if (x < 0){cout << “ x is negative.”cout << endl;}

Page 11: Engineering 1020

ENGI 1020: Control Statements

• The if is a keyword• The ( condition ) is an expressions that is

evaluated as either true or false

– x > 0– y != 5– z == 2*y

• When the condition is true, the statement (or block of statements) are executed

• If not true, then the statements are ignored

Page 12: Engineering 1020

ENGI 1020: Control Statements

• Lets look at the conditions– They are boolean expressions– They are evaluated to either true or false

– We can utilize multiple conditions using the • && → and operator• || → or operator

– If x is greater than 5 and y is less than 2, proceedIf (x > 5 && y < 2)

proceed();

Page 13: Engineering 1020

ENGI 1020: Control Statements

What if we want to select one or the other statements, based on a single condition?

“IF there is any 7-up, I'll have that, else I'll have a Sprite”

Page 14: Engineering 1020

ENGI 1020: Control Statements

The if-else statement Picks between two alternatives

if (x >0) Cout << “x is positive” << endl;

else Cout << “x is negative” << endl;

Page 15: Engineering 1020

ENGI 1020: Control Statements

Or Since we know only one of the statements will get

executed

if (x >0) Cout << “x is positive”;

else Cout << “x is negative”;

cout << endl;

Page 16: Engineering 1020

ENGI 1020: Control Statements

If the condition is true We execute under the if

If the condition is not true (false) We execute under the else

Page 17: Engineering 1020

ENGI 1020: Control Statements

We can also nest our if statements What does that mean?

If time is later than 12pm and earlier than 1pm, eat lunch

if (time > 12){If (time < 13){

eatLunch();}

}

Page 18: Engineering 1020

ENGI 1020: Control Statements

Statement blocks after the if can contain any valid code, even other if statements

Page 19: Engineering 1020

ENGI 1020: Control Statements

One more variation Instead of doing this

if (x < 1)doThis();

elseif(x <2)

DoThat();else

if(x <3)DoSomething();

Page 20: Engineering 1020

ENGI 1020: Control Statements

We can do this

if (x < 1)dothis();

else if(x < 2)doThat();

else if(x < 3)doSomthing();

elsedoNothing();

Page 21: Engineering 1020

ENGI 1020: Control Statements

Grading Examples