Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks.

Similar presentations


Presentation on theme: "CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks."— Presentation transcript:

1 CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks

2  Complex. a + b i a and b are real numbers i is an imaginary number. ( i 2 = -1 ) Different types of Variables  Integer. positive whole numbers {1, 2, 3,... } negative whole numbers {-1, -2, -3,... } zero {0} Matlab Notation N= a+bi or N=a+bj Matlab Notation N= a+bi or N=a+bj  Real. real number. Matrix index (ex: B(2,1) ) Counters All calculus results… Complex calculus Geometry Vector calculus A= 5+10i; B=2.5+20.2j; Examples: Numerical Variables

3  Character/string. Strings of alphanumeric elements Different types of Variables Matlab Notation A=’string’ Matlab Notation A=’string’ All labels and titles. Filenames Strings and characters follow the same rules as other matrices, with each character counting for one element. >>myname=’James’; >>whos myname Name Size Bytes Class myname 1x5 10 char array Example: name=’ James’; Date=’October 7th’; Examples: Character/string Variables

4 Logical Variables  Logical Variables or Boolean. Logical expression with 2 states: 0 or 1 which means: false or true Condition statements Decision making >>A=true A = 1 >> whos A Name Size Bytes Class A 1x1 1 logical array Example: >>B=false B = 0 >> whos B Name Size Bytes Class B 1x1 1 logical array Example: Different types of Variables

5 Structured Programming Initialization Calculation Results Input Initialization Sequential Programming Structured Programming Input Calculation Calculation 1Calculation 2 Initialization Result 1Result 2 ? Decision making Condition statements

6 Relational Operators  Decision making uses comparison of logical variables  Comparison is done by creating logical expressions Format of SIMPLE Logical Expressions: expression1 relational-operator expression2 relational- operator Comparison ==Is equal to >Is greater than <Is smaller than >=Is greater or equal to <=Is smaller or equal to ~=Is not equal to >> A=1; B=2; >> A==B Example: ans = 0 >> A>B ans = 0 >> A<B ans = 1 >> A>=B ans = 0 >> A<=B ans = 1 >> A~=B ans = 1

7 Logical Operators Format of COMPOUND Logical Expressions: (exp1 relational-op exp2) Logical operator (exp3 relational-op exp4) Logical operator operation &and |or xoror (exclusive) ~not ABC= A&B 000 010 100 111 ABC= A|B 000 011 101 111 AB C= xor(A,B) 000 011 101 110 Truth Table ~(A&B) 1 1 1 0 ~xor(A,B) 1 0 0 1 ~(A|B) 1 0 0 0

8 Logical Variables >> A=1; B=2; >> (A==B) & (A>B) Examples: >> (A<B) & (A==B) ans = 0 >> (A==B) | (A>B)ans = 0 >> (A<B) | (A==B) ans = 1 >> xor( (A==B), (A<B) ) ans = 1 >> ~(A<B) ans = 0 >> xor( (A~=B), (A<B) ) >> ~(A>B) ans = 1 ans = 0 >> (A>0) & (B>A)ans = 1 >> (A>0) & (B>A)&(B<0) ans = 0

9 Structured Programming Format of if statement: if Logical Expression Statements …… end if True False Statement Format of if else statement: if Logical Expression Statement 1 else Statement 2 end if True False Statement1Statement2

10 Example: iftest1.m % Program to test the if statement #1 X=input(‘Enter value for x:’); if X>=0 Y=sqrt(X); fprintf(‘The squareroot of %3.2f is %4.3f’,X,Y) end % Program to test the if statement #1 X=input(‘Enter value for x:’); if X>=0 Y=sqrt(X); fprintf(‘The squareroot of %3.2f is %4.3f’,X,Y) end >>iftest1 Enter value for x: 9 The squareroot of 9.00 is 3.0000 >>iftest1 Enter value for x: 9 The squareroot of 9.00 is 3.0000 Input X Calculate If X>=0 Initialization Display Result End of script True False >>iftest1 Enter value for x: -2 >> Structured Programming

