Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

LAB REPORT # 2

Title: Basic command in MATLAB; for loop, while loop, input, if else if
OBJECTIVE: The purpose of for loop to perform repetitive task to a specified number of
times. It is used when you know about how many times you want to execute a set of the
statement or specified task. While loop is also used for repetition when and certain condition
remain true or we don’t know about the iteration. The input function is used to prompt the
user for input during the program execution.it allows to user interact with the program
execution.
Theoretical background:
For loop is based on the concept of iteration. It's designed to iterate over a sequence. And execute the
block of code for each item in the sequence. The loop variable takes each value from the iterable in
sequence, and the loop continues until the iterable is exhausted.
The while loop is based on the concept of repetition. It continues to execute the block of code as long
as the condition remains true. If the condition becomes false at any point, the loop terminates. It's
important to ensure that the condition will eventually become false; otherwise, the loop will run
indefinitely, causing a program to hang or crash.
This structure is based on the concept of branching. The program evaluates the conditions in order,
executing the block of code associated with the first condition that evaluates to true. If none of the
conditions are true and there's an else block, the code inside the else block will be executed. This
construct enables the program to make decisions and choose different paths of execution based on the
given conditions.
Procedure:

For Loop: It repeats a set of instructions for a specified number of times.


While Loop: It repeats a set of instructions as long as a condition is true.

Input: It allows the user to enter data into the program.


If-Else-If: It evaluates conditions and executes different instructions based on the
outcome

Cods and results:


Calculation of table by using for loop for
i=1:10
a (i)=5*i;
fprintf (" 5 * %d = %d \n",i,a(i));
end
a=[3 4 5;6 7 8;-3 4 9]
a(3,2)=6;
if a(2,2)<=9
disp("correct")
else

disp("incorrect")
end

Conclusions:
For loop is used for the task like iterating through elements in array performing numerical
computation of the statement many times as you can want. While loop is iterating as your
specific condition meet according to our requirements.

You might also like