Your country

Tools that support it use your country for local currency, number formats, units and paper size. Your choice is saved only in this browser.

Type a name or a two-letter code. Use the up and down arrow keys to move through the countries, Enter to choose one and Escape to close.

MATLAB & GNU Octave  Module 4 – Logical arrays and control flow

Decisions: if, elseif, else and switch

Write if, elseif, else and switch blocks in MATLAB and Octave, see what if does with arrays and NaN, guard tests with && and ||, and compare text with strcmp.

  • Beginner
  • 20 minutes
  • Examples run with octave 11.3.0 and Python 3.14.8
  • By MySmartCoPilot
Free lesson, Pro answers: 20 minutes
Free: the whole lesson. With a pass: the model answers to its 2 interview questions.

What you will learn

  • Write if, elseif and else chains whose tests run in the right order
  • Predict what if does with an array, an empty array or NaN, and state the intent with any and all
  • Use && and || to guard a test, and explain how they differ from & and |
  • Write switch blocks with numeric, text and grouped cases
  • Compare text with strcmp, strcmpi and ismember instead of ==

Before you start

What you’ll learn · Before you start

What you will learn

  • Write if, elseif and else chains whose tests run in the right order
  • Predict what if does with an array, an empty array or NaN, and state the intent with any and all
  • Use && and || to guard a test, and explain how they differ from & and |
  • Write switch blocks with numeric, text and grouped cases
  • Compare text with strcmp, strcmpi and ismember instead of ==

Before you start

On this page

Six members of a frame have utilisation ratios from 0.42 to 1.12, and each must be labelled safe, near the limit or over it, then sent to the check its type needs. That is a chain of if, elseif and else tests followed by a switch on the type, and the order of the tests decides the answers: swap two of them and a member at 0.42 is reported near the limit.

Here you write if chains and switch blocks, see what if does with an array, an empty array and NaN, use && and || to guard a test, and compare text with strcmp instead of ==.

if takes the first branch whose test is true

if runs its block when its condition is true. Each elseif adds a test that runs only when every test above it was false, and else runs when none was true. This script labels one member by its utilisation u, the load on it divided by the load it may carry, with this example’s own limits of 0.8 and 1.0, then picks the check for its type with the switch of a later section:

classify_load.m: a band from u, a check from the type M · classify_load.m
% classify_load.m: the band of one member's utilisation u, and the check its type needs.
% Reads u and type from the workspace. The limits 0.8 and 1.0 are this example's own rule.
if u <= 0.8
  band = 'safe';
elseif u <= 1.0
  band = 'near the limit';
else
  band = 'over the limit';
end

switch type
  case {'beam', 'girder'}
    check = 'bending';
  case {'column', 'strut'}
    check = 'buckling';
  case 'tie'
    check = 'tension';
  otherwise
    check = 'none: unknown type';
end
fprintf('%s, u = %.2f: %s, check %s\n', type, u, band, check);

It reads u and type from the workspace, so the session sets them at the prompt and runs it for six members, then runs classify_wrong.m, the same chain with its two tests swapped:

classify_load.m for six members, then the swapped chain, GNU Octave 11.3.0 Python · bands_session.py
from session import prompt, version

version()
prompt("members.m")

Output

# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --quiet
octave:1> u = 0.42; type = 'beam'; classify_load
beam, u = 0.42: safe, check bending
octave:2> u = 0.80; type = 'strut'; classify_load
strut, u = 0.80: safe, check buckling
octave:3> u = 0.95; type = 'girder'; classify_load
girder, u = 0.95: near the limit, check bending
octave:4> u = 1.12; type = 'tie'; classify_load
tie, u = 1.12: over the limit, check tension
octave:5> u = 0.60; type = 'Beam'; classify_load
Beam, u = 0.60: safe, check none: unknown type
octave:6> u = 0.56 / 0.7; type = 'beam'; classify_load
beam, u = 0.80: near the limit, check bending
octave:7> fprintf('%.17g\n', u)
0.80000000000000016
octave:8> u = 0.42; classify_wrong
u = 0.42: near the limit

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 bands_session.py

Step through the chain for three of the members:

