Lecture 5 Looping Statements

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

Looping Statements

Lecture 5
INTERMEDIATE PROGRAMMING

Engr. Jennelyn P. Cabale


Loop
• A loop is a basic programming construct that allows repeated execution
of a fragment of source code. Depending on the type of the loop, the
code in it is repeated a fixed number of times or repeats until a given
condition is true (exists).

• Loops that never end are called infinite loops. Using an infinite loop is
rarely needed except in cases where somewhere in the body of the loop
a break operator is used to terminate its execution prematurely.

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 2


While Loops
while (condition)
{
loop body;
}

• condition is any expression that returns a Boolean result – true or false.


It determines how long the loop body will be repeated and is called
the loop condition.

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 3


Do-While Loops
• The do-while loop is similar to the while loop, but it checks the
condition after each execution of its loop body. This type of loops is
called loops with condition at the end (post-test loop). A do-while
loop looks like this:

do
{
executable code;
} while (condition);

• Initially the loop body is executed. Then its condition is checked. If it


is true, the loop’s body is repeated, otherwise the loop ends. This
logic is repeated until the condition of the loop is broken. The body
of the loop is executed at least once. If the loop’s condition is
constantly true, the loop never ends.

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 4


For Loops

• They contain an initialization block


(A), condition (B), body (D) and
updating commands for the loop
variables (C).

for (initialization; condition; update)


{
loop's body;
}

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 5


Example : Printing 1-10

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 6


Example
• The Getting to a Million Club wants an application that allows a club
member to enter two items: the amount of money deposited into a
savings account at the beginning of the year and the annual interest rate.
The application should display the balance in the savings account at the
end of the year, assuming the interest is compounded annually and no
withdrawals or additional deposits are made.

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 8


Example
• The Sales Express Company wants an application
that displays the average amount the company
sold during the prior year. The sales manager will
enter the amount of each salesperson’s sales.
The application will use a counter to keep track of
the number of sales amounts entered and an
accumulator to total the sales amounts. When
the sales manager has finished entering the sales
amounts, the application will calculate the
average sales amount by dividing the value
stored in the accumulator by the value stored in
the counter. It then will display the average sales
amount. If the sales manager does not enter any
sales amounts, the application should display the
message “N/A” (for “not available”).

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 10


The InputBox Function
• Displays a predefined dialog box that allows the user to enter data
• Contains a text message, an OK button, a Cancel button, and an input
area
• Syntax:
–InputBox(Prompt, Title, defaultResponse)

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 12


Including a List Box in an Interface
• Displays a list of choices from which the user can select zero or more
choices

• SelectionMode property: controls the number of choices a user can


select
–None: user can scroll but not select anything
–One: user can select one item
–MultiSimple and MultiExtended: user can select multiple items

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 13


Adding Items to a List Box
• Items collection: a collection of the items in a list box

• Collection: a group of one or more individual objects treated as one unit

• Load event of a form: occurs when an application is started and the form
is displayed for the first time

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 14


• Syntax:
–object.Items.Add(item);

• Add method: adds an item to the list box’s Items collection


• Items to be added must be converted to String
• String Collection Editor window can be used to specify list items during
design time

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 15


Example

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 16


Example

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 19


Sorted Property
• Determines if the list box items are sorted
• Sort order is dictionary order

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 21


Accessing Items in a List Box
• Index:
–A unique number that identifies an item in a collection
–Is zero-relative: the first item has index of 0

• Syntax:
–object.Items(Index);

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 23


Determining the Number of Items
in a List Box
• Items.Count property: stores the number of items in a list box
–Count value is always one higher than the highest index in the list box

• Syntax:
–object.Items.Count;

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 24


The SelectedItem and SelectedIndex Properties
• SelectedItem property:
–Contains the value of the selected item in the list
–If nothing is selected, it contains the empty string

• SelectedIndex property:
–Contains the index of the selected item in the list
–If nothing is selected, it contains the value -1

• Default list box item:


–the item that is selected by default when the interface first appears

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 25


THE END

Engr. Jenn P. Cabale Lecture 5 - LOOPING STATEMENTS 30

You might also like