Al-Isra University Faculty of Information Technology Department of CS Programming Mathematics using MATLAB 605351 Dr. Khaled Al-Qawasmi ١ Dr. Kahled Al-Qawasmi 2010-2011
Chapter 3 Selection Statements Dr. Kahled Al-Qawasmi 2010-2011 ٢
Relational expression If statements use expressions that are conceptually or logically either true or false Relational expressions sometimes are called Boolean or logical expression. Relational operator: >, <, >=, <=, ==, ~= In MATLAB logical true is represented by the integer 1, and logical false is represented by the integer 0. In the command window demonstrates the values of the expressions: >> 3<5 ans= 1 >>9<2 ans= 0 Dr. Kahled Al-Qawasmi 2010-2011 ٣
Cont. In the workspace the type of the expression result is logical Mathematical operation could be performed on the resulting 1 or 0. >> 5<7 ans=1 >>ans+3 ans=4 Dr. Kahled Al-Qawasmi 2010-2011 ٤
Cont. Comparing characters, for example a < c is also possible. Characters are compared using their ASCII >> a < c ans=1 The logical operator are: or && and ~ not Dr. Kahled Al-Qawasmi 2010-2011 ٥
Logical operator All logical operators operate on logical or Boolean operands The not operator is a unary operator The others are binary Not operator give the opposite value for example, ~(3<5) is false since (3<5) is true. In addition to these logical operators, MATLAB has a function xor, which is the exclusive or function. >>xor(3<5, a > c ) ans=1 >>xor(3<5, a < c ) ans=0 Dr. Kahled Al-Qawasmi 2010-2011 ٦
Operator precedence rules (). and ^ - ~ * / \ + - : < <= > >= == ~= && = Dr. Kahled Al-Qawasmi 2010-2011 ٧
The if statement The general form is: if condition action For example >> num=-4 >>if num<0 num=abs(num) >> Dr. Kahled Al-Qawasmi 2010-2011 ٨
Cont. abs() can be used to make sure that the square root function is not used on a negative number: Sqrtexamp.m num= input( please enter a number: ); If num<0 disp( ok, we ll use the absolute value ) num=abs(num); fprintf( the sqrt of %.1f is %.1f\n,num,sqrt(num)); >>sqrtexamp Please enter a number:-4.2 The sqrt of 4.2 is 2.0 Dr. Kahled Al-Qawasmi 2010-2011 ٩
Quick quetion Write a function that creates a vector of increasing value from mymin to my max and return the result. Assume a=3, b=7; But if a=7, b=3, we need to make swap. createvec.m function outvec=createvec(mymin, mymax) if mymin>mymax temp=mymin; mymin=mymax; mymax=temp; End Outvrc=mymin:mymax; Dr. Kahled Al-Qawasmi 2010-2011 ١٠
Logical True and False The concept of false is represented by the integer value of 0 But the concept of true can be represented by any nonzero value (not just the integer 1) >>if 5 disp( yes, this is true ) yes this is true Dr. Kahled Al-Qawasmi 2010-2011 ١١
Cont. Simple mistake can be occurred Lets say that the user is prompted for a choice Y or N for a yes/no >>letter=input ( choice (Y/N):, s ) The script should allow the user to enter ( y or Y ) >>letter== y letter== Y Of by mistake written as: >>letter== y Y This expression always be true. Because Y is nonzero value, the first part ma be false but the second part is always true. Dr. Kahled Al-Qawasmi 2010-2011 ١٢
The if-else statement Choosing between two actions of from several action, is accomplished using if-else, nested if, and switch If-else statement form if condition action 1 else action 2 Dr. Kahled Al-Qawasmi 2010-2011 ١٣
Checkradius.m radius=input( enter radius: ); if radius<=0 fprintf( sorry;%.2f is not a valid radius\n,radius); else area=calarea(radius); fprintf( for a circle with radius of %.2f,radius); fprintf( the area is %.2f\n,area) Dr. Kahled Al-Qawasmi 2010-2011 ١٤
Nested if-else statements To choose from more than two statements Compute the following mathematical function y= f(x): Y=1 for x<-1 Y=x 2 for -1 <= x<=2 y= 4 for x>2 if x<-1 y=1; if x>=-1 && x<= 2 y=x^2; if x<2 y=4; Dr. Kahled Al-Qawasmi 2010-2011 ١٥
cont. the previous code is not efficient, but: if x<-1 y=1; else if x>=-1 && x<= 2 y=x^2; else y=4; not all conditions need to be tested Dr. Kahled Al-Qawasmi 2010-2011 ١٦
the Elseif Cluse MATLAB has another method of accomplishing neted if-else statements if x<-1 y=1; elseif x>=-1 && x<= 2 y=x^2; else y=4; write a function to determine whether an input argument is a scalar, a vector or a matrix (use elseif) Dr. Kahled Al-Qawasmi 2010-2011 ١٧
findtype.m function outtype=findtype(arg) [r c]=size(arg); if r==1 && c==1 outtype='scalar'; elseif r==1 c==1 outtype='vector'; else outtype='matrix'; Dr. Kahled Al-Qawasmi 2010-2011 ١٨
example write a function that receives an integer quiz grade, which should be in the range from 0 to 10. then the program returns a corresponding letter grade according to the following scheme: a 9 or 10 is an A, an 8 is a B, a 7 is a C, a 6 is a D, and anything below that is an F. writ a function that receive an integer then check if the number is odd or even. the mod function is the Modulus after division. mod(n1,n2). Dr. Kahled Al-Qawasmi 2010-2011 ١٩
letgrade.m function grade=letgrade(quiz) %first error check if quiz<0 quiz>10 grade='x'; elseif quiz==9 quiz==10 grade='a'; elseif quiz==8 grade='b'; elseif quiz==7 grade='c'; elseif quiz==6 grade='d'; else grade='f'; Dr. Kahled Al-Qawasmi 2010-2011 ٢٠
chk.m function chk(n) if n>0 if mod(n,2)==0 fprintf('the number%3d is even\n',n); else fprintf('the number%3d is odd\n',n); else disp('the number is not valid'); Dr. Kahled Al-Qawasmi 2010-2011 ٢١
the switch ststement a switch statement can be used in place of a nested if-else or an if statement with many elseif clause general form switch switch_expression case caseexp1 action1 case caseexp2 action2 %etc: there can be many of these otherwise action n Dr. Kahled Al-Qawasmi 2010-2011 ٢٢
example (grade example) switchgrade.m function grade=switchgrade(quiz) %first error check if quiz<0 quiz>10 grade='x'; else switch quiz case 10 grade= A'; case 9 grade='a'; case 8 grade='b' case 7 grade='c' case 6 grade='d' otherwise grade='f' Dr. Kahled Al-Qawasmi 2010-2011 ٢٣
Note: since the same action of printing A is desired for more than one case, these can be combined as follows: switch quiz case {10,9} grade= A ; case 8 grade= B ; %etc. Dr. Kahled Al-Qawasmi 2010-2011 ٢٤
the Menu function MATLAB has a built-in Function called menu that display a figure window with push buttons for the choices. the first string passed to the menu function is the heading the rest are labels that appear on the push buttons. the function returns the number of the button that is passed Dr. Kahled Al-Qawasmi 2010-2011 ٢٥
cont. for example: >>mypick=menu( pick a pizza, cheese, shroom, sausage ); the following window will appear there are three buttons whose equivalent values are 1,2 and 3 if the user press the susage button mupick would have the value 3: >> mypick mypic= 3 Dr. Kahled Al-Qawasmi 2010-2011 ٢٦
pickpizza.m mypick=menu('pick a pizza','cheese','shroom','sausage'); switch mypick case 1 disp('order cheese pizza' ); case 2 disp('order Mushroom pizza' ); case 3 disp('order sausage pizza' ); otherwise disp('no Pizza for us today'); Dr. Kahled Al-Qawasmi 2010-2011 ٢٧
Cont. if the user clicks the red X on the top-right of the menu box the value returned from the function will be 0 which will cause the otherwise clause to be executed instead of using a switch statement in this script it can be use an if-else statement with elseif clauses Dr. Kahled Al-Qawasmi 2010-2011 ٢٨
pickpizzaifelse.m mypick=menu('pick a pizza','cheese','shroom','sausage'); if mypick==1 disp('order cheese pizza' ); elseif mypick==2 disp('order mushroom pizza' ); elseif mypick==3 disp('order sausage pizza' ); else disp('follow directions next time!!!'); Dr. Kahled Al-Qawasmi 2010-2011 ٢٩
The IS Functions In MATLAB there are a lot of function that are built into MATLAB that test whether or not something is true these function names are begin with word is. these functions are frequently used in if statements. for example the function isletter return logical 1 if the argument is a letter of the alphabet, or 0 if it is not Dr. Kahled Al-Qawasmi 2010-2011 ٣٠
cont. >> isletter( 4 ) ans =0 >>isletter( h ) ans=1 also you can do the same thing using comparing characters position within the character encoding for example: mychar>= a && mychar <= z but the efficient method is: isletter(mychar) Dr. Kahled Al-Qawasmi 2010-2011 ٣١
cont. the function isempty returns logical true if a variable is empty, and logical false if it has a value, or an error message if the variable does not exist so it can be used to determine whether a variable has a value yet or not. >>clear >>isempty(evec)??? undefined function or variable evec. >>evec=[ ]; >>isempty(evec) ans=1 Dr. Kahled Al-Qawasmi 2010-2011 ٣٢
cont. >>evec=[evec 11]; >>isempty(evec) ans =0 the isempty function will also determine whether or not a string variable is empty. for example to check whether the user entered a string in the input function: >>istr = input( please enter a string:, s ); please enter a string: >>isempty(istr) ans=1 Dr. Kahled Al-Qawasmi 2010-2011 ٣٣
cont. the function iskeyword will determine whether or not a name is a keyword in MATLAB. by itself (with no argument), it will return the list of all keywords. notice that the name of functions like sin are not keywords. Dr. Kahled Al-Qawasmi 2010-2011 ٣٤
examples >> iskeyword( sin ) ans= 0 >>iskeyword( switch ) ans = 1 >> iskeyword ans= break' 'case' 'catch' 'classdef' 'continue' 'else' 'elseif %etc. Dr. Kahled Al-Qawasmi 2010-2011 ٣٥