Interactive · stage by stage How the if chain of classify_load.m picks one band Step through three members. The tests run from the top, and the first one that is true decides the band.
How the if chain of classify_load.m picks one band, step 6 of 6. Showing if u <= 0.8 (1.12 <= 0.8: false), elseif u <= 1.0 (1.12 <= 1.0: false), else (runs: 'over the limit'), end: one band is set. In focus: if u <= 0.8, elseif u <= 1.0.true: skip the resttrue: skip elseif u <= 0.81.12 <= 0.8: falseelseif u <= 1.01.12 <= 1.0: falseelseruns: 'over the limit'end: one band is setthe program goes onswapped, 0.42 stops here

The drawing shows the whole scene; its 6 steps are listed below.

Every step as text
  1. The girder, u = 0.95. The first test is u <= 0.8, and 0.95 <= 0.8 is false, so its branch is skipped and the next test runs.
  2. The second test. 0.95 <= 1.0 is true, so band becomes 'near the limit'.
  3. Straight to end. Once a branch has run, the rest of the chain is skipped. The else is never reached, and the program goes on after end.
  4. The beam, u = 0.42. The first test is true at once, so band is 'safe' and neither of the other branches is looked at.
  5. The tie, u = 1.12. Both tests are false, 1.12 <= 0.8 and 1.12 <= 1.0, so the else branch runs and band is 'over the limit'.
  6. Order decides. Every u that passes the first test would pass the second too, so the stricter test must come first. With the two swapped, as in classify_wrong.m, u = 0.42 came out as 'near the limit'.

Exactly one branch ran each time, the first whose test was true: the strut at 0.80 is safe because 0.80 <= 0.8 is true. That is why the order matters. Every u that passes u <= 0.8 also passes u <= 1.0, so the stricter test has to come first; classify_wrong.m tests u <= 1.0 first, and the beam at 0.42 came out near the limit. Put the strictest test first, or write each test so that it excludes the others.

A limit also needs care when u is computed rather than typed. The sixth member, u = 0.56 / 0.7, printed as 0.80 and still came out near the limit: in double precision the division gives 0.80000000000000016, just above 0.8, as the next line of the session shows. When a computed value can land on a limit, decide which side it belongs to with a tolerance, as in the lesson on logical arrays.

else if is not elseif

Written as two words, else if opens a second if inside the else branch, and that if needs an end of its own. The Octave manual warns that the elseif keyword must not be spelled else if.

Octave also accepts endif as the end of an if, but MATLAB requires end for every block, as the Octave wiki’s list of differences records, so shared code writes end.

Checkpoint

Check: The first true branch, and the order of tests

  1. Question 1 of 2 classify_load.m tests u <= 0.8, then u <= 1.0, then else. Which band does u = 0.80 get?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: 'safe', because 0.80 <= 0.8 is true and the first true test decides

    The session printed strut, u = 0.80: safe, check buckling.

  2. Question 2 of 2 What does the chain set band to?

    Read the code, then choose one answer.

    u = 0.42;
    if u <= 1.0
      band = 'near the limit';
    elseif u <= 0.8
      band = 'safe';
    else
      band = 'over the limit';
    end
    Show the answer: Question 2 of 2

    Answer: 'near the limit'

    classify_wrong.m printed u = 0.42: near the limit. Put the strictest test first.

An array condition is true only if every element is non-zero

A condition does not have to be one value. Given an array, if takes its branch only when the array is not empty and every element is non-zero. The Octave manual writes if (matrix) as if (all (matrix(:))), with the empty case excluded, and MathWorks says an expression is true when its result is non-empty and contains only non-zero elements. The session tries the cases:

Arrays, empty arrays and NaN as conditions, GNU Octave 11.3.0 Python · conditions_session.py
from session import prompt, version

version()
prompt("conditions.m", errors=1)

Output

# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --quiet
octave:1> if [1 1 0], disp('taken'), else, disp('not taken'), end
not taken
octave:2> if [1 2], disp('taken'), else, disp('not taken'), end
taken
octave:3> if [], disp('taken'), else, disp('not taken'), end
not taken
octave:4> x = [3 -1 4];
octave:5> if x > 0, disp('all positive'), else, disp('not all positive'), end
not all positive
octave:6> if any(x < 0), disp('at least one negative'), end
at least one negative
octave:7> n = NaN;
octave:8> if n > 0, disp('positive'), else, disp('not positive'), end
not positive
octave:9> if n, disp('nonzero'), end
error: invalid conversion from NaN to logical
octave:10> if ~isnan(n) && n > 0, disp('positive'), else, disp('missing or not positive'), end
missing or not positive

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 conditions_session.py

