Branching Looping3

download Branching Looping3

of 39

Transcript of Branching Looping3

  • 8/3/2019 Branching Looping3

    1/39

    Branching and Looping Statements

    charturong.ee.engr.tu.ac.th/CN208

    .

  • 8/3/2019 Branching Looping3

    2/39

    2

    Introduction

    MATLAB MATLAB 2 branches (code) loops

    top-down design techniques

    (logical data type)

    branches loops

  • 8/3/2019 Branching Looping3

    3/39

    3

    top-down design

    top-down design

    (input) (output)

    (design the algorithm) MATLAB

  • 8/3/2019 Branching Looping3

    4/39

    4

    (pseudocode)

    pseudocode pseudocode MATLAB MATLAB

    Pseudocode MATLAB

  • 8/3/2019 Branching Looping3

    5/39

    5

    1: Temperature Conversion

    MATLAB

  • 8/3/2019 Branching Looping3

    6/39

    6

    top-down design

    pseudocode Prompt user to enter temperature in degree Fahrenheit

    Read temperature in degrees Fahrenheit (temp_f)

    temp_c in Celsius (temp_f 32) / 1.8

    Write temperature in Celsius

    ? A( ) ( ) 32 /1.8T in Celsius T in Fahrenheit!

  • 8/3/2019 Branching Looping3

    7/39

    7

    temp_conversion.m

    % Script file: temp_conversion

    % Programmer: .

    % Purpose: to convert an input temperature from degrees Fahrenheit

    % to an output temperature in Celsius.

    % Define variables:

    % temp_f -- Temperature in degrees Fahrenheit

    % temp_c -- Temperature in degrees Celsius

    % Prompt the user for the input temperature

    temp_f = input('Enter the temperature in degrees Fahrenheit:');

    % Convert to Celsius.

    temp_c = (temp_f - 32) / 1.8;

    % Write out the result.

    fprintf('%6.2f degrees Fahrenheit = %6.2f Celsius.\n', temp_f, temp_c);

  • 8/3/2019 Branching Looping3

    8/39

    8

    Testing the program

  • 8/3/2019 Branching Looping3

    9/39

    9

    The Logical Data Type

    MATLAB (logical data type) 2 (true) (false) MATLAB 6.5 2 relational operators logicoperators

    logical >> a1 = true;

    true 1 false 0 0 true 0 false

  • 8/3/2019 Branching Looping3

    10/39

    0

    Relational Operators

    Relational ==

    ~=

    =

  • 8/3/2019 Branching Looping3

    11/39

    11

    relational operators

    3 < 4

    3 == 4

    3 ~= 4

    3 4 +10

    3^2 >= 4

    (1) (0)

    (1) (1) (0) (1)

  • 8/3/2019 Branching Looping3

    12/39

    12

    Logic Operators

    logic relational (true)

    (false)logic MATLAB 3

    logic &

    |

    ~

    (AND) (OR)

    (NOT)

  • 8/3/2019 Branching Looping3

    13/39

    13

    logic

    >> x = (5 > 3) & (2 < 9);

    x (true)

    (AND) (OR) (NOT)P Q

    false false

    false true

    true false

    true true

    P & Q P | Q ~P

    false false true

    false true

    false true false

    true true

  • 8/3/2019 Branching Looping3

    14/39

    14

    Branches

    MATLAB

    if switch

    if

    if

    if expression

    (command 1)(command 2)

    end

    MATLAB commands if end

  • 8/3/2019 Branching Looping3

    15/39

    15

    if else if

    else

    if expression

    (commands evaluated if expression is true)

    else

    (commands evaluated if expression is false)

    end

    expression MATLAB commands ( 1 ) if else expression commands else end

  • 8/3/2019 Branching Looping3

    16/39

    16

    elseif

    if elseif

    if expression_1

    (commands_1 evaluated ifexpression_1 is true)

    elseif expression_2

    (commands_2 evaluated ifexpression_2 is true)

    elseif expression_3

    (commands_3 evaluated ifexpression_3 is true)

    else

    (commands evaluated if no other expression is true)

    end

    expression_1 commands_1 ( 1 ) end expression_1 expression_2

    : else commands else

  • 8/3/2019 Branching Looping3

    17/39

    17

    2: function of two variables

    MATLAB f(x,y) x y f(x,y) f(x,y)

    f(x,y) = x + y, x >= 0 and y >= 0x + y2, x >= 0 and y < 0

    x2 + y, x < 0 and y >= 0

    x2 + y2, x < 0 and y < 0

  • 8/3/2019 Branching Looping3

    18/39

    18

    pseudocode Prompt the user for the values x and y

    Read x and y

    ifx >= 0 and y >= 0

    func

    x + yelseifx >= 0 and y < 0

    func x + y^2

    elseifx < 0 and y >= 0

    func x^2 + y

    else

    func x^2 + y^2

    end

    Write out f(x,y)

  • 8/3/2019 Branching Looping3

    19/39

    19

    func_xy.m

    % Script file: func_xy.m

    % Programmer: ...................

    % Purpose: This program solves the function f(x,y) for a user-

    % specified x and y, where f(x, y) is defined as:

    % f(x,y) = x + y, x >= 0 and y >= 0

    % x + y2, x >= 0 and y < 0

    % x2 + y, x < 0 and y >= 0

    % x2 + y2, x < 0 and y < 0

    %

    % Define variables:

    % x -- first independent variable

    % y -- second independent variable% func -- resulting function

  • 8/3/2019 Branching Looping3

    20/39

    20

    % Prompt the user for the values x and y

    x = input('Enter the x coefficient: ');

    y = input('Enter the y coefficient: ');

    % Calculate the function f(x,y)

    if x >= 0 & y >= 0

    func = x + y;

    elseifx >= 0 & y < 0

    func = x + y^2;

    elseifx < 0 & y >= 0

    func = x^2 + y;

    else

    func = x^2 + y^2;

    end

    %Write the value of the function

    disp (['The value of the function is ', num2str(func)]);

  • 8/3/2019 Branching Looping3

    21/39

    21

    switch switch branching

    switch switch switch_varcase case_expression_1

    (commands_1)

    case case_expression_2

    (commands_2)otherwise

    (commands_3)

    end

    switch_var

    case_expression_1

    commands_1

    ( 1 ) end switch_varcase_expression_2 commands_2 case otherwise

    : otherwise

  • 8/3/2019 Branching Looping3

    22/39

    22

    case switch case case case

    (brackets)

    switch switch_var

    case {case_expression_1, case_expression_2}

    (commands_1)case {case_expression_3, case_expression_4}

    (commands_2)

    otherwise

    (commands_3)

    end

    switch_var (double) (character string)

  • 8/3/2019 Branching Looping3

    23/39

    23

    switch 1

    10 switchvalue = input('Choose a integer between 1 and 10: ');

    switch value

    case {1, 3, 5, 7, 9}

    disp('The number is odd.');

    case {2, 4, 6, 8, 10}

    disp('The number is even.');

    otherwise

    disp('The number is out of range.');end

  • 8/3/2019 Branching Looping3

    24/39

    24

    switch char switch

    (character string) x

    x = 2.7;

    units = 'm' ;

    switch unitscase {'inch' , 'in' }

    y = x/2.54;

    case {'feet' , 'ft' }

    y = x/2.54/12;

    case {'meter' , 'm' }y = x/100;

    otherwise

    disp(['Unknown Units: ', units])

    y = nan;

    end

  • 8/3/2019 Branching Looping3

    25/39

    25

    2 for while

    2 while expression for

  • 8/3/2019 Branching Looping3

    26/39

    26

    while

    while expression expression while expression MATLAB end

    while

    while expression

    (commands)

    end

  • 8/3/2019 Branching Looping3

    27/39

    27

    while

    while loop

    sum = input('What is five plus ten?: ');

    while (sum ~= 15)

    disp('Nope! Your answer is incorrect.');

    sum = input('What is five plus ten?: ');

    end

    disp('Great! You are smart.')

  • 8/3/2019 Branching Looping3

    28/39

    28

    for

    for ( 1 ) expression end expression

    forindex = array

    (commands)end

    index (loop variable) array (loop control expression) array index commands array first:incr:last

  • 8/3/2019 Branching Looping3

    29/39

    29

    for

    sum = 0;forii = 1:10

    sum = sum + ii;

    fprintf('sum = %6.2f.\n', sum);

    endfprintf('The value of ii is now %d.\n', ii);

  • 8/3/2019 Branching Looping3

    30/39

    30

    for1. for MATLAB array

    row vector 1 102. 1 ii 1

    array for end sum =sum + ii 0 + 1

    13. fprintf end

    for array ii 2 ii = 2 sum = sum + ii = 1 +2 3

    4. MATLAB 3 array ii commands for end

  • 8/3/2019 Branching Looping3

    31/39

    31

    2 for

    sum = 0;forii = 1:2:9

    sum = sum + 1;

    fprintf('sum = %6.2f.\n', sum);

    endfprintf('The value of ii is now %d.\n', ii);

  • 8/3/2019 Branching Looping3

    32/39

    32

    for

    indexcommands for index for

    for bug

    sum = 0;

    forii = 1:10

    ii = 5;

    sum = sum + ii;

    fprintf('sum = %6.2f.\n', sum);end

    fprintf('The value of ii is now %d.\n', ii);

    for 10 sum

    50

  • 8/3/2019 Branching Looping3

    33/39

    33

    break continue

    2

    while for break continuebreak for while

    MATLAB end

    continue

    for

    while

    MATLAB

  • 8/3/2019 Branching Looping3

    34/39

    34

    breakforii = 1:5

    ifii == 3 break;end

    fprintf('ii = %d\n', ii);

    end

    disp(['End of loop!']);

    m-file run ii = 1

    ii = 2

    End of loop! ii 3 if

    break for end

  • 8/3/2019 Branching Looping3

    35/39

    35

    continueforii = 1:5

    ifii == 3

    continue;end

    fprintf('ii = %d\n', ii);

    end

    disp(['End of loop!']);

    m-file run ii = 1

    ii = 2

    ii = 4

    ii = 5

    End of loop!

    ii 3 ifcontinue for ii 4

  • 8/3/2019 Branching Looping3

    36/39

    36

    Nested Loops

    2 nested loops for

    for (two nested for loops)

    forii = 1:3

    forjj = 1:3

    product = ii * jj;

    fprintf('%d * %d = %d\n', ii, jj, product);

    end

    end

  • 8/3/2019 Branching Looping3

    37/39

    37

    for() 1 ii for() 3 jj

    1, 2, 3 for

    for ii 2 for for ii 3

    1 * 1 = 11 * 2 = 2

    1 * 3 = 3

    2 * 1 = 2

    2 * 2 = 4

    2 * 3 = 6

    3 * 1 = 3

    3 * 2 = 6

    3 * 3 = 9

  • 8/3/2019 Branching Looping3

    38/39

    38

    break continue nested loopsbreak continue

    break continue forii = 1:3

    forjj = 1:3

    ifjj == 3break;

    end

    product = ii * jj;

    fprintf('%d * %d = %d\n', ii, jj, product);

    end

    fprintf('End of inner loop\n');

    end

    fprintf('End of outer loop\n');

  • 8/3/2019 Branching Looping3

    39/39

    39

    jj 3 break End of inner loop ii 1

    1 * 1 = 1

    1 * 2 = 2

    End of inner loop

    2 * 1 = 2

    2 * 2 = 4

    End of inner loop

    3 * 1 = 3

    3 * 2 = 6

    End of inner loop

    End of outer loop