0% found this document useful (0 votes)
11 views16 pages

Matlab Lec#5

Uploaded by

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

Matlab Lec#5

Uploaded by

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

MATLAB

Lect#5
SZIC
Control Statement

▰ Control statements are used to alter the flow of program execution


based on certain conditions.

▰ They allow you to make decisions and control the order in which
statements are executed.

▰ The two primary types of control statements in MATLAB are:

▰ Conditional Statement (if-Else)


▰ Loops Statement (for, While, Do While)
Conditional Statement If and if Else

▰ If and if-else statements are used for decision-making in a program.

▰ The "if" statement allows you to execute a block of code only if a specified condition
is true.

▰ "if-else" statement provides an alternative block of code to be executed if the initial


condition is false.

▰ This is useful when you want your program to take different actions based on
whether a condition is true or false for example…..
x=1;
x = 10; y=8;
if x > 0 if x>8
disp('x is positive’); disp('greater tan
else 7')
disp('x is non-positive'); else
end disp('less then 8')
end
Example 1

▰x=input('input value of x');


▰if x<5
▰ y=2*x+5;
▰else
▰ y=x.^5;
▰end
▰disp(y)
Example 2
Nested If Else

▰ A nested if else statement refers to the use of one or more if


statements inside another if statement.
▰ This allows you to create more complex conditional structures by
evaluating multiple conditions in a hierarchical manner.
▰ the nested if statements allow you to check additional conditions
based on whether the outer if statement's condition is true or false.
▰ The indentation helps to visually represent the nested structure
Example….

x = 10;
y = 5;
if x > y
disp('x is greater than y');
if x > 0
disp('x is positive');
else
disp('x is non-positive');
end
else
disp('y is greater than or equal to x');
end
Example 3

▰x=input('enter value of x');


▰y=input('enter value of y');
▰if (x>=0 && y>=0)
▰ z=x+y;
▰ disp(z);
▰elseif(x>=0 && y<0)
▰ z=x+y^2;
▰ disp(z)
▰else
▰ disp(‘-----------')
MATLAB Loops

• Loops in MATLAB
• Repeats commands until a condition is met or a
specified number of iterations are completed.
• Types of Loops
• While loops
• For loops
• Nested loops
While Loop

▰ While loop is used to execute a block of statements repeatedly until a given a


condition is satisfied.
▰ When the condition becomes false, the line immediately after the loop in
program is executed.

While Loop
Syntax
while condition
% commands
end
While Loop Flow Chart….

Flowchart Section:

1. Start
2. Initialization: Label: Initialize num = 20
3. Condition Check: Label: num < 30?
4. Commands: Label: Display num, Increment num by 1
5. Update: Label: Update num
6. Terminate: Label: End
While Program

num = 20;
while (num < 30)
disp(['value of num: '
num2str(num)]);
num = num + 1;
end
For Loop Syntax

Syntax
for initial value:step value:final-value
statements
end
or
for initial value:final value
statements
end
For Loop

Flowchart

1. Initialize: Set initial values


2. Condition: Label: Check if the condition is true
3. Commands: Label: Execute statements within the loop
4. Loop: Label: Return to the condition check
For Loop Program

for num = 0:10


disp(['value of num: ' num2str(num)]);
end
OR
for i = 1:2:5
disp(['value of num: ' num2str(num)]);
end
Nested For Loop

Nested Loops
Syntax
for i = start1:increment1:end1
for j = start2:increment2:end2
% commands
end
end

You might also like