CS3 if statement Visual Basic Flashcards
The Decision Structure allows the code to do different actions based on conditions; in other words it makes decisions.
The If…Then Statement
The If…Then Statement Causes Other Statements to Execute Only Under a Certain Condition
condition’s syntax relational operators
n Usually a condition is formed using a relational operator n A relational operator determines if a specific relationship exists between two values n > Greater than n < Less than n = Equal to (you’ll learn how to distinguish between = relational operator and = assignment statement from the ‘context) n <> n >= n <= Not equal to Greater than or equal to Less than or equal to
binary operatros
Relational operators use two operands, e.g. length>width Islengthgreaterthanwidth? size <= 10 Is size less than or equal 10? n Relational operators yield a True or False result, or a Boolean value
operands
operand ‘3’ is one of the inputs (quantities) followed by the addition operator, and the operand ‘6’ is the other input necessary for the operation.
Thus far, our code has been executed sequentially in a sequence structure
n To write meaningful programs we need multiple paths of execution
n Somestatementsshouldbeexecutedundercertain circumstances in a decision structure
n Thisunitpresentsthemeanstoexecute statements conditionally
THE AND OPERATOR CHART
THRUTH TABLE
THE Or OPERATOR
THRUTH TABLE
NOTE ITS WRONG TO SAY
n Ifgrade>0And<60 Then
n Theabovewillcauseasyntaxerror ngrade>0isavalidcondition
n But<60isnotavalidcondition
n Recalleachconditionrequirestwooperandsand one relational operator.
PRECEDENCE OF LOGICAL OPERATORS
n Logical operators have an order of precedence just as arithmetic operators do
n From highest to lowest precedence n Not
n And n Or n Xor
n As with arithmetic operations, parentheses are often used to clarify order of operations
the decision structure
Flowchart of a typical decision structure
n Evaluate the condition
Is it cold outside?
Is it cold outside?
False
True
If yes (True), wear a coat, if not (False), in this example, do nothing.
if then statements syntax
If condition Then statement
(more statements as needed)
End If
n New keywords used above: n If
n Then n End
conditina of reals sytaz’s relational operators
Usually a condition is formed using a relational operator
n A relational operator determines if a specific relationship exists
between two values n > Greater than
n < Less than
n = Equal to
(you’ll learn how to distinguish between = relational operator and = assignment statement from the ‘context)
n <> n >= n <=
Not equal to
Greater than or equal to Less than or equal to
binary operators
Relational operators use two operands, e.g.length>width Islengthgreaterthanwidth?
size <= 10 Is size less than or equal 10?
n Relational operators yield a True or False result, or a Boolean value
operators
An operator in a programming language is a symbol that tells the compiler or interpreter to perform specific mathematical, relational or logical operation and produce final result.
IF…THEN RULES
he If and the Then keywords must be on the same line
n Only a comment may follow Then on the same line n The End If must be on a separate line
n Only a comment may follow the End If
Recall a Keyword is a reserved word that has a special meaning to the compiler. Do not use keywords as variable or control names as this may result in a syntax error