• Tidak ada hasil yang ditemukan

Loops and Conditional Statements Dr.Abbas

N/A
N/A
Protected

Academic year: 2025

Membagikan "Loops and Conditional Statements Dr.Abbas"

Copied!
22
0
0

Teks penuh

(1)

Loops and Conditional Statements Dr.Abbas

Lab 4

(2)

For loops

To run a command more than once as an index varies, you may use a for loop.

% display of numbers for i = 1:9

disp(i) End

% 6!

fact = 1;

for i = 2:6 fact = fact * i;

end

(3)

Ex 1:

Write m-file to compute the factorial of any integers !

(4)

The Solution :

N= input('Enter the number to calculate the factorial: ');

fact=1;

for i=2:N

fact=fact*i;

end

disp(fact)

(5)

% Calculate the Square of numbers 1 to 5 for i = 1:5

disp([int2str(i) ' squared equals ' int2str(i^2)]) end

Note : INT2STR used to convert integer to string

.

(6)

Summing Series

total = 0;

for n = 1:6

total = total + 2ˆn;

end

(7)

% function

function [out] = f(in1)

out = in1 * sin(in1 *pi/4);

% loop

maxN = input('Enter the value of N : ');

I(1) = f(1);

for N=2:maxN

I(N) = I(N-1) + f(N);

end

disp('Values of I_N') disp(I)

(8)

>>

maxN = input('Enter the value of N : ')

;

for N=2:maxN

X= N * sin (N *pi \ 4) end

disp('Values of I_N')

disp(X)

(9)

EXERCISE :

Calculate the summations

(10)

While loop:

while (condition) commands...

end

(11)

Examples

x=1 •

;

while x<=100 • disp(x) •

;

x=x+1 •

;

end •

(12)

EX :

Write out the values of x2 for all positive integer values x such that x3 < 2000.

x = 1;

while x^3 < 2000 disp(x^2)

x = x+1;

end

(13)

n = 10 ;

while n > 0

disp('Hello World') n = n - 1

;

end

(14)

IF Statement

if (expression) commands ...

elseif (expression) commands ...

else

commands ...

end

(15)

a=50

;

if a<10 b=a/2

;

else b=a/5

;

end

(16)

if x >= 0 & x <= 1 f = x;

elseif x > 1 & x <= 2 f = 2-x;

else f = 0;

end

(17)

Conditional Statements

if logical_expression1 • statements1 •

elseif logical_expression2 • statements2 •

else •

statements3 •

end •

(18)

if 2>4 • a=5 •

elseif 2<1 • a=6 •

else • a=10 •

end •

(19)

The MATLAB Command switch

clc;

n=input('Please input the figure:','s') switch n

case ('triangle') n=3;

sum=(n-3).*(180) case ('square') n=4;

sum=(n-3).*(180) case('pentagon') n=5;

sum=(n-3).*(180) case ('hexagon') n=6;

sum=(n-3).*(180) end

(20)

color = 'rose

;'

switch lower(color)

case {'red', 'light red', 'rose'}

disp('color is red')

case 'blue '

disp('color is blue')

case 'white '

disp('color is white')

otherwise

(21)

x=input('Enter the value of N : ') u=input('Enter the value of s : ')

switch u

case {'in','inch'} % multiple matches

y = 2.54*x; % converts to centimeters

disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)])

% disp is used to print pretty in the command window

% in the above a string vector is being printed

case {'m','meter'}

y = x*100; % converts to centimeters

disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)])

case { 'millimeter','mm'}

y = x/10

;

disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)])

case {'cm','centimeter'}

y = x

;

disp ([num2str(x) ' ' u ' converted to cm is :' num2str(y)])

otherwise

disp (['unknown units:' units])

(22)

 

 20

1

) sin(

i

i

A 

HW.3

Referensi

Dokumen terkait