Comparison Operators
Logical Operators
Conditional Loops
if(condition)
statement(s)
end
if(condition)
statement(s)
else
statements(s)
end
if(condition)
statements
.
.
elif(condition)
statements
.
.
else(condition)
statements
end
For Loops
for index = initial value : step size: final value
statement(s)
end
While Loops
while(expression)
statements(s)
end
a) Loop condition cannot be possibly wrong
b) Logic of the loop prevents the loop condition from becoming false
Incase of running into a infinite loop press ctrl+C in the command window
%%%%%%%%%%
%Illustration of If..elif..else loop
%To Print the
%Chinese
%Zodiac
%year
%
%%%%%%%%%
kk=input('enter your birth year in complete 4 digits: ');
yearvalue=mod(kk,12);
if (yearvalue==0)
'You were born on a monkey year'
elseif (yearvalue==1)
'you were born on a rooster year'
elseif (yearvalue==2)
'you were born on a dog year'
elseif(yearvalue==3)
'you were born on a pig year'
elseif(yearvalue==4)
'you were born on a rat year'
elseif(yearvalue==5)
'you were born on an Ox year'
elseif(yearvalue==6)
'you were born on a tiger year'
elseif(yearvalue==7)
'you were born on a rabbit year'
elseif(yearvalue==8)
'you were born on a dragon year'
elseif(yearvalue==9)
'you were born on a snake year'
elseif(yearvalue==10)
'you were born on a horse year'
else(yearvalue==11)
'you were born on a sheep year'
end
%%%%%%%%%%%%%
%
%Illustration of For Loop
%Program to generate
%random numbers
%
%
%%%%%%%%%%%%
clear all
clc
aa=32310901;
xx=1729;
zz=2^24;
oldseed=input('Please enter a seed value: ');
newseed=zeros(1,100);
for ii=1:1:100
step1value=(aa*oldseed)+xx;
newseed(ii)=mod(step1value,zz);
oldseed=newseed(ii);
end
%%%%%%%%%%%%%
%
%Illustration of While Loop
%Program to determine
%the number of years
%to double the investment
%
%%%%%%%%%%%%
clc
clear all
rate=8.0;
initbalance=150000;
target=2*initbalance;
balance=initbalance;
year=0;
while(balance<target)
year=year+1;
interest=balance*rate/100;
balance=balance+interest;
end
'the investment will double in'
year
University of Cincinnati Libraries
PO Box 210033 Cincinnati, Ohio 45221-0033
Phone: 513-556-1424
University of Cincinnati
Alerts | Clery and HEOA Notice | Notice of Non-Discrimination | eAccessibility Concern | Privacy Statement | Copyright Information
© 2021 University of Cincinnati