Flow control

Flow control
Conditionals
(& the 2 brothers problem)
Conditionals
if
 condition
 
do this stuff
end
if
 condition
 
do this stuff
else
 
do this stuff
end
if
 condition
 
do this stuff
elseif 
condition
 
do this stuff
else
 
do this stuff
end
Conditionals
if
 condition
 
do this stuff
end
 
condition
 should evaluate to 
logical
true or false
.  examples:
 
x > 5
 
y == 5
 
x<3 & y>=7
 
strcmp(subject,’S01’)
Flow Control (IF statement)
d=10;
if d<5
    'less than 5'
else
    'not less than 5'
end
 
output?
 
d=10
 
True?
 
no
 
Skip to
here
 
end
 
The 
if
 statement evaluates a logical expression and executes a group of
statements when the expression is 
TRUE
. The optional 
elseif
 and 
else
keywords provide for the execution of alternate groups of statements. An
end
 keyword, terminates the last group of statements
Example: for different values of A
 
If (A > B)   
 
% Test for condition A > B
  
disp('A is greater than B'); % disp writes string argument to screen
 
elseif  (A < B)     % Test for condition A < B
  
disp('A is less than B');
 
elseif (A == B)   % Note that == means test for equality
  
disp('A and B are equal');
 
else
  
disp('Rather unexpected situation.... ')
 
end
Flow Control (IF statement)
The 
if
 statement evaluates a logical expression and executes a group of
statements when the expression is TRUE. The optional 
elseif
 and 
else
keywords provide for the execution of alternate groups of statements. An
end
 keyword, terminates the last group of statements
Example: for different values of A
 
If (A > B)   
 
% Test for condition A > B
 
  
disp('A is greater than B'); % disp writes string argument to screen
 
elseif  (A < B)     % Test for condition A < B
  
disp('A is less than B');
 
elseif (A == B)   % Note that == means test for equality
  
disp('A and B are equal');
 
else
  
disp('Rather unexpected situation.... ')
 
end
function 
isItBigger(x,y)
% will decide if x is bigger than y
if 
x > y
    
fprintf(
'Yes, x is bigger than y.\n'
);
else
    
fprintf(
'No, x is not bigger than y.\n'
);
end
>> 
isItBigger(3,4)
No, x is not bigger than y.
>> 
isItBigger(8,4)
Yes, x is bigger than y.
function 
isItNine(x)
% will decide if x is equal to 9
if 
x = 9
    
fprintf(
’It is nine!\n'
);
else
    
fprintf(
'No, its not nine.\n'
);
end
function 
isItNine(x)
% will decide if x is equal to 9
if 
x == 9
    
fprintf(
’It is nine!\n'
);
else
    
fprintf(
'No, its not nine.\n'
);
end
function 
fruit = 
pickAFruit(color,size)
% choose a fruit based on color and size
% color is a string, and size is a double representing weight in grams
if 
strcmp(color,
’red
’)
 
if 
size < 10
  
fruit = 
‘apple’
;
 
else
  
fruit = 
‘watermelon’
;
 
end
  
elseif 
strcmp(color,
’yellow’
)
 
fruit = 
‘banana’
;
else
 
fruit = 
‘unknown’
;
end
Nested conditionals
Switch and Case Statements
The 
switch
 statement executes one of a group of
case 
statements, based on whether the value of a
variable (logicals included) is the same in both. Only
the first match is executed. If no match is found,
otherwise
 is executed.
% In this example, need first to specify a 
positive integer value for n
 
switch (rem(n,2)) % use help “rem”
 
    case 0
 
          disp('n is even');
 
    case 1
 
           disp('n is odd')
 
    otherwise
 
       error('This seems hard to believe')
 
 end
Useful videos
https://www.youtube.com/watch?v=rzm_FyZ9SmA
(Selection (Branching) in MATLAB - 12min)
https://www.youtube.com/watch?v=rEarTHsM5dY
(If-Statement cont'd – 8min)
https://www.youtube.com/watch?v=vJpeMFfwpTM
(Nested If-statements – 2min)
Slide Note
Embed
Share

Diving into flow control and conditionals in programming, this content discusses various concepts such as if statements, logical evaluations, and nested conditionals. Explore examples and practical implementations to enhance your understanding of how to control the flow of a program based on conditions.

  • Programming
  • Flow Control
  • Conditionals
  • Logical Evaluation
  • Nested Conditionals

Uploaded on Feb 15, 2025 | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. Flow control

  2. Conditionals (& the 2 brothers problem)

  3. Conditionals if condition if condition if condition do this stuff do this stuff do this stuff end elseif condition else do this stuff do this stuff else end do this stuff end

  4. Conditionals condition should evaluate to logical true or false. examples: x > 5 y == 5 x<3 & y>=7 strcmp(subject, S01 ) if condition end do this stuff

  5. Flow Control (IF statement) d=10; d=10 if d<5 'less than 5' else 'not less than 5' end True? no Skip to here output? end

  6. Flow Control (IF statement) The if statement evaluates a logical expression and executes a group of statements when the expression is TRUE. The optional elseif and else keywords provide for the execution of alternate groups of statements. An end keyword, terminates the last group of statements end keyword, terminates the last group of statements The if statement evaluates a logical expression and executes a group of statements when the expression is TRUE. The optional elseif and else keywords provide for the execution of alternate groups of statements. An Example: for different values of A If (A > B) disp('A is greater than B'); % disp writes string argument to screen elseif (A < B) % Test for condition A < B disp('A is less than B'); elseif (A == B) % Note that == means test for equality disp('A and B are equal'); else disp('Rather unexpected situation.... ') end end Example: for different values of A If (A > B) % Test for condition A > B % Test for condition A > B disp('A is greater than B'); % disp writes string argument to screen elseif (A < B) % Test for condition A < B disp('A is less than B'); elseif (A == B) % Note that == means test for equality disp('A and B are equal'); else disp('Rather unexpected situation.... ')

  7. function isItBigger(x,y) % will decide if x is bigger than y if x > y fprintf('Yes, x is bigger than y.\n'); else fprintf('No, x is not bigger than y.\n'); end >> isItBigger(3,4) No, x is not bigger than y. >> isItBigger(8,4) Yes, x is bigger than y. function isItNine(x) % will decide if x is equal to 9 if x = 9 fprintf( It is nine!\n'); else fprintf('No, its not nine.\n'); end function isItNine(x) % will decide if x is equal to 9 if x == 9 fprintf( It is nine!\n'); else fprintf('No, its not nine.\n'); end

  8. Nested conditionals function fruit = pickAFruit(color,size) % choose a fruit based on color and size % color is a string, and size is a double representing weight in grams if strcmp(color, red ) if size < 10 else end fruit = apple ; fruit = watermelon ; elseif strcmp(color, yellow ) fruit = banana ; else fruit = unknown ; end

  9. Switch and Case Statements The switch statement executes one of a group of case statements, based on whether the value of a variable (logicals included) is the same in both. Only the first match is executed. If no match is found, otherwise is executed. % In this example, need first to specify a positive integer value for n switch (rem(n,2)) % use help rem case 0 disp('n is even'); case 1 disp('n is odd') otherwise error('This seems hard to believe') end

  10. Useful videos https://www.youtube.com/watch?v=rzm_FyZ9SmA (Selection (Branching) in MATLAB - 12min) https://www.youtube.com/watch?v=rEarTHsM5dY (If-Statement cont'd 8min) https://www.youtube.com/watch?v=vJpeMFfwpTM (Nested If-statements 2min)

More Related Content

giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#giItT1WQy@!-/#