Each condition of the session, and why it went the way it did:

Condition Branch taken? Why
if [1 1 0] no one element is zero
if [1 2] yes every element is non-zero
if [] no an empty array is never true
if x > 0, with x = [3 -1 4] no the mask 1 0 1 has a zero
if any(x < 0) yes any reduces the mask to one value
if n > 0, with n = NaN no NaN > 0 is false, so else runs without a word
if n, with n = NaN error NaN cannot become true or false

The rule is simple; the trouble is how if x > 0 reads. It looks like “if x is positive”, and for an array it means “if x is not empty and every one of its elements is positive”. Say which you mean with all or any, and test isempty on its own wherever an empty input can arrive.

NaN needs the same care. if n > 0 with n = NaN quietly took the else branch, so a missing value was handled as “not positive”, while if n stopped with invalid conversion from NaN to logical. Test isnan first, as the last line of the session did: if ~isnan(n) && n > 0.

Key idea

Never hand if an array and hope: write all(...) or any(...) for the question you mean, and test isempty and isnan wherever empty or missing input can reach the condition.

Checkpoint

Check: Arrays, empty arrays and NaN as conditions

  1. Question 1 of 2 What does the second line display?

    Read the code, then choose one answer.

    x = [3 -1 4];
    if x > 0, disp('all positive'), else, disp('not all positive'), end
    Show the answer: Question 1 of 2

    Answer: not all positive

    The session printed not all positive. Write all(x > 0) or any(x < 0) to say which question you mean.

  2. Question 2 of 2 n is NaN. What does if n > 0, disp('positive'), else, disp('not positive'), end display?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: 'not positive', with no warning

    The session printed not positive for if n > 0 and stopped only for if n. Test isnan(n) first.

The short-circuit operators stop as soon as the answer is known

a && b is true when both sides are, and a || b when at least one is, like & and |, but they evaluate their left side first and leave the right side alone once the result is settled. When the left side of && is false, the whole is false whatever the right side says, so the right side never runs; when the left side of || is true, the whole is true. This is short-circuit evaluation: Working out a logical expression from left to right and stopping once its value is known: in a && b, b is not evaluated when a is false, and in a || b, not when a is true., and it makes guards possible: the left test makes the right one safe to evaluate. This script guards an index with &&, then tries &:

guard_empty.m: protect an index with &&, then try & M · guard_empty.m
% guard_empty.m: protect an index with && and see what & does instead.
x = [];
if ~isempty(x) && x(1) > 0
  disp('&&: first value positive');
else
  disp('&&: empty, or first value not positive');
end
if ~isempty(x) & x(1) > 0
  disp('&: first value positive');
else
  disp('&: empty, or first value not positive');
end
ok = ~isempty(x) & x(1) > 0;
guard_empty.m, then && and || at the prompt, GNU Octave 11.3.0 Python · guard_session.py
from session import prompt, run, version

version()
run("!octave-cli --no-init-all guard_empty.m")
prompt("shortcircuit.m")

Output

# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --no-init-all guard_empty.m
&&: empty, or first value not positive
warning: Matlab-style short-circuit operation performed for operator &
warning: called from
    guard_empty at line 8 column 4

&: empty, or first value not positive
error: x(1): out of bound 0 (dimensions are 0x0)
error: called from
    guard_empty at line 13 column 1
[exit status 1]
$ octave-cli --quiet
octave:1> [1 0 1] & [1 1 0]
ans =

  1  0  0

octave:2> [1 1] && 1
ans = 1
octave:3> [] && true
ans = 0
octave:4> [] || true
ans = 1

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 guard_session.py

Step through the condition of its first if:

Interactive · step by step How && keeps x(1) from running on an empty x Step through the condition ~isempty(x) && x(1) > 0, first with x empty, then with x = [5 -2], then written with &.
How && keeps x(1) from running on an empty x, step 5 of 5. Showing ~isempty(x) (false: x is empty), x(1) > 0 (runs: an error), The condition (never reached). In focus: x(1) > 0.only if true~isempty(x)false: x is emptyx(1) > 0runs: an errorThe conditionnever reachedx(1): out of bound 0

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. An empty x. x is empty, as after a filter that kept nothing. The if statement asks ~isempty(x) && x(1) > 0.
  2. Left first. && works out its left operand first: ~isempty([]) is false.
  3. The right is skipped. With a false left side the whole condition is false, so && never evaluates x(1) > 0, the index that would fail on an empty x, and the else branch runs.
  4. x = [5 -2]. Now the left side is true, so && goes on to the right side. x(1) > 0 is 5 > 0, which is true, and so is the condition.
  5. Written with &. & evaluates both sides. Inside an if or while condition both programs short-circuit it all the same, Octave with a warning, but in ok = ~isempty(x) & x(1) > 0 the right side runs and fails on an empty x.

