0% found this document useful (0 votes)
9 views12 pages

Loop Statements-OCTAVE - Lab Class

Uploaded by

Ghadeer
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)
9 views12 pages

Loop Statements-OCTAVE - Lab Class

Uploaded by

Ghadeer
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/ 12

Loop Statements

IT 6010-Maths for Computing


What are Loop Statements?
• Loop statements are essential
constructs in programming that allow
us to repeat a block of code multiple
times.
• In Octave, there are three main types
of loop statements: `for`, `while`,
and `do-until`.
• Octave does not have a built-in do-
while loop structure like some other
programming languages (e.g., C or C+
+, Java or JavaScript).
1. For Loop
The `for` loop is used when we know in advance
how many times we want to execute a block of
code.
Syntax:
for variable = start:increment:end
% code to be executed
end

 variable: Loop control variable


 start: Initial value of the control variable
 increment: Step size; if not specified, it
defaults to 1
 end: Final value of the control variable
1. For Loop
Example1: Print numbers from 1 to 5 using for loop

for i = 1:5 % loop starts with i = 1 and


increments by 1 until it reaches 5
disp(i);
% prints the current value of i
during each iteration
end
% specifies where the loop's block of
statements stops, in this case i = 5
1. For Loop
Example2: Print even numbers from 2 to 10 using for loop

% loop starts with i = 2 and


for i = 2:2:10
increments by 2 until it reaches 10

disp(['Even number: ', num2str(i)]);


% prints the current value of i
end during each iteration

% specifies where the loop's block of


statements stops, in this case i = 10
2. While Loop
The `while` loop continues to execute a block of
code as long as a specified condition is true.
This loop is useful when the number of iterations
is not known in advance.
Syntax:
while condition
% code to be executed
end
 condition: a logical expression that controls the
loop's execution
 code to be executed: block of statements or instructions
within the while loop that will run repeatedly as long
as the loop's condition remains true
 end: indicates where the loop's block of code stops
2. While Loop

Example1: Print numbers from 1 to 5 using while loop

x = 1; % loop starts with x = 1

% checks the condition if x<= 5. If


while x <= 5; true, it executes inside the loop

disp(x);
% display the number

x = x +% 1;
increments x to ensure the loop
eventually terminates
end
% specifies where the loop's block of
statements stops, in this case x = 5
2. While Loop

Example2: Print even numbers from 2 to 10 using while loop

x = 2; % loop starts with x = 2

% checks the condition if x<= 10. If


while x <= 10; true, it executes inside the loop

disp(x);
% display the number

x = x +% 2;
increments x to ensure the loop
eventually terminates
end
% specifies where the loop's block of
statements stops, in this case x = 10
3. Do-Until Loop
The do-until loop executes the loop body first and
checks the condition at the end of each iteration.
This guarantees the loop executes at least once.
Syntax:
do
% code to be executed
until condition

 code to be executed: This is the block of code that is


executed in each iteration of the loop. The code inside
the do block will run at least once, regardless of the
condition.
 condition: the condition that determines when the
loop stops
3. Do-Until Loop
Example1: Print numbers from 1 to 5 do-until loop

x = 1; % loop starts with x = 1


do
disp(x);
% the loop executes the block first
x = x + 1;
until (x > 5)
% after the loop body, it checks if x > 5.
If false, it continues; otherwise, it
exits.
3. Do-Until Loop
Example2: Print even numbers from 2 to 10 using do-until
loop
x = 2; % loop starts with x = 2
do
disp(x);
% the loop executes the block first
x = x + 2;
until (x > 10)
% after the loop body, it checks if x > 10.
If false, it continues; otherwise, it
exits.
Loop Control Statements
`break`: Exits the loop immediately
`continue`: Skips the rest of the current
iteration and moves to the next
Example: Print numbers from 1 to 10 but skip when
i is 5 and stop when i is 8.
% The loop runs from i = 1 to i = 10.
for i = 1:10
if i == 5 % When i is 5, the continue statement skips
the disp(i) command for that iteration
continue;
% When i reaches 8, the break statement
elseif i == 8 exits the loop, so the loop stops running
break; at this point
% marks the end of the inner loop (if-else block)
end
% displays the value of i for each iteration,
disp(i);
except when i is 5 and after the loop breaks
end at i = 8.
% marks the end of the outer loop

You might also like