[go: up one dir, main page]

0% found this document useful (0 votes)
11 views39 pages

CSC301 - Chapter 3

The document discusses various programming concepts like decision structures, logical operators, string comparisons, and message boxes. It provides syntax and examples for if-then and if-then-else statements, select case statements, and displaying message boxes. String handling concepts like case sensitivity and methods like ToUpper and ToLower are also covered.

Uploaded by

kimhyejoo
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)
11 views39 pages

CSC301 - Chapter 3

The document discusses various programming concepts like decision structures, logical operators, string comparisons, and message boxes. It provides syntax and examples for if-then and if-then-else statements, select case statements, and displaying message boxes. String handling concepts like case sensitivity and methods like ToUpper and ToLower are also covered.

Uploaded by

kimhyejoo
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/ 39

CHAPTER 3

DECISIONS & CONDITIONS


Use block lfs and nested lfs to control the flow of logic

Use logical operator to combine multiple Boolean expressions

Comparing, testing and working with string

Use Select Case statement

Create message boxes to display error conditions as input validation


DECISION STRUCTURES

 Tells the computer that it needs to make a decision before it can


select the next instruction to process.
 The decision is based on a condition specified at the beginning of the decision
structure.
 The condition in a decision structure must be phrased so that it evaluates to an
answer of either true or false.
 The next instruction to process is based on the result of that decision.
IF…THEN  Visual Basic provide If…Then statement for coding
single-alternative decision structures.
STATEMENT  Requires one or more actions to be taken ONLY when
(1 OF 3) its condition evaluates to true.
IF…THEN STATEMENT (2 OF 3)

 Syntax of the If…Then statement:

 The condition must be a Boolean expression.


 Can contain variables, named constants, literals, properties, methods, keywords,
arithmetic operators, comparison operators, and logical operators
 Figure shows an example of code for single-
IF…THEN STATEMENT
(3 OF 3) alternative decision structure for
btnCalc_Click procedures
 Visual Basic provide If…Then…Else statement for coding
IF…THEN…ELSE dual-alternative decision structures.
STATEMENT  Requires one set of instructions to be followed only when
(1 OF 3) the condition is true and a different set of instructions
only when it is false.
IF…THEN…ELSE STATEMENT (2 OF 3)

 Syntax of the If…Then…Else statement:


 Figure shows an example of code for dual-
IF…THEN…ELSE
STATEMENT (3 OF 3) alternative decision structure for
btnCalc_Click procedures
IF…THEN…ELSEIF
STATEMENT (1 OF 3)
 Also known as multiple-
alternative decision
structures.
 Some procedures require a
decision structure that can
choose from several
different alternatives.
IF…THEN…ELSEIF STATEMENT (2 OF 3)
 Syntax of the If…Then…ElseIf statement:
If condition Then
statement block
ElseIf condition Then
statement block
(put as many ElseIf statements as necessary)
Else
statement block
(more statements may follow)
EndIf

 Each of the series of condition is tested in sequence.


 When a condition is true, the remaining conditions are ignored.
IF…THEN…ELSEIF  Figure shows an example of code for multiple-
STATEMENT (3 OF 3) alternative decision structure
NESTED SELECTION
STRUCTURES (1 OF 3)

 A selection structure that is


wholly contained (nested) within
either the true or false path of
another selection structure.
 The nested structure in is
processed only when the outer
structure’s condition evaluates to
True.
NESTED SELECTION STRUCTURES (2 OF 3)
 Figure shows the code for the
NESTED SELECTION
STRUCTURE (3 OF 3)
btnDisplay_Click procedures, where
the nested structures are shaded in the figure.
List and examples of commonly used comparison operators

COMPARISON
OPERATORS (1 OF 2)
 Comparison or
relational operators are
used to compare two
values.
 Expressions containing a
comparison operator
always evaluate to a
Boolean value: either
True or False.
COMPARISON Evaluation steps for expressions containing arithmetic and
OPERATORS (2 OF 2) comparison operators.

 Comparison operators in
Visual Basic do not have an
order of precedence.
 When an expression contains
more than one comparison
operator, the computer
evaluates the comparison
operators from left to right in
the expression.
 However, comparison
operators are evaluated after
any arithmetic operators as in
examples shown.
OPERATOR OPERATIONS PRECEDENCE
LOGICAL NUMBER
OPERATORS (1 OF 3) Not Reverses the truth-value of the condition;True becomes 1
False, and False becomes True
 When more than one
And All subconditions must be true for the compound 2
condition is included in an condition to evaluate to True
If...Then...Else statement,
AndAlso As soon as a condition is found to be false, no further 2
the conditions are called a
conditions are tested and the compound condition is false
compound condition.
 Logical operator allows Or Only one of the subconditions needs to be true for the 3