The first if printed its else message without touching x(1). The second, written with &, gave the same answer with a warning: inside the condition of an if or a while, MATLAB short-circuits & and | too, as MathWorks’ && page notes, and Octave does the same for compatibility, warning Matlab-style short-circuit operation performed for operator &, as its manual describes. Outside a condition there is no such rule, and the last line of the script, ok = ~isempty(x) & x(1) > 0, evaluated both sides and stopped with x(1): out of bound 0.

&& takes single values in MATLAB

MathWorks’ && page says each operand must evaluate to a scalar logical result, so [1 1] && 1 is not valid MATLAB. Octave 11.3 accepted it and returned 1, and it treats an empty operand as false: [] && true gave 0 and [] || true gave 1. Code that leans on this runs in Octave and fails in MATLAB, so reduce arrays with all or any before they reach && or ||.

Checkpoint

Check: && and || as guards

  1. Question 1 of 2 x may be empty. Which condition is safe to evaluate in an if statement?

    Choose one answer.

    Show the answer: Question 1 of 2

    Answer: ~isempty(x) && x(1) > 0

    guard_empty.m printed its else message for an empty x without touching x(1). The guard goes on the left.

  2. Question 2 of 2 x = []. Which of these lines stops with an error in Octave 11.3?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: ok = ~isempty(x) & x(1) > 0;

    The last line of guard_empty.m stopped with x(1): out of bound 0, after the if with & had worked and warned.

switch runs the first case that matches, and only that one

When one value is compared with a list of fixed values, switch says it more plainly than a chain of elseif. classify_load.m switches on type: each case names a value, or several in braces, and otherwise takes everything else. MathWorks documents how a case matches: numbers with ==, text with strcmp, and a cell of values when any of its elements matches. Step through it:

Interactive · stage by stage How switch picks one case Step through the switch of classify_load.m for a girder, then for 'Beam' with a capital B, then with lower.
How switch picks one case, step 5 of 5. Showing switch type (lower(type) = 'beam'), case {'beam', 'girder'} (matches: check = bending), case {'column', 'strut'} (not tried), case 'tie' (not tried), otherwise (not run). In focus: case {'beam', 'girder'}.switch typelower(type) = 'beam'case {'beam', 'girder'}matches: check = bendingcase {'column', 'strut'}not triedcase 'tie'not triedotherwisenot run

The drawing shows the whole scene; its 5 steps are listed below.

Every step as text
  1. One value, many cases. switch compares the value of type with each case in turn. For text it compares with strcmp, which is case-sensitive.
  2. The first case. This case lists two names, and a cell case matches when any of its elements does. strcmp('girder', 'girder') is true, so check becomes 'bending'.
  3. No fall-through. Only the first matching case runs. The program goes on after end without trying the other cases, which is what both programs document.
  4. type = 'Beam'. With a capital B, strcmp finds no case equal to 'Beam', because 'B' and 'b' are different characters, so the otherwise branch runs.
  5. With lower. switch lower(type) compares 'beam' instead, and the first case matches again, as the session below shows.

Only the first matching case runs. MathWorks says MATLAB’s switch “does not fall through like a C language switch statement”, and the Octave manual says only the first match is executed, so there is no break to forget and no fall-through: In a C switch, running on into the statements of the next case when a case has no break. MATLAB and Octave never fall through: only the first matching case runs.. The session checks both points, then the cure for letter case:

Cases, fall-through and letter case, GNU Octave 11.3.0 Python · switch_session.py
from session import prompt, version

version()
prompt("switch_demo.m")

Output

# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --quiet
octave:1> code = 2;
octave:2> switch code, case 0, disp('ok'), case {1, 2}, disp('warning'), case 2, disp('second match'), otherwise, disp('fault'), end
warning
octave:3> type = 'Beam';
octave:4> switch type, case {'beam', 'girder'}, disp('bending'), otherwise, disp('no case matched'), end
no case matched
octave:5> switch lower(type), case {'beam', 'girder'}, disp('bending'), otherwise, disp('no case matched'), end
bending

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 switch_session.py

