Lab 06
Lab 06
No 06
Conditional if-else statements and Loops (for loop, while loop and do-while loop) in Matlab
Lab. Objective: To practical implement if-else statement and loops in Matlab.
Theory:
An if statement can be followed by one (or more) optional elseif... and an else statement, which is very
useful to test various conditions.
When using if... elseif...else statements, there are few points to keep in mind:
• An if can have zero or one else's and it must come after any elseif's.
• An if can have zero to many elseif's and they must come before the else.
• Once an else if succeeds, none of the remaining elseif's or else's will be tested.
Loops in Matlab
Loops repeats a statement or group of statements while a given condition is true. It tests the condition
before executing the loop body. Executes a sequence of statements multiple times and abbreviates the
code that manages the loop variable. You can use one or more loops inside any another loop
Matlab Work
if statement in Matlab
Output:
20
Output:
Matlab work
output:
n is even number
Matlab Work
disp(i^2)%taking square of i
Output:
16
a=10;%defining variable a
b=a+2; %incrementing a
Lab. Assignment: