Professional Documents
Culture Documents
Aiviraj It Workshop
Aiviraj It Workshop
LOOP OPERATIONS
THEORY:
1. Conditional statements if
Instructions 1
else
Instructions 2
End
select variable
case value 1
instructions 1
case value 2
instructions 2
else
instructions 3
end
instructions
end
end
PROCEDURE:
PPROGRAME:
1. // Conditional statements if. A number is taken as input and checked for positive or negative
using if- else condition.
if (x >=0) then
else
end
OUTPUT:
Enter the value of x::3
2. //Select statements to print on what day we are in a week, The dayNum returns the number
for the given system date ie. Sun is considered 1, Mon = 2 , Tue = 2 etc. The dayString
returns the day ie. Mon,Tue etc. Using dayString, the different cases are dealt and the
statements are dealt accordingly.
[dayNum,dayString] = weekday(datenum());
select dayString
case "Mon" then
disp("Day2");
disp("Day3");
disp("Day4");
else
disp("Weekend");
end
OUTPUT:
"Day4"
function fact=factorial(n)
fact=1;
for i=1:n
fact=fact*i;
end
endfunction
OUTPUT:
factorial(3)
ans =
6.
4. //While loop to find factorial of given number
function fact=factorial(n)
fact =1;
i=1;
while i<=n
fact=fact*i;
i=i+1;
end
endfunction
OUTPUT:
factorial(5)
ans =
120.
RESULT: The programs are executed using if, for, while and select statements.