code = 2 matched case {1, 2} and printed warning, and the later case 2 never ran. type = 'Beam' matched no case, because strcmp is case-sensitive, and switch lower(type) matched again. Which of the two statements to use depends on the test:

Your test Write Why
One value against fixed numbers or names switch one comparison per case, alternatives in braces
Ranges, such as u <= 0.8 if and elseif a case compares with == or strcmp only
Tests on different variables if and elseif switch looks at one value
Names in any letter case switch lower(name) strcmp is case-sensitive

End every switch with otherwise

classify_load.m only labels an unknown type. In real code, make otherwise stop with the value it did not expect, such as error('unknown member type: %s', type), so that a typo in the data becomes a message instead of a quiet default.

Checkpoint

Check: switch, cases and otherwise

  1. Question 1 of 2 What does the second line display?

    Read the code, then choose one answer.

    code = 2;
    switch code, case {1, 2}, disp('warning'), case 2, disp('second match'), otherwise, disp('fault'), end
    Show the answer: Question 1 of 2

    Answer: warning

    The session printed warning and nothing else.

  2. Question 2 of 2 type = 'Beam'. Which branch of the switch in classify_load.m runs?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: otherwise, because strcmp is case-sensitive

    The session printed Beam, u = 0.60: safe, check none: unknown type.

Text is compared with strcmp, not character by character

== on text compares character codes position by position, as any element-wise operator does, so it returns a mask and needs texts of the same length. strcmp compares complete texts, in the Octave manual’s words, and returns one true or false. The session compares a few member names both ways:

Comparing text, GNU Octave 11.3.0 Python · text_session.py
from session import prompt, version

version()
prompt("text_compare.m", errors=1)

Output

# GNU Octave (aarch64-apple-darwin25.4.0) version 11.3.0
$ octave-cli --quiet
octave:1> 'beam' == 'bean'
ans =

  1  1  1  0

octave:2> 'beam' == 'girder'
error: mx_el_eq: nonconformant arguments (op1 is 1x4, op2 is 1x6)
octave:3> if 'beam' == 'bean', disp('same'), else, disp('different'), end
different
octave:4> strcmp('beam', 'girder')
ans = 0
octave:5> strcmp('Beam', 'beam')
ans = 0
octave:6> strcmpi('Beam', 'beam')
ans = 1
octave:7> ismember('strut', {'column', 'strut'})
ans = 1
octave:8> "beam" == "bean"
ans =

  1  1  1  0

octave:9> class("beam")
ans = char

Recorded with Python 3.14.8 on macOS 26 arm64. To run it yourself: mise exec python@3.14.8 -- python3 text_session.py

Line by line, with what each result means:

Expression Octave 11.3 gave What it means
'beam' == 'bean' 1 1 1 0 a mask, one element per character
'beam' == 'girder' an error: nonconformant arguments 4 characters against 6
if 'beam' == 'bean' the else branch the mask has a zero
strcmp('beam', 'girder') 0 different texts, and no error
strcmp('Beam', 'beam') 0 letter case counts
strcmpi('Beam', 'beam') 1 letter case is ignored
ismember('strut', {'column', 'strut'}) 1 is the text one of these?

MathWorks’ if page gives the same advice for MATLAB: use strcmp to compare character vectors, because == fails when their sizes differ. if 'beam' == 'beam' does work, since every element of its mask is 1, which is how the habit survives until two lengths differ.

Double quotes differ between the programs

In MATLAB, double quotes create a string, and MathWorks’ strcmp page notes that string arrays can be compared with ==, one answer per string. In Octave 11.3, class("beam") printed char, and "beam" == "bean" gave the mask 1 1 1 0. strcmp behaves the same in both programs, so it is the portable choice.

Checkpoint

Check: Comparing text

  1. Question 1 of 2 What does the line give?

    Read the code, then choose one answer.

    'beam' == 'girder'
    Show the answer: Question 1 of 2

    Answer: An error: nonconformant arguments

    The session stopped with mx_el_eq: nonconformant arguments (op1 is 1x4, op2 is 1x6).

  2. Question 2 of 2 A data file names members 'Beam', 'BEAM' or 'beam'. Which test is true for all three?

    Choose one answer.

    Show the answer: Question 2 of 2

    Answer: strcmpi(name, 'beam')

    strcmp('Beam', 'beam') gave 0 and strcmpi('Beam', 'beam') gave 1 in the session.

