1.
(a) If Statement Requirements.
True.
- Correct! The
endkeyword is required to close everyifstatement, whether it contains just anifblock, anif-else, or anif-elseif-elsechain. False.
- Correct! The
endkeyword is required to close everyifstatement, whether it contains just anifblock, anif-else, or anif-elseif-elsechain.
(b) Else Branch Requirement.
True.
- Incorrect. The
elsebranch is optional. If you only need to run code when a condition is true, you can use just anifblock without anelse. False.
- Incorrect. The
elsebranch is optional. If you only need to run code when a condition is true, you can use just anifblock without anelse.
(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-elsechain, 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
otherwiseblock is optional. - In a
switchstatement theotherwiseblock 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-elseiffor 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.
True.
- Correct! The
otherwisebranch is similar to theelsein anifstatementβit provides a default action when no cases match. False.
- Correct! The
otherwisebranch is similar to theelsein anifstatementβ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.
- 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. Useif-elseifinstead. - Testing multiple logical conditions that use AND (
&) or OR (|). - Incorrect. Switch statements only test for equality. Use
if-elseiffor complex logical conditions. - Checking if a value is greater than a threshold.
- Incorrect. This requires a relational operator (
>), which switch statements cannot use. Use anifstatement.
(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-elsechain. The blocks donβt accumulate. y = 1- Correct! Even though
x > 8andx > 3are also true, MATLAB stops at the first true condition (x > 5) and assignsy = 1.
(i) Nested If vs Elseif Chain.
- There is no difference; they are equivalent in all cases.
- Incorrect. Nested
ifstatements can check multiple conditions independently, whileelseifchains are mutually exclusive. - Nested
ifstatements run faster thanelseifchains. - Incorrect. Performance is not the key difference; the distinction is in the logic structure and whether branches are mutually exclusive.
- Nested
ifstatements can allow multiple blocks to execute;elseifchains execute only one block. - Correct! In an
if-elseif-elsechain, only one block runs. With nestedifstatements, multiple independent conditions can be true. - Elseif chains require more
endkeywords than nestedifstatements. - Incorrect. Actually, nested
ifstatements require moreendkeywords (one for eachif), while anelseifchain 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, becausexis not a logical value.- Incorrect. While
xis numeric, MATLAB automatically converts non-zero numbers to true in conditional statements. - An error occurs because
xis not a logical value. - Incorrect. MATLAB allows numeric values in
ifconditions and treats non-zero as true and zero as false. x is false, because5is not equal totrue.- Incorrect. MATLAB doesnβt compare
xtotrue; it converts non-zero values to true.
