SLIDE 1
Conditional Programming The if-statement: if condition statements - - PDF document
Conditional Programming The if-statement: if condition statements - - PDF document
TDDD34 - Lecture 2 1/7 Conditional Programming The if-statement: if condition statements end Example 1: choice = input('Enter a number: '); some_text = 'wrong'; if choice == 42 some_text = 'right'; end disp(['You guessed the ',
SLIDE 2
SLIDE 3
TDDD34 - Lecture 2 3/7
The Switch-statement: switch expression case value statements case value statements case {value, value} statements
- therwise
statements end Example
choice = input('Enter a number: '); switch choice case {1 , 9 , 42 , 111} disp('That is one of my favorite numbers!'); case {-1 , 13 , 88} disp('That is a number I do not like!');
- therwise
disp('I am indifferent to that number...'); end
SLIDE 4
TDDD34 - Lecture 2 4/7
The for loop: for control_variable = interval statements end Example:
for i = 1:4 disp(['The control variable is ', num2str(i)]); end The output will be: The control variable is 1 The control variable is 2 The control variable is 3 The control variable is 4
SLIDE 5
TDDD34 - Lecture 2 5/7
The while loop while condition statements end Example: while true disp('I love you!'); end
The output is: I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! I love you! (Continues indefinitely)
SLIDE 6
TDDD34 - Lecture 2 6/7
Break: while true statements if condition break; end end Continue: while true if condition continue; end statements end Advice: Avoid break and continue if you can...
SLIDE 7