Engineering 1020 Introduction to Programming Peter King peter

  • Slides: 21
Download presentation
Engineering 1020 Introduction to Programming Peter King peter. king@mun. ca www. engr. mun. ca/~peter

Engineering 1020 Introduction to Programming Peter King peter. king@mun. ca www. engr. mun. ca/~peter Winter 2010

ENGI 1020: Update • Midterm – Date: Feb 17 th (unchanged) – Time: 7

ENGI 1020: Update • Midterm – Date: Feb 17 th (unchanged) – Time: 7 pm to 8: 15 pm – • 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

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

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 4 m from a wall

ENGI 1020: Control Statements • We need to enable the program to make “decisions”

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

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 9 pm, I will go home”

ENGI 1020: Control Statements • You've all probably seen this: Check Bank Account Have

ENGI 1020: Control Statements • You've all probably seen this: Check Bank Account Have more than $10 k? Buy Car Get Job

ENGI 1020: Control Statements • This is an if statement Have more than $10

ENGI 1020: Control Statements • This is an if statement Have more than $10 k? • Depending on some condition, we will take a particular path

ENGI 1020: Control Statements • Let's see it in C++ • If (some condition)

ENGI 1020: Control Statements • Let's see it in C++ • If (some condition) do something • if (some condition){ do something …… do something }

ENGI 1020: Control Statements • Example if (x > 0) cout << “x is

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

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

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

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)

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

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

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

ENGI 1020: Control Statements We can also nest our if statements What does that mean? If time is later than 12 pm and earlier than 1 pm, eat lunch if (time > 12){ If (time < 13){ eat. Lunch(); } }

ENGI 1020: Control Statements Statement blocks after the if can contain any valid code,

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 <

ENGI 1020: Control Statements One more variation Instead of doing this if (x < 1) do. This(); else if(x <2) Do. That(); else if(x <3) Do. Something();

ENGI 1020: Control Statements We can do this if (x < 1) dothis(); else

ENGI 1020: Control Statements We can do this if (x < 1) dothis(); else if(x < 2) do. That(); else if(x < 3) do. Somthing(); else do. Nothing();

ENGI 1020: Control Statements Grading Examples

ENGI 1020: Control Statements Grading Examples