0% found this document useful (0 votes)
29 views3 pages

Lab 06

The document discusses if-else statements and different types of loops (for, while, do-while) in Matlab. It provides theoretical explanations and examples of implementing each type. It explains the syntax and usage of if-else statements, including that an if can have zero or one else and must come after any elseif statements. It also provides examples of for, while, and do-while loops in Matlab code, displaying output. The objective of the lab is to practically implement if-else statements and loops (including do-while) in Matlab.

Uploaded by

Mohsin Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
29 views3 pages

Lab 06

The document discusses if-else statements and different types of loops (for, while, do-while) in Matlab. It provides theoretical explanations and examples of implementing each type. It explains the syntax and usage of if-else statements, including that an if can have zero or one else and must come after any elseif statements. It also provides examples of for, while, and do-while loops in Matlab code, displaying output. The objective of the lab is to practically implement if-else statements and loops (including do-while) in Matlab.

Uploaded by

Mohsin Iqbal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Lab.

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.

Tools: PC, Matlab Software.

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

%if statement in Matlab

x=20; %defining value of x

if(x<100);%defining the if statement

disp(x)%displaying the number

end% Matlab built-inn command to terminate the loop

Output:

20

if-else statement in Matlab

%if statement in Matlab


x=200; %defining value of x

if((x>0)&(x<100));%defining the if statement

disp('ok')%displaying the number

else % defining the else statement

disp('x contain invalid number')

end% Matlab built-inn command to terminate the loop

Output:

x contain invalid number

if- else statement in Matlab

Matlab work

%if-else statement in Matlab

n=10; %defining value of x

if(n<=0);%defining the if statement

disp('n is zero or negative number')%displaying the number

elseif(rem(n,2)==0);%defining condition that number is even

disp('n is even number')%displaying the number

else% defining the else statement

disp('n is odd number')%displaying the number

end% Matlab built-inn command to terminate the loop

output:

n is even number

for loop in Matlab

Matlab Work

%for loop in Matlab


for i=1:1:4;%defining the for loop

disp(i^2)%taking square of i

end %terminating the for loop

Output:

16

While loop in matlab

%while loop in Matlab

a=10;%defining variable a

while a<3;%defining the while loop

b=a+2; %incrementing a

disp(b)%displaying the number b

end %terminating the while loop

Lab. Assignment:

Implement do-while loop in matlab

You might also like