Practical 4: For- and While- Loops, If-statements
Command Sequence Controls
MATLAB offers features to allow you to control the sequencing of commands by setting conditions. The following table shows the main types.
Command | Action | Example |
for - loop | Executes a set of commands repeatedly by incrementing a variable by a given step size until the set maximum is reached. | For each hour from 1pm to 12pm, print the statement “it is <hour> o’clock”. |
while - loop | Executes a set of commands if a condition after while is true. | You are asked to count during one minute. In other words, while chronometer hand have not done a whole circle, keep counting. In this case you do not know what will be your last number. |
if-else statements |
Executes a set of commands once, if a given condition is met, else- executes a set of commands, if the given condition is not met. |
If the hour of the day is 6pm, print the statement “it is dinner-time, it’s <hour> o’clock”. Else the hour of the day is not 6pm, print the statement “it is not dinner-time, it’s <hour> o’clock”. |