0% found this document useful (0 votes)
5 views

VB Lecture III23

The document discusses control structures for selection and repetition in programming. It covers if/then/else statements, comparison and logical operators, and select case statements for selection. It also discusses do/loop statements for pretest and posttest loops, infinite loops, and for/next statements for counter-controlled loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

VB Lecture III23

The document discusses control structures for selection and repetition in programming. It covers if/then/else statements, comparison and logical operators, and select case statements for selection. It also discusses do/loop statements for pretest and posttest loops, infinite loops, and for/next statements for counter-controlled loops.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

Lecture III

Control structures: The Selection & Repetition Structures


Control structures

Every procedure in an application is written using one or more
of three basic control structures:
 sequence, selection, and repetition.

They are called control structures because they control the
order in which a procedure’s instructions are processed.
The Selection Structure

If…Then…Else statement

Comparison operators

Logical operators

String comparisons

Nested selection structures

Select Case statement
Selection Structures
Selection Structures


The condition in a selection structure must be phrased so that it
evaluates to an answer of either true or false.

A single-alternative selection structure requires one or more actions to
be taken only when its condition evaluates to true.

A dual-alternative selection structure , on the other hand, requires one
set of instructions to be followed only when the condition is true and a
different set of instructions only when it is false.

The instructions to follow when the condition evaluates to true are called
the true path.

The instructions to follow when the condition evaluates to false are called
the false path.
If…Then…Else

The If…Then…Else statement is used for coding single-alternative and
dual-alternative selection structures.

The condition must be a Boolean expression and can contain:
 variables, named constants, literals, properties, methods, keywords, arithmetic
operators, comparison operators, and logical operators.
Comparison Operators

Comparison operators (also referred to as relational operators) are
used to compare two values.


When making comparisons, keep in mind that:

 equal to (=) is the opposite of not equal to (<>),

 greater than (>) is the opposite of less than or equal to (<=),

 and less than (<) is the opposite of greater than or equal to (>=).


Expressions containing a comparison operator always evaluate to a
Boolean value: either True or False.
Comparison Operator Example: Total Due Application

The Total Due application displays the total amount a customer
owes,
 which is based on the number of items purchased.

Each item costs $8.50.

However, the customer receives a 10% discount when the
number of items purchased is at least 5.
Button click code: Variable declaration
Button click code: If..End if
Comparison Operator Example: Net Income/Loss Application

The Net Income/Loss application calculates either a company’s net
income or its net loss.

It displays the net income using a black font and displays the net loss
using a red font.

The statement in the selection structure’s true path will be processed
only when the decNet variable’s value is less than 0.

Otherwise, which means the variable’s value is greater than or equal
to 0, the statement in the false path will be processed.
Comparison Operator Example: Net Income/Loss Application

User Interface
Comparison Operator Example: Net Income/Loss Application

Variable declaration
Comparison Operator Example: Net Income/Loss Application

If...Else...End If
Logical Operators
Operator Operations Precedence
Not reverses the truth-value of the condition; True becomes 1
False, and False becomes True
And all subconditions must be true for the compound 2
condition to evaluate to True
AndAlso same as the And operator, except performs 2
short-circuit evaluation
Or only one of the subconditions needs to be true for the 3
compound condition to evaluate to True
OrElse same as the Or operator, except performs 3
short-circuit evaluation
Xor one and only one of the subconditions can be true for the 4
compound condition to evaluate to True
Logical Operators

Truth Table
Logical Operator Example: Gross Pay Calculator Application

The Gross Pay Calculator application calculates and displays an
employee’s weekly gross pay, given the number of hours worked and
the hourly pay rate.

The number of hours worked must be greater than 0 but less than or
equal to 40.

If the number of hours worked is not valid, the application should
display N/A (for Not Available).
Logical Operator Example: Gross Pay Calculator Application

.
Logical Operator Example: Gross Pay – AndAlso


Variable declaration
Logical Operator Example: Gross Pay – OrElse

Variable declaration
String Comparison – Assignment

Develop a simple application that makes use of methods Trim,
ToUpper, and ToLower. Present your interface and code in hard copy
on 17th July. Each student is required to submit a unique solution
(copy work means cancellation of assignment).
Nested Selection Structures

When either a selection structure’s true path or its false path contains
another selection structure,
 the inner selection structure is referred to as a nested selection
structure
 because it is contained (nested) entirely within the outer selection
structure.
Nested Selection Structures – Example

Voter eligibility checker
 Two different code versions of code on next slides.
Nested Selection Structures – Example

Voter eligibility checker
Nested Selection Structures – Example

Voter eligibility checker
Select Case Statement

When a multiple-alternative selection structure has many paths from
which to choose,
 it is often simpler and clearer to code the selection structure using
the Select Case statement rather than several If…Then…Else
statements.
Select Case Statement

Select Case Statement

Range of Values – Example
Select Case Statement

Range of Values – Example
Repetition Structures

Do…Loop statement (pretest loop)

Infinite loops

Do…Loop statement (posttest loop)

For…Next statement
Repetition Structures

Programmers use the repetition structure, referred to more simply as a loop,
 when they need the computer to repeatedly process one or more
program instructions.

The loop contains a condition that controls whether the instructions are
repeated.

It can either specify the requirement for repeating the instructions – looping
condition

or the requirement for not repeating them – loop exit condition.

When the condition is at the top of the loop, the loop is referred to as a pretest
loop.

And posttest loop when at the bottom.
Repetition Structures – Do…Loop Statement (Pretest Loop)

The loop’s condition, must follow either the keyword While or
Until.

The While keyword is used in a looping condition to specify that
the loop body should be processed while the condition is true.

The Until keyword is used in a loop exit condition to specify that
the loop body should be processed until the condition becomes
true, at which time the loop should stop.

Let’s consider the examples below:
Do…Loop Statement (Pretest Loop) – Example

Printing integers from 1 – 5.
Do…Loop Statement (Pretest Loop) – Example

Printing integers from 1 – 5: While keyword.
Do…Loop Statement (Pretest Loop) – Example

Printing integers from 1 – 5: Until keyword.
Repetition Structures – Infinite Loops

A loop that has no way to end is called an infinite loop or an endless
loop .


An infinite loop is many times created when the loop body does not
contain an instruction that will make either;
 the looping condition evaluate to False or the loop exit condition
evaluate to True.

An infinite loop is stopped by clicking Debug on the menu bar and
then clicking Stop Debugging.


Or, by clicking the Stop Debugging button (the red square) on the
Standard toolbar.
Repetition Structures – Do…Loop Statement (Posttest Loop)

Except for the location of the keyword (While or Until) and the
condition, the syntax is the same as the pretest loop.
Do…Loop Statement (Posttest Loop) – Example

Printing integers from 1 – 5: While
Do…Loop Statement (Posttest Loop) – Example

Printing integers from 1 – 5: Until
Repetition Structures – For…Next Statement

Unlike the Do…Loop statement, which can be used to code both pretest
and posttest loops.

The For…Next statement can be used to code only a specific type of
pretest loop, called a counter-controlled loop.

A counter-controlled loop is a loop whose processing is controlled by a
counter.

Counter-controlled loop are used when the computer needs to process
the loop instructions a precise number of times.

Although the Do…Loop statement can be used, the For…Next statement
provides a more compact and convenient way of writing that type of loop.
Repetition Structures – For…Next Statement

U
Repetition Structures – For…Next Statement

U
Repetition Structures – For…Next Statement

U
Repetition Structures – For…Next Statement

Comparing the For…Next and Do…Loop Statements

You might also like