Matlab - Tutor5 - Logic2
Matlab - Tutor5 - Logic2
o If statements can be nested, but each if statement requires the end keyword.
Syntax:
1. if expression
2. Statements
3. if expression
4. Statements
5. else
6. Statements
7. end
8. elseif expression
9. Statements
10. if expression
11. Statements
12. end
13. else
14. Statements
15. end
Example1
Output:
Example 2:
MATLAB switch
The switch is another type of conditional statement and executes one of the group of
several statements.
ADVERTISEMENT
o If we want to test the equality against a pre-defined set of rules, then the switch
statement can be an alternative of the if statement.
Syntax:
1. switch switch_expression
2. case case_expression1
3. Statements
4. case case_expression2
5. Statements
6. case case_expressionN
7. Statements
8. otherwise
9. Statements
10. End
o case & switch must be equal for numbers as- case_expression == switch_expression.
o For character vectors, the result returned by the strcmp function must be equal to 1 as -
strcmp(case_expression, switch_expression) == 1.
o For object, case_expression == switch_expression.
o For a cell array, at least one of the elements of the cell array in case_expression must
match switch_expression.
o switch statement doesn't test for inequality, so a case_expression cannot include
relational operators such as < or > for comparison against the switch_expression.
Example1:
Output:
enter a number: 4
Thursday
Example2:
MATLAB Loops
A loop statement allow us to execute a statement or group of statements multiple
times.
Two additional command, break and continue, can be used to create a third type of
loop, known as midpoint break loop. Midpoint break loop is useful for situations where
the commands in the loop must be executed at least once, but where the decision to
exit the loop is based on some criterion.
Types of Loops
There are two types of loop in MATLAB.
PauseNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
ADVERTISEMENT
1. for
2. while
for loop
A for loop is used to repeat a statement or a group of statements for a fixed number of
times.
Syntax
while loop
A while loop is used to execute a statement or a group of statements for an indefinite
number of times until the conditional specified by while is no longer satisfied.
Syntax
1. while <expression>
2. <statements>
3. end
Syntax:
Example1:
1. % program to print multiples of first prime number between 1000 and 2000
2. % using for loop
3. pr = 0;
4. for k = 1000:2000
5. if isprime(k)
6. pr = k;
7. disp(['The first prime number is : ',num2str(pr)])
8. for m = pr:pr:pr*10
9. disp(m)
10. end
11.
12. break
13. end
14. end
Output:
2018
3027
4036
5045
6054
7063
8072
9081
10090
Example2:
1. for a = 10:20
2. fprintf('value of a: %d\n', a);
3. end
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
value of a: 20
Example3:
Output:
PauseNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
1
9/10
4/5
7/10
3/5
1/2
2/5
3/10
1/5
1/10
0
Syntax:
1. while <expression>
2. <statements>
3. end
Example1:
1. % program to find the number ten from a series of random numbers
2. % using while loop
3. k = 1;
4. while k
5. if randi(50,1) == 10
6. disp(['The random number equivalent to 10 found at ',num2str(k),' step'])
7. break
8. end
9. k = k + 1;
10. end
Output:
Example2:
1. a = 10;
2. % while loop execution
3. while( a < 20 )
4. fprintf('value of a: %d\n', a);
5. a = a + 1;
6. end
Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
The syntax for the nested for loop statement in MATLAB is as follows:
1. for m = 1:j
2. for n = 1:k
3. <statements>;
4.
5. end
6. end
The syntax for the nested while loop statement in MATLAB is as follows:
1. while <expression1>
2. while <expression2>
3. <statements>
4. end
5. end
Example:
We can use the nested for loop to display all the prime numbers from 1 to 100.
PauseNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
ADVERTISEMENT
1. for i=2:100
2. for j=2:100
3. if(~mod(i, j))
4. break; % if factor found, not prime
5. end
6. end
7. if(j > (i/j))
8. fprintf('%d is prime\n', i);
9. end
10. end
Output:
2 is prime
3 is prime
5 is prime
7 is prime
11 is prime
13 is prime
17 is prime
19 is prime
23 is prime
29 is prime
31 is prime
37 is prime
41 is prime
43 is prime
47 is prime
53 is prime
59 is prime
61 is prime
67 is prime
71 is prime
73 is prime
79 is prime
83 is prime
89 is prime
97 is prime
MATLAB break
The break statement terminate the execution of a for loop or while loop. When a break
statement is encountered, execution proceeds with the next statement outside of the
loop. In nested loops, break exists from the innermost loop only.
Syntax:
1. break
Output:
a = 6×6
82 17 70 54 54 10
27 18 70 66 33 27
60 43 64 41 11 16
3 10 4 82 62 29
43 60 7 72 78 45
32 48 32 97 43 53
program running smoothly
program running smoothly
program encounters the number 27, which is not useful for the current
program;
at index no.:2
so loop terminates now.....bye bye
Example2:
Output:
PlayNext
Mute
Duration 18:10
Loaded: 2.94%
Â
Fullscreen
a = 4×4
0.2398 -1.6118 0.8617 0.5812
-0.6904 -0.0245 0.0012 -2.1924
-0.6516 -1.9488 -0.0708 -2.3193
1.1921 1.0205 -2.4863 0.0799
negative number :-0.69036,found at index: 2,hence the program terminated
Example:
The system is installed at different locations all over the world. Somewhere the
temperature is measured in Celsius and somewhere it is measured in Fahrenheit. So we
need to take care of these temperature units also.
MATLAB continue
The continue statement works within a for or while loop and passes control to the next
iteration of the loop.
Syntax:
1. Continue
o Continue statement passes the control of the execution to the next iteration of
a for or while loop.
o All remaining statements following the continue statement do not execute for the
current iteration.
o The continue statement applies only to the body of the loop where it is called, hence in
nested loops, it affects the execution of the loop in which it occurs.
o The continue statement only works inside a for or while loop, and it can't be used inside
a function. But if a function is having a for or while loop, there we can
use continue inside the loop.
Example1:
Output:
21
33
45