CS3 if statement Visual Basic Flashcards Preview

CS 3 INTRO TO PROGAMING > CS3 if statement Visual Basic > Flashcards

Flashcards in CS3 if statement Visual Basic Deck (46)
Loading flashcards...
1

The Decision Structure allows the code to do different actions based on conditions; in other words it makes decisions.

2

The If...Then Statement

The If...Then Statement Causes Other Statements to Execute Only Under a Certain Condition

3

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

4

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

5

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.

6

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

7

THE AND OPERATOR CHART 

THRUTH TABLE 

8

THE Or OPERATOR

THRUTH TABLE 

9

NOTE ITS WRONG TO SAY 

n  Ifgrade>0And<60 Then
n  Theabovewillcauseasyntaxerror ngrade>0isavalidcondition
n  But<60isnotavalidcondition

n  Recalleachconditionrequirestwooperandsand one relational operator.

10

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

11

12

13

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.

14

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

15

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

16

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

17

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.

18

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

19

IF THEN CONVENTIONS

The code between the If...Then and the End If is indented

n  Visual Basic does not require this
n  It is a convention among programmers to aid in the

readability of programs

n  By default, the Visual Basic editor will automatically do this indentation as you type your code

20

RELATIONAL OPERATORS WITH MATH OPERATORS 

Either or both relational operator operands may be expressions

n  Math operators are evaluated before relational

operators

n  x+yanda-bareevaluatedfirst
n  Each result is then compared using

21

Either or both relational operator operands may be function calls

22

BOOLEAN VARIABLES AS FLAGS 

A flag is a Boolean variable that signals when some condition exists in the program

n  Since a Boolean variable is either True or False, it can be used as the condition of an If

n  Notethatanoperatorisnotrequired

23

= HAS TWO MEANINGS 

Relational Operator: If x = y Then

‘....some code End If

n  Assignment Statement: Quantity = Textbox1.Text

24

THE IF... THEN...ELSE STATEMENT 

The If...Then...Else Statement Executes One Group of Statements If the Condition Is True and Another Group of Statements If the Condition Is False

25

The If...Then construct will execute or ignore a group of statements
(do something or do nothing)

26

The If...Then...Else construct will execute one group of statements or another group (do this or do that)

27

If temperature < 40 Then
lblMesage.Text = “A little cold, isn’t it?”

Else
lblMesage.Text = “Nice weather we’re having!”

End If

 

IF THEN... ELSE EXAMPLE 

28

THE IF ... THEN ELSE... ELSE IF 

The If...Then...Elseif Statement Is Like a Chain of If...Then...Else Statements

They Perform Their Tests, One After the Other, Until One of Them Is Found to Be True

29

TWO MUTUALLY EXCLUSIVE CHOICES 

The If...Then...Else has two choices
n  The condition will be True or False
n  So one of the two choices must be selected
n  They are mutually exclusive (only one can be true)

30

 

MULTIPLE MUTUALLY EXCLUSIVE CHOICES 

 

Wear a coat

Elseif it is chilly Wear light jacket

Elseif it is windy Wear a windbreaker

Elseif it is hot Wear no jacket

The If...Then...ElseIf statement allows for an entire series of mutually exclusive choices

n  In pseudo code (not VB yet):