you to combine two or compound condition to evaluate to True
more conditions into a
OrElse As soon as a condition is found to be true, no further 3
compound condition that conditions are tested and the compound condition is true
can be tested with an If
statement.
Xor One and only one of the subconditions can be true for the 4
compound condition to evaluate to True
LOGICAL
OPERATORS (2 OF 3)

 Example of using logical


operators in the
If…Then…Else
statement’s condition.
LOGICAL OPERATORS (3 OF 3)

 The truth tables shown the


summarize how the computer
evaluates the logical operators
in an expression.
STRING COMPARISONS (1 OF 3)
 String comparisons in Visual Basic are case sensitive.
 The uppercase version of a letter and its lowercase counterpart are not interchangeable.
 For example, a human recognizes K and k as being the same letter, a computer does not;
to a computer, a K is different from a k.
 String comparisons that involve user input can be problematic because the
user might enter the string using any combination of uppercase and lowercase
letters.
 ToLower method – temporarily converts a string to lowercase
 ToUpper method – temporarily converts a string to uppercase
 The syntax and examples of the ToUpper and ToLower methods
as shown:

STRING
COMPARISONS
(2 OF 3)
STRING
COMPARISONS
(3 OF 3)

 Figure shows three ways


of writing the
If…Then…Else
statement using
ToUpper method and
ToLower Method
 In some programming applications, different operations can
occur based on the value in a single field.
 The first line starts with Select Case and is followed by a test
expression (may be any numeric or string expression).
 Starting on the next line is a sequence of one or more Case
statements and is followed by an expression list.
SELECT CASE  Statements are executed if the value of the test expression
matches any of the expressions in the Case statement’s
STATEMENT expression list.
 The Case Else section is optional.
(1 OF 5)
 In some programming applications,
SELECT CASE different operations can occur
based on the value in a single field
STATEMENT (2 OF 5)
SELECT CASE  Here is an example of Select Case
STATEMENT (3 OF 5) expressions.
SELECT CASE STATEMENT (4 OF 5)

 You can use relational operators


in the Case statement, as shown
by the following example.
 The Is keyword represents the
test expression in the relational
comparison
SELECT CASE STATEMENT (5 OF 5)

 You can determine whether the test expression


falls within a range of values.
 Requires the To keyword, as shown in the
example.
 The numbers used on each side of the To
keyword are included in the range.
USER INTERFACE DESIGN

Message Box

Radio Buttons

Check Boxes
DISPLAY A MESSAGE
BOX (1 OF 5)

 In Visual Basic, the


MessageBox
method can be call
to pop up a message
window.
DISPLAY A
MESSAGE BOX (2
OF 5)

 General format to
display message box
statement.
 A message box can be displayed during
execution with a variety of arguments.
 In many applications, the caption is used to
give further information to the user.
DISPLAY A
MESSAGE
BOX (3 OF 5)
DISPLAY A MESSAGE BOX (4 OF 5)
MsgBoxButtons Arguments Value Use
MsgBoxStyle.OKOnly 0 Displays an OK button —
default setting
MsgBoxStyle.OKCancel 1 Displays an OK and
Cancel button
MsgBoxStyle.AbortRetryIgnore 2 After a failing situation,
the user can choose to
Abort, Retry, or Ignore
MsgBoxStyle.YesNoCancel 3 Displays Yes, No, and
Cancel buttons
MsgBoxStyle.YesNo 4 Displays Yes and No
buttons
MsgBoxStyle.RetryCancel 5 After an error occurs, the
user can choose to Retry
or Cancel
DISPLAY A
MESSAGE BOX (5
OF 5)
 To create the message in the message box,
you can use concatenation.

STRING
CONCATENATION

MsgBox(“You entered ” & decFootage.ToString()


& “. Enter a Positive Number”, , “Input
Error”)
RADIO BUTTONS (1 OF
2)
 Are used want the user to
select one choice from
several possible choices.
 The RadioButton control
displays a single radio
button.
 RadioButton controls are
normally grouped in one of
the following ways:
 All radio buttons inside a
GroupBox control are
members of the same group.
 All radio buttons located on
a form but not inside a
GroupBox control are
members of the same group.
 Commonly used properties of a radio button.

RADIO
BUTTONS (2
OF 2)  In the following example code, assume that radCoffee, radTea,
and radSoftDrink are radio buttons within the same group.
 A check box provides an option that the user can
either choose to select or choose not to select.
 The option offered by each check box must be
unique and unrelated to any of the other check
box options.
 Commonly used properties of a check box as shown
CHECK BOX below.
(1 OF 3)
 The following code demonstrates
CHECK BOX (2 OF 3) check boxes.
END OF CHAPTER 3

You might also like