CS3 if statement Visual Basic Flashcards

1
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

The If…Then Statement

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

condition’s syntax relational operators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

binary operatros

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

operands

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

THE AND OPERATOR CHART

THRUTH TABLE

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

THE Or OPERATOR

THRUTH TABLE

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

NOTE ITS WRONG TO SAY

A

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

n Recalleachconditionrequirestwooperandsand one relational operator.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

PRECEDENCE OF LOGICAL OPERATORS

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

the decision structure

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

if then statements syntax

A

If condition Then statement

(more statements as needed)

End If

n New keywords used above: n If

n Then n End

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

conditina of reals sytaz’s relational operators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

binary operators

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

operators

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

IF…THEN RULES

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

IF THEN CONVENTIONS

A

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
Q

RELATIONAL OPERATORS WITH MATH OPERATORS

A

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
Q

Either or both relational operator operands may be function calls

A
22
Q

BOOLEAN VARIABLES AS FLAGS

A

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
Q

= HAS TWO MEANINGS

A

Relational Operator: If x = y Then

‘….some code End If

n Assignment Statement: Quantity = Textbox1.Text

24
Q

THE IF… THEN…ELSE STATEMENT

A

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
Q

The If…Then construct will execute or ignore a group of statements

(do something or do nothing)

A
26
Q

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

A
27
Q

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

A
28
Q

THE IF … THEN ELSE… ELSE IF

A

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
Q

TWO MUTUALLY EXCLUSIVE CHOICES

A

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
Q

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

A

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

n In pseudo code (not VB yet):

31
Q

Each of the series of conditions in an If…Then… ElseIf is tested in sequence

A
32
Q

When a condition is true, the remaining conditions are ignored

A
33
Q

The order of the conditions is vital
n Wrongordercanresultinwrongdecision

n Whatifit’schillyandwindy?

n Ifwindyistestedbeforechilly,you’dgooutwitha windbreaker when you need a jacket

A
34
Q

IN VISUAL BASIC SNYTAX

If condition1

Then Statement(s)1

Elseif condition2

Then Statements(s)2

Elseif condition3

Then Statements3

End If

A
35
Q

EXAMPLE OF ElseIf USAGE

A
36
Q
A
37
Q

THE (OPTIONAL) TRAILLING ElseIf

A

n A sequence of ElseIf’s may end with a plain Else, called a trailing Else

n If none of the conditions are True, the trailing Else statement(s) will be executed

n Remember, you code based on the logic (the flowchart).

38
Q

NESTED IF STATEMENTS-

OPTIIONAL TOPICS

A

A Nested If Statement Is an If Statement in the Conditionally Executed Code of Another If Statement

Focus on the previous slides then move to Nested If Statements

39
Q

Any type of statement may be used inside thestatement(s) portion of any form of If

n This includes other If statements

n If statements within If statements create a more complex decision structure called a Nested If

A

IF STATEMENTS WITHIN IF STATEMENTS

40
Q

NESTED IF STATEMENTS

A

If sngSalary > 30000 Then If intYearsOnJob > 2 Then

lblMessage.Text = “The applicant qualifies.” Else

lblMessage.Text = “The applicant does not qualify.” End If

Else If intYearsOnJob > 5 Then

lblMessage.Text = “The applicant qualifies.” Else

lblMessage.Text = “The applicant does not qualify.” End If

End If

Note how the convention of indentations

41
Q

LOGICAL OPERATORS

A

Logical Operators Connect Two or More Relational Expressions Into One, or Reverse the Logic of an Expression

42
Q

VISUAL BASIC LOGICAL OPERATORS

A

Not

Reverses the logical value of an expression

43
Q

VISUAL BASIC LOGICAL OPERATORS

A

Xor

One operand (but not both) must be true for the overall expression to be true, otherwise it is false

44
Q

VISUAL BASIC LOGICAL OPERATORS

A

Xor

One operand (but not both) must be true for the overall expression to be true, otherwise it is false

45
Q

VISUAL BASIC LOGICAL OPERATORS

A

Or

One or both operands must be true for the overall expression to be true, otherwise it is false

46
Q

VISUAL BASIC LOGICAL OPERATORS

A

Operator Effect
And Both operands must be true for the overall

expression to be true, otherwise it is false