ENGI 1020: Control Statements

advertisement
Engineering 1020
Introduction to Programming
Peter King
peter.king@mun.ca
www.engr.mun.ca/~peter
Winter 2010
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
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
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
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
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”
ENGI 1020: Control Statements
•
You've all probably seen this:
Check
Bank
Account
Have more than
$10k?
Buy Car
Get Job
ENGI 1020: Control Statements
•
This is an if statement
Have more than
$10k?
•
Depending on some condition, we will take a
particular path
ENGI 1020: Control Statements
•
Let's see it in C++
• If (some condition)
do something
• if (some condition){
do something
do something
……
do something
}
ENGI 1020: Control Statements
•
Example
if (x > 0)
cout << “x is positive.” << endl;
if (x < 0){
cout << “ x is negative.”
cout << endl;
}
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
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, proceed
If (x > 5 && y < 2)
proceed();
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”
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;
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;
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
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();
}
}
ENGI 1020: Control Statements

Statement blocks after the if can contain any valid
code, even other if statements
ENGI 1020: Control Statements

One more variation

Instead of doing this
if (x < 1)
doThis();
else
if(x <2)
DoThat();
else
if(x <3)
DoSomething();
ENGI 1020: Control Statements

We can do this
if (x < 1)
dothis();
else if(x < 2)
doThat();
else if(x < 3)
doSomthing();
else
doNothing();
ENGI 1020: Control Statements

Grading Examples
Download