Skip to main content
Logo image

Exercises πŸ€”πŸ’­ Conceptual Questions

1.

(a) If Statement Requirements.

    Every if statement in MATLAB must end with the keyword end.
  • True.

  • Correct! The end keyword is required to close every if statement, whether it contains just an if block, an if-else, or an if-elseif-else chain.
  • False.

  • Correct! The end keyword is required to close every if statement, whether it contains just an if block, an if-else, or an if-elseif-else chain.

(b) Else Branch Requirement.

    An else branch is required in every if statement.
  • True.

  • Incorrect. The else branch is optional. If you only need to run code when a condition is true, you can use just an if block without an else.
  • False.

  • Incorrect. The else branch is optional. If you only need to run code when a condition is true, you can use just an if block without an else.

(c) Elseif Chain Evaluation Order.

In an if-elseif-else chain, how does MATLAB determine which block to execute?
  • MATLAB checks all conditions and runs the block with the highest priority.
  • Incorrect. MATLAB evaluates conditions in order from top to bottom and stops at the first true condition.
  • MATLAB checks conditions from top to bottom and runs the first true block.
  • Correct! Once MATLAB finds a true condition, it executes that block and skips all remaining branches.
  • MATLAB checks all conditions and runs all blocks whose conditions are true.
  • Incorrect. Only one block executes in an if-elseif-else chain, even if multiple conditions are true.
  • MATLAB evaluates conditions randomly.
  • Incorrect. Conditions are always evaluated in the order they appear in the code.

(d) Switch Case Values.

Which of the following statements about switch case values are true? Select all that apply.
  • The otherwise block is optional.
  • In a switch statement the otherwise block is optional.
  • Case values can be numbers.
  • Correct! Switch cases can match numeric values.
  • Case values can be strings.
  • Correct! Switch cases work well with string comparisons.
  • Case values can use relational operators like > or <.
  • Incorrect. Switch statements only test for equality. Use if-elseif for range-based conditions.
  • Multiple case values can be grouped using curly braces { }.
  • Correct! You can use case {val1, val2, val3} to match multiple values.

(e) Otherwise Branch Behavior.

    In a switch statement, the otherwise block only runs when none of the case values match.
  • True.

  • Correct! The otherwise branch is similar to the else in an if statementβ€”it provides a default action when no cases match.
  • False.

  • Correct! The otherwise branch is similar to the else in an if statementβ€”it provides a default action when no cases match.

(f) Switch Case Sensitivity.

Consider this code:
status = 'active';
switch status
	case 'Active'
		disp('User is active')
	case 'active'
		disp('User is online')
	otherwise
		disp('Unknown status')
end
What will be displayed?
  • User is active
  • Incorrect. Case values are case-sensitive, so 'Active' does not match 'active'.
  • User is online
  • Correct! The string 'active' exactly matches the second case. Switch comparisons are case-sensitive.
  • Unknown status
  • Incorrect. There is a case that matches: the second case 'active'.
  • An error will occur.
  • Incorrect. This is valid code; the second case matches and executes.

(g) When to Use Switch vs If.

Which scenario is best suited for a switch statement rather than an if-elseif chain?
  • Comparing a variable to a list of specific string values.
  • Correct! Switch statements are ideal for testing exact matches against discrete values, especially strings.
  • Checking if a number falls within different ranges (e.g., 0-10, 10-20).
  • Incorrect. Range-based conditions require relational operators like > or <=, which switch statements cannot handle. Use if-elseif instead.
  • Testing multiple logical conditions that use AND (&) or OR (|).
  • Incorrect. Switch statements only test for equality. Use if-elseif for complex logical conditions.
  • Checking if a value is greater than a threshold.
  • Incorrect. This requires a relational operator (>), which switch statements cannot use. Use an if statement.

(h) How Many Blocks Execute?

Given this code:
x = 10;
if x > 5
	y = 1;
elseif x > 8
	y = 2;
elseif x > 3
	y = 3;
else
	y = 0;
end
What is the value of y after this code executes?
  • y = 2
  • Incorrect. The first condition (x > 5) is true, so MATLAB never checks the second condition.
  • y = 3
  • Incorrect. The first condition (x > 5) is true, so MATLAB never checks the third condition.
  • y = 6 (sum of 1+2+3)
  • Incorrect. Only one block executes in an if-elseif-else chain. The blocks don’t accumulate.
  • y = 1
  • Correct! Even though x > 8 and x > 3 are also true, MATLAB stops at the first true condition (x > 5) and assigns y = 1.

(i) Nested If vs Elseif Chain.

What is the main difference between using nested if statements and using an if-elseif chain?
  • There is no difference; they are equivalent in all cases.
  • Incorrect. Nested if statements can check multiple conditions independently, while elseif chains are mutually exclusive.
  • Nested if statements run faster than elseif chains.
  • Incorrect. Performance is not the key difference; the distinction is in the logic structure and whether branches are mutually exclusive.
  • Nested if statements can allow multiple blocks to execute; elseif chains execute only one block.
  • Correct! In an if-elseif-else chain, only one block runs. With nested if statements, multiple independent conditions can be true.
  • Elseif chains require more end keywords than nested if statements.
  • Incorrect. Actually, nested if statements require more end keywords (one for each if), while an elseif chain needs only one.

(j) Duplicate Case Values.

What happens if a switch statement contains duplicate case values?
  • MATLAB runs both matching cases.
  • Incorrect. MATLAB will actually report an error if you try to use duplicate case values.
  • MATLAB runs the first matching case and ignores the duplicate.
  • Incorrect. MATLAB doesn’t allow duplicate case values at all.
  • MATLAB uses the last duplicate case value.
  • Incorrect. MATLAB won’t allow duplicate case values at all.
  • MATLAB produces an error.
  • Correct! Duplicate case values are not allowed in MATLAB switch statements and will cause an error.

(k) If Statement Condition Type.

Consider this code:
x = 5;
if x
	disp('x is true')
else
	disp('x is false')
end
What will be displayed, and why?
  • x is true, because any non-zero number is treated as true.
  • Correct! In MATLAB, non-zero values are treated as true in conditional statements, and zero is treated as false.
  • x is false, because x is not a logical value.
  • Incorrect. While x is numeric, MATLAB automatically converts non-zero numbers to true in conditional statements.
  • An error occurs because x is not a logical value.
  • Incorrect. MATLAB allows numeric values in if conditions and treats non-zero as true and zero as false.
  • x is false, because 5 is not equal to true.
  • Incorrect. MATLAB doesn’t compare x to true; it converts non-zero values to true.