0% found this document useful (0 votes)
12 views4 pages

Matlab 12

Uploaded by

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

Matlab 12

Uploaded by

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

Experiment: Understanding Loops in MATLAB

Objective

To explore and implement different types of loops in MATLAB for iterative tasks and automation.

Theory

Loops are fundamental constructs in programming used for executing a block of code multiple times.
MATLAB supports two primary types of loops:

1. For Loop
Executes a block of code for a fixed number of iterations.
Syntax:

for variable = start:step:end

% Code to execute

end

2. While Loop
Executes a block of code as long as a condition is true.
Syntax:

while condition

% Code to execute

end

Key Points:

 Loops are useful for automating repetitive tasks.

 Proper initialization and termination conditions are essential to avoid infinite loops.

Materials Required

 MATLAB Software

 A computer system with MATLAB installed

Procedure

1. For Loop: Create a for loop to calculate and display the squares of numbers from 1 to 10.

for i = 1:10

square = i^2;

fprintf('Square of %d is %d\n', i, square);

end
2. While Loop: Create a while loop to calculate the factorial of a given number.

n = 5; % Example number

factorial = 1;

i = 1;

while i <= n

factorial = factorial * i;

i = i + 1;

end

fprintf('Factorial of %d is %d\n', n, factorial);

3. Nested Loops: Use nested loops to generate a multiplication table for numbers 1 to 5.

for i = 1:5

for j = 1:5

product = i * j;

fprintf('%d x %d = %d\t', i, j, product);

end

fprintf('\n');

end

4. Break and Continue Statements:

o Break: Exit a loop when a condition is met.

for i = 1:10

if i == 6

fprintf('Breaking loop at i = %d\n', i);

break;

end

disp(i);

end

o Continue: Skip the rest of the current iteration and continue with the next iteration.

for i = 1:10

if mod(i, 2) == 0

continue;

end
fprintf('%d is odd\n', i);

end

Observations

Loop Type Description Example Task

For Loop Fixed number of iterations Calculate squares of numbers

While Loop Conditional execution Calculate factorial

Nested Loops Loop within a loop Generate multiplication table

Break Exit a loop early Stop at a specific condition

Continue Skip to the next iteration Process only specific values

Conclusion

Loops in MATLAB are powerful tools for automating repetitive tasks. Understanding their use and
control mechanisms, such as break and continue, enhances programming efficiency.

Code

% For Loop: Calculate squares of numbers

for i = 1:10

square = i^2;

fprintf('Square of %d is %d\n', i, square);

end

% While Loop: Calculate factorial

n = 5; % Example number

factorial = 1;

i = 1;

while i <= n

factorial = factorial * i;

i = i + 1;

end

fprintf('Factorial of %d is %d\n', n, factorial);

% Nested Loops: Multiplication table


for i = 1:5

for j = 1:5

product = i * j;

fprintf('%d x %d = %d\t', i, j, product);

end

fprintf('\n');

end

% Break Statement: Exit loop early

for i = 1:10

if i == 6

fprintf('Breaking loop at i = %d\n', i);

break;

end

disp(i);

end

% Continue Statement: Skip specific iterations

for i = 1:10

if mod(i, 2) == 0

continue;

end

fprintf('%d is odd\n', i);

end

You might also like