Skip to Main Content
UC Logo
Libraries | Ask the Libraries

MATLAB Resources

Introduction to MATLAB and MATLAB Scripting

Control Statements

Comparison Operators

  • Equal to '='
  • Not equal to '~='
  • Less than '<'
  • Greater than '>'
  • Less than and equal to '<='
  • Greater than and equal to '>='

Logical Operators

  • Logical AND (&)
  • Logical OR (|)
  • Logical NOT (~)

Loops

Conditional Loops

  • If statement consist of a Boolean expression followed by one or more statements

                                 if(condition)

                                     statement(s)

                                 end

  • If...else statement - If statement followed by an optional else statement which executes when the Boolean expression is false

                                if(condition)

                                     statement(s)

                                else

                                     statements(s) 

                                end

  • If...elif..else statement - If statements followed by multiple else if (elif) statements to check multiple conditions will be present in this type of control statement. Finally when all conditions fail there will be else statement followed by a set of statements to perform the operation

                              if(condition)

                                  statements

                             .

                             .

                             elif(condition)

                                 statements

                              .

                              .

                              else(condition)

                                  statements

                              end

For Loops

  • For loop executes the statements specified a number of times

                    for index = initial value : step size: final value

                        statement(s)

                    end

  • The increment of the array starts with the initial value and ends when the value is greater than the final value
  • Step size can also be negative
  • The index of For loop can also be an array
  • When the index of For loop is an array then the loop executed for the length of the array

While Loops

  • While loop repeats the execution of the statements while the condition is true

                    while(expression)

                        statements(s)

                    end

  • Infinite loop may occur (Use with caution !) when:

          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

 

          

Examples

%%%%%%%%%%
%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

Contact Us | Staff Directory

University of Cincinnati

Alerts | Clery and HEOA Notice | Notice of Non-Discrimination | eAccessibility Concern | Privacy Statement | Copyright Information

© 2021 University of Cincinnati