Different types of Variables Logical operators and Conditional
Download
Report
Transcript Different types of Variables Logical operators and Conditional
CSE123 Lecture 3
Different types of Variables
Logical operators, Conditional Statements
and If Blocks
Different types of Variables
Numerical Variables
Integer.
positive whole numbers {1, 2, 3,... }
negative whole numbers {-1, -2, -3,... }
Matrix index (ex: B(2,1) )
Counters
zero {0}
Real.
real number.
Complex.
a+bi
• a and b are real numbers
All calculus results…
Complex calculus
Geometry Vector calculus
• i is an imaginary number. ( i2= -1 )
Matlab Notation
N= a+bi
or
N=a+bj
Examples:
A= 5+10i;
B=2.5+20.2j;
Different types of Variables
Character/string Variables
Character/string.
Strings of alphanumeric elements
Matlab Notation
A=’string’
All labels and titles.
Filenames
Examples:
name=’ James’;
Date=’October 7th’;
Example:
>>myname=’James’;
>>whos myname
Strings and characters follow the same rules
as other matrices, with each character
counting for one element.
Name
Size Bytes Class
myname
1x5
10 char array
Different types of Variables
Logical Variables
Logical Variables or Boolean.
Logical expression with 2 states:
Condition statements
Decision making
0 or 1
which means:
false or true
Example:
Example:
>>A=true
A= 1
>> whos A
Name
Size Bytes Class
>>B=false
B= 0
>> whos B
Name
Size
A
1x1
1
logical array
B
1x1
Bytes Class
1
logical array
Structured Programming
Structured Programming
Sequential Programming
Initialization
Initialization
Input
Initialization
Input
Calculation
Calculation
Results
Decision
making
?
Calculation 1
Result 1
Condition
statements
Calculation 2
Result 2
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
relationaloperator
Comparison
==
Is equal to
>
Is greater than
<
Is smaller than
>=
Is greater or equal to
<=
Is smaller or equal to
~=
Is not equal to
Example:
>> A=1; B=2;
>> A==B
ans =
0
>> A>B
>> A<B
>> A>=B
>> A<=B
>> A~=B
ans =
ans =
ans =
ans =
ans =
0
1
0
1
1
Logical Operators
Format of COMPOUND Logical Expressions:
(exp1 relational-op exp2) Logical operator (exp3 relational-op exp4)
Logical
operator
operation
&
|
and
or
xor
~
or (exclusive)
not
Truth
Table
A
B
C= A|B
~(A|B)
0
0
0
1
0
1
1
0
1
0
1
0
1
1
1
0
A
B
C= A&B
~(A&B)
A
B
C= xor(A,B)
~xor(A,B)
0
0
0
1
0
0
0
1
0
1
0
1
0
1
1
0
1
0
0
1
1
0
1
0
1
1
1
0
1
1
0
1
Logical Variables
Examples:
>> A=1; B=2;
>> (A==B) & (A>B)
>> (A<B) & (A==B)
ans = 0
ans = 0
>> (A==B) | (A>B)
>> (A<B) | (A==B)
ans =
ans =
0
1
>> xor( (A==B), (A<B) )
>> xor( (A~=B), (A<B) )
ans =
ans =
1
0
>> ~(A<B)
>> ~(A>B)
ans =
ans =
0
1
>> (A>0) & (B>A)
ans = 1
>> (A>0) & (B>A)&(B<0) ans = 0
Structured Programming
Format of if statement:
False
if
if Logical Expression
Statements
……
end
Format of if else statement:
if Logical Expression
Statement 1
else
Statement 2
end
True
Statement
if
False
True
Statement1
Statement2
Structured Programming
Example: iftest1.m
Initialization
Input X
False
If
X>=0
True
Calculate
X
Display Result
End of script
% 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: -2
>>
Structured Programming
Example: iftest2.m
Initialization
Input X
% 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
False
If X>=0
True
Calculate
X
Display Result
End of script
Display
NO 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
>>
Structured Programming
Format of if elseif else statement:
if Logical Expression
Statements 1
elseif Logical Expression
Statements 2
else
Statements 3
end
if
False
True
Statement1
elseif
False
True
Statement2
Statement3
Structured Programming
Example: iftest3.m
Initialization
Input X
False
If X>0
True
Display Result >0
False
% 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
If X<0
True
Display Result <0
Display Result = 0
End of script
>>iftest3
Enter value for x: 3
x is positive
>>iftest3
Enter value for x: -2
x is negative
Structured Programming
“nesting “
Problem:
% nested “if statements” example
•Pick a random number N
(-2<N<2)
Calculate B=
N= rand(1)*4-2;
log( N )
•If N positive calculate: A=log(N)
•if A positive calculate: B=sqrt(A)
if N>=0
A=log(N);
A=log(N);
if A>0
if A>0
B=sqrt(A);
B=sqrt(A);
endend
end
Use
indentation
(Tab key)
Structured Programming
The “SWITCH” structure
switch variable
case test1
Statement 1
case test2
Statement 2
…..
otherwise
Statement n
end
Switch
Statement1
Statement3
Statement2
Statement n
Statement4
ME 102 Introduction to Mechanical Engineering
The “SWITCH” structure
% program to test switch
>> Testswitch
A=input('Your choice [1,2 3] ? ');
Your choice [1,2 3] ? 1
switch A
case 1
disp('Choice 1')
case 2
disp('Choice 2')
case 3
disp('Choice 3')
otherwise
disp('Wrong choice')
end
Choice 1
>> Testswitch
Your choice [1,2 3] ? 2
Choice 2
>> Testswitch
Your choice [1,2 3] ? 3
Choice 3
>> Testswitch
Your choice [1,2 3] ? 7
Wrong choice
Example1
Write a script example.m to find roots of a
second order equation
ax2+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.
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.