You are on page 1of 22

University of Dar es Salaam

ES171: Computer Aided


Drafting and Design (CADD)

Unit IV: Introduction to MATLAB


University of Dar es Salaam

LECTURE 4
 Input/Output Prompt
 Control Statements
 Loop Statements
University of Dar es Salaam
THE INPUT PROMPT

• The input function is used for requesting for users for prompt
inputs from keyboard
• Syntax
x=input(‘enter value of x ’)
• Displays the text in prompt and waits for the user to input a
value and press the Return key
University of Dar es Salaam
THE INPUT PROMPT

Example
University of Dar es Salaam
DISPLAY FUNCTIONS

• MATLAB can display values, text, or any content of variables


without printing their variables

• Example: with disp function


University of Dar es Salaam
DISPLAY FUNCTIONS

• The msgbox function creates a pop up message dialog box


• Example
University of Dar es Salaam
CONTROL STRUCTURES

MATLAB has four basic control structures


• Two for deciding (control statements)
• If statements
• Switch statements
• Two for repeating (looping/iteration)
• While loops
• For loops
University of Dar es Salaam
IF, ELSE, ELSEIF

• The condition statement evaluates TRUE or FALSE


• Command blocks are between reserved words
• The statements will rearrange themselves accordingly
University of Dar es Salaam
IF, ELSE, ELSEIF ELSEIF
if condition
• Syntax ELSE commands
if condition elseif condition
IF commands commands
if condition else else
commands commands commands
end end end
University of Dar es Salaam
IF, ELSE, ELSEIF

• if – tests one condition and returns answer if the condition is TRUE


• if else – tests one condition and returns answers if the condition is
TRUE and another answer if the condition is FALSE
• if elseif…. else – tests multiple conditions and returns answers for
the TRUE condition and another answer for FALSE condition
University of Dar es Salaam
IF, ELSE, ELSEIF

Examples..
University of Dar es Salaam
IF, ELSE, ELSEIF

Examples..
University of Dar es Salaam
IF, ELSE, ELSEIF

Examples..
University of Dar es Salaam
SWITCH, CASE, OTHERWISE
Syntax…
• Execute one of several groups switch expression
of statements case expression
statements
• Switch expression must be a case expression
scalar or string constant statements
otherwise
statements
end
University of Dar es Salaam
SWITCH, CASE, OTHERWISE

• Example

• Read other examples


from doc switch
University of Dar es Salaam
FOR LOOP
• Used for known number of • Syntax
iterations for index=value
• The loop variable is defined as Commands
a vector and its scalar end
elements are executed within
the command block
• Commands are everything
between the for and end lines
University of Dar es Salaam
FOR LOOP
• Examples…
University of Dar es Salaam
FOR LOOP
• Examples…
Nested for loop

• Read other examples


from doc for
University of Dar es Salaam
WHILE LOOP
• Used for unknown number of • Syntax
iterations while condition
• The command block will Commands
execute as long as the condition end
expression is TRUE
• Beware of infinite loops (press
Ctrl+C to interrupt)
University of Dar es Salaam
WHILE LOOP
• Examples…
University of Dar es Salaam
WHILE LOOP
• Examples…
Calculate 10 factorial using
while loop

Read other examples from doc


while
University of Dar es Salaam
Reading Assignment

1. An Introduction to Matlab Version 3.1 David F. Griffiths


Univ. of Dundee : Section 18 – 20
2. Matlab getting started - Matlab Primer R2016B – Chapter 5
3. Mastering MATLAB (Hanselman & Littlefield) – Chapter 11:
Control Flow

You might also like