Practise: grade bands and units

Write these as scripts that read their input from the workspace, like classify_load.m, and run them at the prompt:

  1. grade_band.m: from marks out of 100, set band to 'O' for 90 and above, 'A+' from 80, 'A' from 70, 'B+' from 60, 'B' from 50, 'C' from 40 and 'F' below 40, the scheme of a fictional university. Run it for 90, 89, 80, 79, 40 and 39: you should get O, A+, A+, A, C and F.
  2. Make it stop with error('grades:badMarks', 'marks must be one number from 0 to 100') when marks is not one number from 0 to 100, testing in this order: ~isscalar(marks) || marks < 0 || marks > 100. The || keeps an array away from the comparisons, so marks = [50 60] and marks = 101 must both stop. Then try marks = NaN: it passes both comparisons and lands in F, so add isnan(marks) to the test.
  3. unit_factor.m: from unit, set factor with a switch whose cases group a symbol with its name: {'N', 'newton'} gives 1, {'kN', 'kilonewton'} 1000 and {'MN', 'meganewton'} 1000000, and otherwise stops with an error. Do not use lower here: in the SI, M is mega, 10^6, and m is milli, 10^-3, as the BIPM’s table of prefixes shows, so 'mN' must never become a meganewton.

Then test your decisions. The questions follow your answers, and each one tells you why a choice holds or fails:

Practice that adapts to you

Practise this lesson

15 questions about 5 ideas of this lesson. Each answer is explained at once, and what you get right is kept in this browser.

  1. Question 1 of 15 When does the block of an elseif run?

    Choose one answer.

    Show the answer: Question 1 of 15

    Answer: When its test is true and every test above it was false

    The Octave manual says each condition is tested in turn, and the body of the first true one is executed.

  2. Question 2 of 15 classify_load.m tests u <= 0.8, then u <= 1.0. Which values of u get 'near the limit'?

    Choose one answer.

    Show the answer: Question 2 of 15

    Answer: Those with 0.8 < u <= 1.0

    An elseif test means "this, and none of the tests above": here, u <= 1.0 and not u <= 0.8.

  3. Question 3 of 15 u = 0.56 / 0.7 printed as 0.80, yet classify_load.m labelled it 'near the limit'. Why?

    Choose one answer.

    Show the answer: Question 3 of 15

    Answer: In double precision the division gives 0.80000000000000016, just above 0.8

    A computed value can land on either side of a limit. Decide with a tolerance which side a value within it belongs to.

  4. Question 4 of 15 What does if [], disp('taken'), else, disp('not taken'), end display?

    Choose one answer.

    Show the answer: Question 4 of 15

    Answer: not taken

    The session printed not taken. Test isempty on its own wherever an empty input can arrive.

  5. Question 5 of 15 A branch must run if at least one load is negative. Which condition says that?

    Choose one answer.

    Show the answer: Question 5 of 15

    Answer: if any(loads < 0)

    if any(x < 0) printed at least one negative for x = [3 -1 4] in the session.

  6. Question 6 of 15 A check reads if loads <= 0, error('bad loads'), end. Which input does it stop?

    Choose one answer.

    Show the answer: Question 6 of 15

    Answer: [-1 -2], where every load is non-positive

    An if on a mask means "every element": this check fires only when every load is zero or negative, so a single bad load among good ones, an empty array and a NaN all pass it.

  7. Question 7 of 15 In a && b, when is b evaluated?

    Choose one answer.

    Show the answer: Question 7 of 15

    Answer: Only when a is true

    MathWorks' && page says expr2 is not evaluated if expr1 is false.

  8. Question 8 of 15 Which condition is true for an empty x or a negative first value, without an error?

    Choose one answer.

    Show the answer: Question 8 of 15

    Answer: isempty(x) || x(1) < 0

    With ||, the guard is true when it should stop the evaluation; with &&, it is false.

  9. Question 9 of 15 flags is a 1-by-3 logical array, and the branch must run when every flag is set and n > 0. Which condition does that in both MATLAB and Octave?

    Choose one answer.

    Show the answer: Question 9 of 15

    Answer: if all(flags) && n > 0

    Say which question you mean, all or any, before the value reaches && or ||.

  10. Question 10 of 15 When does case {'column', 'strut'} match?

    Choose one answer.

    Show the answer: Question 10 of 15

    Answer: When the switch value is equal to either text

    MathWorks: for a cell array case, at least one of its elements matches the switch value.

  11. Question 11 of 15 What does the second line display?

    Read the code, then choose one answer.

    code = 3;
    switch code, case 0, disp('ok'), case {1, 2}, disp('warning'), otherwise, disp('fault'), end
    Show the answer: Question 11 of 15

    Answer: fault

    Numbers are compared with ==, and only otherwise is left for 3.

  12. Question 12 of 15 A label depends on ranges: up to 0.8 safe, up to 1.0 near the limit, above that over it. Which statement fits?

    Choose one answer.

    Show the answer: Question 12 of 15

    Answer: An if, elseif and else chain

    switch is for one value against fixed values; ranges need if and elseif, as classify_load.m does.

  13. Question 13 of 15 What does strcmp('beam', 'girder') return?

    Choose one answer.

    Show the answer: Question 13 of 15

    Answer: 0

    The session printed 0, where 'beam' == 'girder' had stopped with an error.

  14. Question 14 of 15 Which test is true when name is 'column' or 'strut', exactly as written?

    Choose one answer.

    Show the answer: Question 14 of 15

    Answer: ismember(name, {'column', 'strut'})

    ismember('strut', {'column', 'strut'}) gave 1 in the session.

  15. Question 15 of 15 "beam" == "bean" runs in both programs. What differs?

    Choose one answer.

    Show the answer: Question 15 of 15

    Answer: MATLAB compares two strings and gives one 0; Octave compares characters and gives 1 1 1 0

    strcmp("beam", "bean") gives 0 in both, which is why strcmp is the portable choice.

