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

EXPERIMENT- 4

LOOP OPERATIONS

AIM: Write a program to perform if, for, while operations.

SOFTWARE REQUIRED: PC loaded with SCILAB software

THEORY:

Scilab language includes classical control structures

1. Conditional statements if

If Boolean expression then

Instructions 1

else

Instructions 2

End

2. Branching with respect to the value of a variable select

select variable

case value 1

instructions 1

case value 2

instructions 2

else

instructions 3

end

3. Loop control statements for

for variable = start: step: end

instructions

end

4. Loop control based on a boolean expression while

While (boolean expression)


instructions

end

PROCEDURE:

1. Click on SCILAB Icon

2. Click on launch scinotes.

3. Type the program on editor window

4. Save the program with filename.sce extension

5. Execute the program and observe the output

PPROGRAME:

1. // Conditional statements if. A number is taken as input and checked for positive or negative
using if- else condition.

x=input('Enter the value of x::')

if (x >=0) then

disp ("x is positive " );

else

disp ("x is negative " );

end

OUTPUT:
Enter the value of x::3

"x is positive "

Enter the value of x::-3

"x is negative "

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("Start of work week");

case "Tue" then

disp("Day2");

case "Wed" then

disp("Day3");

case "Thu" then

disp("Day4");

case "Fri" then

disp("Last day of work week");

else

disp("Weekend");

end

OUTPUT:
"Day4"

3. //for loop to find factorial of given number

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.

You might also like