Chapter 2: logical statements
Chapter 2: logical statements
The flow of information through a program is often determined by a set of logical statements, such as:
if A,
{execute commands based on truth of A}
else if B,
{execute commands based on truth of B}
else if C,
{execute commands based on truth of C}
else
{execute default commands}
end
The series of statements enclosed by if...else...elseif...elseif...end are executed differentially depending on whether the logical statements A or B or C are TRUE. A, B, C are propositions whose truth values we set earlier in the program. Here we will use the built-in random number generator to practice using the if...else....elseif logical structure.
N=rand([1 2]);
if all(N>.5), ['option 1']
elseif all(N<.5), ['option 2']
else ['default option'], end
The output will be one of the three options. Which is least likely? What happens if it is possible for more than one option to be true?