Interview questions

Warm-up: what is the difference between & and && in MATLAB and Octave?

& is the element-wise and: it evaluates both of its operands and returns a logical array, so [1 0 1] & [1 1 0] is 1 0 0, and it is the operator for combining masks. && is the short-circuit and of two single true or false values: it evaluates its left operand first and skips the right one when the left is false, so ~isempty(x) && x(1) > 0 never indexes an empty x. Use & on arrays and && in if and while conditions. Two portability details: MathWorks requires each operand of && to be a scalar logical result, while Octave 11.3 accepted [1 1] && 1; and inside a condition MATLAB short-circuits & as well, which Octave does too, with a warning. | and || follow the same rules.

Model answers open with any pass: ₹79 for 1 week ($2.99), one payment that never renews.

  1. Fresher to mid

    An input check reads if loads <= 0, error('invalid loads'), end. It let [5 0 7] through, let an empty vector through, and let [5 NaN 7] through. Explain each case, and rewrite the check so that it stops all three.

    Model answer, Pro. Model answers open with any pass; prices are in the notice on this page. First step and follow-ups

  2. Mid to senior

    A script that runs without errors in Octave 11.3 stops in MATLAB at if flags && n > 0, where flags is a 1-by-3 logical array. Explain the difference, give the portable fix, and name two other condition or comparison patterns that behave differently in the two programs.

    Model answer, Pro. First step and follow-ups

Key takeaways

  • if, elseif and else run the first branch whose test is true and skip the rest, so the strictest test comes first.
  • An array condition is true only when it is non-empty and has no zero element: write all or any for what you mean, and test isempty and isnan first.
  • && and || evaluate from left to right and stop once the answer is known, which makes guards such as ~isempty(x) && x(1) > 0 safe, while & outside a condition evaluates both sides.
  • MATLAB requires single true or false values on both sides of && and ||; Octave 11.3 also accepted arrays, so shared code reduces them with all or any.
  • switch compares numbers with == and text with strcmp, runs only the first matching case, groups alternatives in braces and should end with otherwise.
  • Compare text with strcmp, strcmpi or ismember: == compares characters and needs equal lengths, and double quotes make a string only in MATLAB.

Terms in this lesson

fall-through
In a C switch, running on into the statements of the next case when a case has no break. MATLAB and Octave never fall through: only the first matching case runs.
short-circuit evaluation
Working out a logical expression from left to right and stopping once its value is known: in a && b, b is not evaluated when a is false, and in a || b, not when a is true.

References

Report a problem with this lesson

Quick answers and tool search

Type to search tools or to get a quick answer, for example 18% of 2500. Use the up and down arrow keys to move through the results, Enter to choose, and Escape to close.