0% found this document useful (0 votes)
18 views26 pages

2024 Spring ICS Week11

Uploaded by

behzathayri08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views26 pages

2024 Spring ICS Week11

Uploaded by

behzathayri08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

PROCEDURAL PROGRAMMING:

SELECTION / BRANCHING
SELECTION/BRANCHING
q The flow of control is handled behind the scenes by the
MATLAB interpreter.
q MATLAB interpreter is a program running in the
background that
q reads the statements that you write and carries
them out one by one,
q allocating space for variables,
q writing values into those variables, and
q reading values from them,
q accessing elements of arrays,
q calling functions, and
q displaying results on the screen.
SELECTION/BRANCHING
q Executing the statements in the order that the
programmer wrote them is called sequential control.
q Sequential control is the most natural and the most
common sequence in any program written in any
programming language.
q A control construct is simply a method by which the
interpreter selects the following statement to be
executed after the execution of the current statement
has concluded.
SELECTION/BRANCHING
q The programmer tells the interpreter which construct to
use utilizing (or not) particular keywords.
q Now, we will introduce keywords that signal the
interpreter to base its decision as to which statement is
to be executed not only on the order in which the
statements are written but also on the values of
expressions.
q This new control construct is called selection or
branching.
SELECTION - if statements
q An if-statement is used when the programmer wishes to
have the interpreter choose whether or not to execute a
statement or set of statements on the basis of the values
of variables.
SELECTION - if statements

A block is a set of
adjacent statements,
and each block in the
figure is controlled by
a control statement.
SELECTION – if-else statements
SELECTION – if-elseif-else statements
SELECTION – if-elseif statements
q If we wanted to require a bit more thinking on the part
of the person trying to answer our question, we could
omit the else clause from our example.
SELECTION - if statements
q So far, our
examples have
selected from
among one, two,
or three blocks
of code, but any
number of blocks
can be included
by including
additional elseif
keywords.
SELECTION - The return statement
q When a return-statement is executed (in any
programming language), it halts the function in which it
appears, in this case, day_of_week, and returns control
to the caller of the function.

q If that return-statement had been omitted, execution


would have continued to the final if-else-statement:
SELECTION - The conditional
q The name for the expression that follows the keywords
if and elseif is conditional.
q A conditional is the expression that determines whether
or not a block in an if-statement is executed.
q It can have one of two values—true or false.
q If it is true, the block is executed; if it is false, the block
is not executed.
q We have seen several simple examples already:
x == 2, x == 42, x < 42, n == 1,
n == 2, day_type == 1.
SELECTION - If Statement Summary
SELECTION - Relational Operators
• A relational operator produces a value that depends on the relation between
the values of its two operands.
• The operators, ==, and <, which we have seen in if-statements above, are
examples of relational operators.
SELECTION - Relational Operators
• There are six relational operators:
SELECTION - Logical Operators
• A logical operator produces a value that depends on the truth of its two
operands.
• There are three logical operators:
SELECTION - Logical Operators
• Consider the following example:

• The && operator :


• If both are true (values are nonzero), then it returns true (value of 1).
• Otherwise, it returns false (value of 0).
SELECTION - Logical Operators
• Short Circuiting:
SELECTION - Logical Operators
• the logical “or” operator, symbolized by || :
• It returns true (value of 1) if at least one of its operands is true—the first one, the second
one, or both—and (value of 0) if both operands are false.

• Consider the example using ||:


SELECTION - Logical Operators
• Short-circuiting takes place if the first operand is true, because in that case, the
||operator will return 1, regardless of the value of the second operand.
SELECTION - Logical Operators
• In the table below, the outputs of the logical “and” operator && and the logical
“or” operator || are given for their four possible inputs and the outputs for the
related function xor (“exclusive or”) is given as well.
• There is no operator for xor. It is a function that takes two inputs as for example:
xor(x,y).
SELECTION - Logical Operators
• The logical “not” operator, symbolized by ~ (called “tilde” ):
• The “not” operator is a unary prefix operator, means that it takes only one
operand and it comes before its operand.
• Consider the following example:
SELECTION - Logical Operators
• Like the relational operators, but unlike && and ||, the operator ~ is an array
operator: Thus, it can be applied to an array producing a “not” operation on
each element:
SELECTION - Logical Operators
• There are two more logical operators—the “element-wise” versions of the logical
“and” operator and the logical “or” operator.
• These operators are symbolized by a single & and a single |.
• These operators, like the relational operators and ~, are array operators: They are the
array versions of && and ||.
• Like the other binary array operators, they can also take one scalar operand and one
non-scalar operand.
SELECTION - Operator Precedence
• The table below gives the complete precedences table for all Matlab operators:

• When there are two or more operators of the same precedence, left-to-right
associativity is used, meaning that the order of operation is from left to right.
Parentheses can override this associativity rule also.
SELECTION - Nested Selection Statements
• We refer to the inclusion of one control construct inside another construct as
nesting:

You might also like