Control flow statements are essential in programming as they allow you to execute code conditionally. In MATLAB, the primary control flow statements are if, else, elseif, and switch. This section will cover these statements in detail, providing examples and exercises to solidify your understanding.

  1. if Statement

The if statement allows you to execute a block of code only if a specified condition is true.

Syntax

if condition
    % Code to execute if condition is true
end

Example

x = 10;
if x > 5
    disp('x is greater than 5');
end

Explanation

  • x = 10; assigns the value 10 to the variable x.
  • The if statement checks if x is greater than 5.
  • Since the condition is true, the message 'x is greater than 5' is displayed.

  1. else Statement

The else statement allows you to execute a block of code if the condition in the if statement is false.

Syntax

if condition
    % Code to execute if condition is true
else
    % Code to execute if condition is false
end

Example

x = 3;
if x > 5
    disp('x is greater than 5');
else
    disp('x is not greater than 5');
end

Explanation

  • x = 3; assigns the value 3 to the variable x.
  • The if statement checks if x is greater than 5.
  • Since the condition is false, the message 'x is not greater than 5' is displayed.

  1. elseif Statement

The elseif statement allows you to check multiple conditions sequentially.

Syntax

if condition1
    % Code to execute if condition1 is true
elseif condition2
    % Code to execute if condition2 is true
else
    % Code to execute if none of the conditions are true
end

Example

x = 5;
if x > 5
    disp('x is greater than 5');
elseif x == 5
    disp('x is equal to 5');
else
    disp('x is less than 5');
end

Explanation

  • x = 5; assigns the value 5 to the variable x.
  • The if statement checks if x is greater than 5.
  • Since the condition is false, it checks the elseif condition.
  • The elseif condition is true, so the message 'x is equal to 5' is displayed.

  1. switch Statement

The switch statement allows you to execute one block of code from multiple choices based on the value of a variable.

Syntax

switch variable
    case value1
        % Code to execute if variable equals value1
    case value2
        % Code to execute if variable equals value2
    otherwise
        % Code to execute if variable does not match any case
end

Example

day = 'Monday';
switch day
    case 'Monday'
        disp('Start of the work week');
    case 'Friday'
        disp('End of the work week');
    otherwise
        disp('Midweek day');
end

Explanation

  • day = 'Monday'; assigns the string 'Monday' to the variable day.
  • The switch statement checks the value of day.
  • Since day is 'Monday', the message 'Start of the work week' is displayed.

Practical Exercises

Exercise 1

Write a script that checks if a number is positive, negative, or zero and displays an appropriate message.

Solution

number = -3;
if number > 0
    disp('The number is positive');
elseif number < 0
    disp('The number is negative');
else
    disp('The number is zero');
end

Exercise 2

Write a script that assigns a grade based on a score using the switch statement. The grading criteria are:

  • A: 90-100
  • B: 80-89
  • C: 70-79
  • D: 60-69
  • F: below 60

Solution

score = 85;
switch true
    case score >= 90
        disp('Grade: A');
    case score >= 80
        disp('Grade: B');
    case score >= 70
        disp('Grade: C');
    case score >= 60
        disp('Grade: D');
    otherwise
        disp('Grade: F');
end

Common Mistakes and Tips

  • Common Mistake: Forgetting to use end to close the if, elseif, else, and switch blocks.
    • Tip: Always ensure that each control flow block is properly closed with end.
  • Common Mistake: Using = instead of == for comparison.
    • Tip: Remember that = is for assignment, while == is for comparison.

Conclusion

In this section, you learned how to use if, else, elseif, and switch statements to control the flow of your MATLAB programs. These constructs allow you to execute code conditionally, making your programs more dynamic and responsive to different inputs. Practice the exercises provided to reinforce your understanding, and you'll be well-prepared for more complex programming tasks.

© Copyright 2024. All rights reserved