You are on page 1of 7

11/11/23 4:57 AM C:\MyFi...

1 of 7

clc, clear, close all, format compact


%%
clc, clear, close all, format compact
% Control Statements ..
% 1- Relational Operators ..
3 > 8
a = 3>8
b = 5<10
c = a + b *6 % Using relational operators in
math Expressions ..
y=(6<10)+(7>8)+( 5 * 3 == 60 / 4 )
b=[15 6 9 4 11 7 14], c=[8 20 9 2 19 7 10]
d = b==c , e = b~=c , f=b-c>0

A=[2 9 4; -3 5 2; 6 7 -1] % Define a matrix A


B=A<=2

% The results of a relational operation with


vectors, which are vectors with 0s and
% 1s, are called logical vectors and can be used
for addressing vectors. When a logical vector is
used for addressing another vector, it extracts
from that vector the
% elements in the positions where the logical
vector has 1s.
11/11/23 4:57 AM C:\MyFi... 2 of 7

r = [8 12 9 4 23 19 10]
s = r<10
t =r(s)
% Numerical vectors and arrays with the numbers
0s and 1s are not the same as
% logical vectors and arrays with 0s and 1s.
Numerical vectors and arrays can not
% be used for addressing.

k = [ 1 1 0 1 0 1 1 0]
% tt=r(k) Make Err..
%%
clc, clear, close all, format compact
% Order of precedence:
% Order of precedence: In a mathematical
expression that includes relational and
% arithmetic operations, the arithmetic
operations (+, –, *, /, \) have precedence
% over relational operations. The relational
operators themselves have equal precedence and
are evaluated from left to right. Parentheses
can be used to alter the
% order of precedence.

a = 3+4<16/2
b = 3+(4<16)/2
11/11/23 4:57 AM C:\MyFi... 3 of 7

%%
clc, clear, close all, format compact
% Logical operators & , | , ~
a = 3&4 % both numbers are nonzero ..
b = 0 & 5
c = 4 | 0
d = ~ 15
t=25*((12&0)+(~0)+(0|5)) % Using logical
operators in a math expression
x=[9 3 0 11 0 15]; y=[2 0 13 -11 0 4];
x&y
z=x|y
x + y
~(x+y)
%%
%%
clc, clear, close all, format compact
% Order of precedence:
% Precedence Operation
% 1 (highest) Parentheses (if nested parentheses
exist, inner ones have
% precedence)
% 2 Exponentiation
% 3 Logical NOT (~)
11/11/23 4:57 AM C:\MyFi... 4 of 7

% 4 Multiplication, division
% 5 Addition, subtraction
% 6 Relational operators (>, <, >=, <=, ==, ~=)
% 7 Logical AND (&)
% 8 (lowest) Logical OR ( | )
x=-2; y=5;
a = -5< x <-1
b = -5<x & x<-1
c = ~(y<7)
d = ~y<7
e = ~((y>=8)|(x<-1))
f = ~(y>=8)|(x<-1)
%% Built-in logical functions
clc, clear, close all, format compact
A=[6 2 15 9 7 11]; B=[6 2 15 9 0 11];
and(A,B)
or(A,B)
not(A)
%% 6.2 CONDITIONAL STATEMENTS
% The if-end Structure

% Ex: A worker is paid according to his hourly


wage up to 40 hours,
% and (50%) more for
% overtime. Write a program in a script file
that calculates the pay
11/11/23 4:57 AM C:\MyFi... 5 of 7

% to a worker. The program asks the user to


enter the number of hours
% and the hourly wage. The program then displays
the pay.
%Ans ..
% t=input('Please enter the number of hours
worked ');
% h=input('Please enter the hourly wage in $ ');
% Pay=t*h;
% if t>40
% Pay=Pay+(t-40)*0.5*h;
% end
% fprintf('The worker''s pay is $ %5.2f',Pay)

%% 6.2.2 The if-else-end Structure


clc, clear, close all, format compact
a =5
if a>3
b = 7
else
b = 9
end

%% 6.2.3 The if-elseif-else-end Structure


% t=input('Please enter the number ');
% if t > 5
11/11/23 4:57 AM C:\MyFi... 6 of 7

% b = 9
% elseif t < 5
% b = 15
% else
% b = 6
% end

%% 6.3 THE switch-case STATEMENT out of scope


..

%% 6.4 LOOPS

%% 6.4.1 for-end Loops


clc, clear, close all, format compact
a = 0
for k=1:3:10
x = k^2
a = a+1;
end
a % to know number of iteration ..

% n=input('Enter the number of terms ' );


% S=0; Totlal = 0;
11/11/23 4:57 AM C:\MyFi... 7 of 7

% for k=1:n
% S = S + n % no. of items ..
% i = 1
% Totlal = Totlal + k(i) % sum of numbers
contains in vector
% i = i +1;
% end
% S
% a=1:5
% a(4)

%% 6.4.2 while-end Loops


clc, clear, close all, format compact
x=1
while x<=15
x=2*x
end

You might also like