E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS - PowerPoint PPT Presentation

1 / 12
About This Presentation
Title:

E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS

Description:

case (value1) Execute command group 1. case (value2) Execute command group 2. otherwise ... VALUE1. VALUE= VALUE2. COMMAND. GROUP 1. COMMAND. GROUP 2. COMMAND ... – PowerPoint PPT presentation

Number of Views:21
Avg rating:3.0/5.0
Slides: 13
Provided by: pack3
Category:

less

Transcript and Presenter's Notes

Title: E77: INTRODUCTION TO COMPUTER PROGRAMMING FOR SCIENTISTS AND ENGINEERS


1
E77 INTRODUCTION TO COMPUTER PROGRAMMING FOR
SCIENTISTS AND ENGINEERS
  • Lecture Outline
  • 1. The switch construct
  • 2. Definite loops
  • 3. Conditional loops

2
Switch construct
  • 1. Switch construct
  • This is a conditional construct alternative to
    the
  • if-elseif-else-end construct. Its general form
    is
  • switch (input expression)
  • case (value1)
  • Execute command group 1
  • case (value2)
  • Execute command group 2
  • otherwise
  • Execute command group N
  • end

3
Switch construct
  • Schematically

FALSE
FALSE
VALUEVALUE2
VALUEVALUE1
TRUE
TRUE
COMMANDGROUP 2
COMMANDGROUP 1
COMMANDGROUP N
4
Switch construct
  • By way of background, recall the general form of
    the
  • if-elseif-else-end construct
  • if (logical expression 1)
  • Execute command group 1
  • elseif (logical expression 2)
  • Execute command group 2
  • else
  • Execute command group N
  • end

5
Switch construct
  • Compare the if-elseif-else-end
  • to switch-case-otherwise-end
  • Both constructs allow branching of the code.
  • In both constructs, only one group of commands is
    eventually executed
  • In both constructs, the comparisons are made
    sequentially from top to bottom
  • Unlike if-elseif-else-end, the decision to
    execute a command group in switch-case-otherwise-e
    nd is based on the value of a single variable

6
Definite loops
  • 2. Definite loops
  • Definite loops allow a group of commands to be
    repeatedly
  • executed a fixed and given number of times.
  • The general form of the definite loop construct
    in MATLAB
  • is
  • for loop_variable array
  • Execute command group for given value of
    loop_variable
  • end
  • Here the command group is executed once for each
    column
  • of the array array. During the k-th execution,
  • loop_variable array(,k)

7
Definite loops
  • Schematic depiction of a definite loop construct

k 1
variable array( , k )
k k 1
COMMANDGROUP
k gtarraycolumns
FALSE
TRUE
8
Definite loops
  • Remarks on definite loops
  • It is a dangerous practice to change the value of
    the loop variable during the execution of the
    command group.
  • MATLAB is not particularly efficient in executing
    definite loops (there are ways to address this
    problem).
  • Several MATLAB commands already perform definite
    loops, albeit efficiently (e.g., , find, sum,
    sort).
  • Definite loops can be nested.
  • It is possible (although not recommended, to
    break a definite loop using the break command).

9
Conditional loops
  • 3. Conditional loops
  • Conditional (or indefinite) loops allow a group
    of commands
  • to be repeatedly executed as long as a logical
    expression is
  • true.
  • The general form of the conditional loop
    construct in
  • MATLAB is
  • while (logical expression)
  • Execute command group
  • end
  • Conditional loops are useful when the exact
    number of
  • repetitive executions is not known in advance

10
Conditional loops
  • Schematic depiction of a conditional loop
    construct

FALSE
LOGICALEXPRESSION
TRUE
COMMANDGROUP
11
Conditional loops
  • Remarks on conditional loops
  • The logical variable needs to be defined before
    execution of the while statement.
  • The logical expression in the while statement is
    reevaluated after each loop and needs to be
    changed at some stage during the execution of the
    command group.
  • Care is needed to ensure that the while loop is
    not infinite.
  • MATLAB is not particularly efficient in executing
    conditional loops (there are ways to address this
    problem).
  • Conditional loops can be nested.

12
Summary
  • What did we learn today?
  • The switch construct is ideal for branching of a
    code based on the value of a single expression.
  • Definite loops (for loops) are useful when
    executing the same group of commands a fixed and
    given number of times.
  • Conditional loops (while loops) are useful when
    executing the same group of commands repeatedly
    without knowing in advance the total number of
    repetitions.
  • Loops are expensive in MATLAB and can be often
    avoided by relying on built-in functions.
Write a Comment
User Comments (0)
About PowerShow.com