11 Example: iftest2.m % Program to test the if statement #2 X=input(‘Enter value for x:’); if X>=0 Y=sqrt(X); fprintf(‘The squareroot of %3.2f is %3.4f’,X,Y) else disp(‘x is negative: there is no real result’) end % Program to test the if statement #2 X=input(‘Enter value for x:’); if X>=0 Y=sqrt(X); fprintf(‘The squareroot of %3.2f is %3.4f’,X,Y) else disp(‘x is negative: there is no real result’) end >>iftest2 Enter value for x: 3 The squareroot of 9.00 is 3.0000 >>iftest2 Enter value for x: -2 x is negative: there is no real result >> >>iftest2 Enter value for x: 3 The squareroot of 9.00 is 3.0000 >>iftest2 Enter value for x: -2 x is negative: there is no real result >> Input X Calculate If X>=0 Initialization Display Result End of script True False Display NO Result Structured Programming

12 Format of if elseif else statement: if Logical Expression Statements 1 elseif Logical Expression Statements 2 else Statements 3 end if True False Statement1 elseif Statement2 Statement3 True False Structured Programming

13 Example: iftest3.m % Program to test the if statement #3 X=input(‘Enter value for x:’); if X>0 disp(‘x is positive’); elseif X<0 disp(‘x is negative’); else disp(‘x equal 0’); end % Program to test the if statement #3 X=input(‘Enter value for x:’); if X>0 disp(‘x is positive’); elseif X<0 disp(‘x is negative’); else disp(‘x equal 0’); end >>iftest3 Enter value for x: 3 x is positive >>iftest3 Enter value for x: -2 x is negative >>iftest3 Enter value for x: 3 x is positive >>iftest3 Enter value for x: -2 x is negative Input X If X>0 Initialization End of script True False Display Result >0 Display Result <0 If X<0 Display Result = 0 True False Structured Programming

14 Problem: Pick a random number N (-2<N<2) Calculate B= “nesting “ If N positive calculate: A=log(N) if A positive calculate: B=sqrt(A) % nested “if statements” example N= rand(1)*4-2; if N>=0 A=log(N); if A>0 B=sqrt(A); end % nested “if statements” example N= rand(1)*4-2; if N>=0 A=log(N); if A>0 B=sqrt(A); end % nested “if statements” example N= rand(1)*4-2; if N>=0 A=log(N); if A>0 B=sqrt(A); end % nested “if statements” example N= rand(1)*4-2; if N>=0 A=log(N); if A>0 B=sqrt(A); end Use indentation (Tab key) Structured Programming

15 The “ SWITCH” structure switch variable case test1 Statement 1 case test2 Statement 2 ….. otherwise Statement n end Statement3 Switch Statement nStatement1 Statement4Statement2 Structured Programming

16 Introduction to Mechanical Engineering ME 102 Introduction to Mechanical Engineering The “ SWITCH” structure % program to test switch A=input('Your choice [1,2 3] ? '); switch A case 1 disp('Choice 1') case 2 disp('Choice 2') case 3 disp('Choice 3') otherwise disp('Wrong choice') end % program to test switch A=input('Your choice [1,2 3] ? '); switch A case 1 disp('Choice 1') case 2 disp('Choice 2') case 3 disp('Choice 3') otherwise disp('Wrong choice') end >> Testswitch Your choice [1,2 3] ? 1 Choice 1 >> Testswitch Your choice [1,2 3] ? 7 Wrong choice >> Testswitch Your choice [1,2 3] ? 2 Choice 2 >> Testswitch Your choice [1,2 3] ? 3 Choice 3

17 Example1 Write a script example.m to find roots of a second order equation ax 2 +bx+c=0. When the script is executed it will –ask the user enter the coefficients a,b,c –calculate discriminant –calculate the roots and display the case according to sign of discriminant.

18 Example2 Write a script that allows a user to enter a string containing a day of a week (“Sunday”, “Monday” etc) uses a switch construct to convert the day to its corresponding number, where Moday is the first day of the week. Print out the resulting day number. Also be sure to handle the case of an illegal day name.


Download ppt "CSE123 Lecture 3 Different types of Variables Logical operators, Conditional Statements and If Blocks."

Similar presentations


Ads by Google