Engineering 1020
This presentation is the property of its rightful owner.
Sponsored Links
1 / 21

Engineering 1020 PowerPoint PPT Presentation


  • 110 Views
  • Uploaded on
  • Presentation posted in: General

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.

Download Presentation

Engineering 1020

An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript


Engineering 1020

Engineering 1020

Introduction to Programming

Peter King

[email protected]

www.engr.mun.ca/~peter

Winter 2010


Engi 1020 update

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

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 statements1

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 statements2

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 statements3

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 statements4

ENGI 1020: Control Statements

  • You've all probably seen this:

CheckBank

Account

Have more than

$10k?

Buy Car

Get Job


Engi 1020 control statements5

ENGI 1020: Control Statements

  • This is an if statement

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

Have more than

$10k?


Engi 1020 control statements6

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 statements7

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 statements8

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 statements9

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 statements10

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 statements11

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 statements12

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 statements13

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 statements14

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 statements15

ENGI 1020: Control Statements

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


Engi 1020 control statements16

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 statements17

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 statements18

ENGI 1020: Control Statements

  • Grading Examples


  • Login