Top Banner

of 37

12 - Control Structure

Apr 06, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/2/2019 12 - Control Structure

    1/37

    Operators and ControlStructures

  • 8/2/2019 12 - Control Structure

    2/37

    On the menu today

    I- Quick reviewII- Relational & logical operators

    III- Control Structure

  • 8/2/2019 12 - Control Structure

    3/37

    I- Review

  • 8/2/2019 12 - Control Structure

    4/37

    A- Variables declaration

    What is required?

    Three rules to declare a variable

    Good habits for a variable name

  • 8/2/2019 12 - Control Structure

    5/37

    How can we get a value into this variable

    B- Variables assignment

    1- variable = expression;it means: variable expression

    (value of the expression assigned to

    the variable)

  • 8/2/2019 12 - Control Structure

    6/37

    C- Types of expression

    Provide the different types of expression:

    - numeric constant

    - another variable- arithmetic expression

    operand operator operand

  • 8/2/2019 12 - Control Structure

    7/37

    II- Relational & logical operators

  • 8/2/2019 12 - Control Structure

    8/37

    A- Relational operators

    Goal: Compare or Write conditions

    Relational expression: operand operator operand

    Different operators

    < less than> greater than

    = greater than or equal to== equal to

    != not equal to

  • 8/2/2019 12 - Control Structure

    9/37

    A- Relational operators: example

    Relational expressions

    - (4 < 3) less than

    - (firstInitial != 'A') not equal

    - (hoursWorked > 40) greater than

    - (pollutionIndex >= cutoff) greater than or equal

    - (age

  • 8/2/2019 12 - Control Structure

    10/37

    B- Logical operators

    Goal: Create more complex conditions

    && and

    || or

    ! not

  • 8/2/2019 12 - Control Structure

    11/37

    B- Logical operators: Example

    Logic expressions

    (age > 40) && (height > 1.75)

    (age > 40) || (height > 1.75) || (weight > 80)

    ! ((age > 40) && (height > 1.75))

  • 8/2/2019 12 - Control Structure

    12/37

    left to right

    * / %

    + -

    < >=

    == !=

    &&||

    C- Operators precedence

  • 8/2/2019 12 - Control Structure

    13/37

    C- Expression value and interpretation

    relational and logical expression take

    value 1 0

    interpreted in true false

  • 8/2/2019 12 - Control Structure

    14/37

    C- Expression value andinterpretation: Example

    int i = 5, j = 7, k = 12;

    Expression equivalent value interpretationexpression

    i+1==k+1 (i+1)==(k+1) 0 false

    3*1-jk 1 true

    k+3

  • 8/2/2019 12 - Control Structure

    15/37

  • 8/2/2019 12 - Control Structure

    16/37

    Lecture 4

    II- Program Control

    Structure

  • 8/2/2019 12 - Control Structure

    17/37

    A- Syntax form

    //Syntax form of a C++ program# include

    definitions, declarations and

    functions

    void main()

    {declarations and statements

    }

  • 8/2/2019 12 - Control Structure

    18/37

    B- Control Structure: what for?

    We can think of a program as a set ofstatements that manipulate variables.

    How to control the order in which thosestatements execute?

    Goal: Control the order of thestatements

  • 8/2/2019 12 - Control Structure

    19/37

    B- Control Structure: 3 Types

    Sequences:

    By default, they execute sequentially

    Selection : Condition - DecisionWe can force them to execute selectivelyconditionally

    Iteration: LoopWe can force them to execute iteratively

  • 8/2/2019 12 - Control Structure

    20/37

    C- Sequences# include

    void main()

    {

    float a, b, c;

    a = 12.0;b = 15.0;

    c = (a + b) / (a - b);

    cout

  • 8/2/2019 12 - Control Structure

    21/37

    Does the order of statements matter ?

    C- Sequences

    Yes, the statements are executed insequence.

  • 8/2/2019 12 - Control Structure

    22/37

    C- Selection

    - Sometimes we only want to execute astatement under certain conditions.

    - That is, we want to selectivelyexecute astatement or a series of statements.

    C S

  • 8/2/2019 12 - Control Structure

    23/37

    -We informally think of this as an "if...then"situation:

    if (condition)statements to execute if condition is true

    else

    statements to execute if condition is false

    C- Selection

    C S l i

  • 8/2/2019 12 - Control Structure

    24/37

    C- Selection- Flow diagrams are good for capturing this idea:

    - The condition states a question whose answer isYes/True or No/False.

    condition

    Statement

    to be executed

    when condition

    is false

    Statement

    to be executed

    when condition

    is true

    N Y

    C S l i l

  • 8/2/2019 12 - Control Structure

    25/37

    if hours worked is greater than 40

    compute regular hours as 40

    compute overtime hours as hours worked - 40

    compute earnings as regular hours * wage rateplus overtime hours * 1.5 * wage rate

    otherwise

    compute earnings as hours worked * wage rate

    C- Selection: example

    C S l i l

  • 8/2/2019 12 - Control Structure

    26/37

    C- Selection: example

    hrsworked > 40

    ?

    earnings hrsworked* wageRate

    reghours 40

    othours hrsworked -40

    earning reghours *wageRate

    + othours*1.5* wageRate

    N Y

    C S l i l

  • 8/2/2019 12 - Control Structure

    27/37

    C- Selection: example

    Scenario: Three pollution levels called reading1,reading2, and reading3. An air pollution index isdefined as the average of these readings. Any air

    pollution index less than the cutoff means air quality issafe; otherwise it is hazardous.

    Assume values already in all four variables:reading1,reading2, reading3, and cutoff.

    C S l ti l

  • 8/2/2019 12 - Control Structure

    28/37

    C- Selection: example

    Example:

    float Index;

    Index = (reading1+ reading2+ reading3)/3;if (Index < cutoff)

    cout

  • 8/2/2019 12 - Control Structure

    29/37

    C- Selection - ifStatement : twotypes

    condition

    Statements

    N Y

    if (condition){

    statements;

    }

    C S l ti if

  • 8/2/2019 12 - Control Structure

    30/37

    condition

    Statements

    N Y if (condition){

    statements;}

    else

    {statements;

    }

    Statements

    C- Selection - ifStatement : two types

    D It ti

  • 8/2/2019 12 - Control Structure

    31/37

    D- Iteration: why we need loops

    Let's write a program to print out

    a chart of the first 10 positive integers

    and their squares.

    D It ti

  • 8/2/2019 12 - Control Structure

    32/37

    Lecture 4

    D- Iteration: why we need loops

    #include void main(){

    cout

  • 8/2/2019 12 - Control Structure

    33/37

    D- Iteration: why we need loops

    Observations?

    D It ti h d l

  • 8/2/2019 12 - Control Structure

    34/37

    D- Iteration: why we need loopsModified version:

    #include void main(){

    int x = 1;cout

  • 8/2/2019 12 - Control Structure

    35/37

    Observations?

    We are repeating exactlythe same two statementsten times.

    Alternative?x 0;loop while x < 10

    add 1 to xreport x and x2

    end loop

    D- Iteration: why we need loops

    D Iteration h d l

  • 8/2/2019 12 - Control Structure

    36/37

    D- Iteration: why we need loops

    Observations?

    a) we have found the patternandonly written that

    b) we have let the computer do thework

  • 8/2/2019 12 - Control Structure

    37/37

    Questions?