Title: CIS162AD
1CIS162AD
- Loops, Lists, Printing
- 10_loops.ppt
2Overview of Topics
- While Loop
- Do While Loop
- For Loop
- Pretest vs Posttest
- Nested Loops
- List and Combo Boxes
- Printing Controls
3Flowcharting
- A flowchart is a pictorial representation of an
algorithm or logical steps. - Each step is represented by a symbol and the
arrows indicate the flow and order of the steps. - The shape of the symbol indicates the type of
operation that is to occur. - Flowcharts may help the move visual students
learn and understand logic.
4Flowchart Symbols
5Flow Control Structures
- The order in which statements are executed.
- There are four structures.
- Sequence Control Structure
- Selection Control Structure
- Also referred to as branching (if and if-else)
- Case Control Structure (select)
- Repetition Control Structure (loops)
64. Repetition Control (loops)
- Loops are the 4th flow control structure.
- Loop a group of statements that are repeated
until a certain condition occurs to stop it. - The conditions are Boolean expressions like those
in if statements. - The conditions evaluate to True or False.
- Can use Relational and Logical Operators.
7While is a Pretest Loop - Example
- intCount 1 //initialize controlling variable
-
- while (intCount lt 4)
-
- txtCount.Text intCount.ToString(N0)
- intCount //add one
-
- Output
- 1 2 3
8While is a Pretest Loop
- Pretest - controlling condition is evaluated
before executing loop body. - Controlling variable must be initialized.
- Condition must be true to enter loop body.
- There is NO semi-colon after the condition.
- It is possible that body is not executed at all.
- Condition must be false to get out of loop.
- Controlling variable should be modified within
the loop. - Execution continues with next statement after
Loop.
9Flowchart While Pretest Loop
Initialization important for While Pretest Loop
count 1
while count lt 4
False
True
Output count
Skip or Exit Loop
Represents Loop
count 1
Next statement
10Do-While is a Posttest Loop -Example
- intCount 1 //initialize controlling variable
-
- do
-
- txtCount.Text intCount.ToString(N0)
- intCount
- while (intCount lt 4)
- Output
- 1 2 3
11Do-While Posttest Loop
- Posttest - controlling condition is evaluated
after executing loop body. - So, body is always executed at least one time.
- Initialization of controlling variable not
necessarily required. - Condition must be false to get out of loop.
- There is a semi-colon after the condition.
- Controlling variable should be modified within
the loop. - Execution continues with next statement after
Loop.
12Flowchart Do-While Posttest Loop
count 1
Output count
Loop will be executed at least one time, because
the condition is at the bottom.
count 1
Represents Loop
while count lt 4
True
False - Exit Loop
Next statement
13Pretest vs. Posttest
- Pretest
- For pretest loops the terminating condition is at
the top of the loop. - It is possible that the body is not executed if
the condition to get into the loop is not met. - Posttest
- For posttest loops the terminating condition is
at the bottom of the loop. - The body is always executed at least one time.
14Loop Summary
- Infinite loop
- A loop that never ends.
- While condition always evaluates to true.
- Controlling variable must be altered within the
loop. - Click on the close form icon to stop the program.
- Use Control-Break to enter debug mode.
- When to use a While or Do-While will become
evident as we continue to use and learn each
loop. - Nested Loop is a loop inside another loop (see
next slide).
15Nested Loops
1
1 2 3
2
1 2 3
- intCount 1
- while (intCount lt 4)
-
- txtCount.Text intCount.ToString(N0)
- intCount
- intCount2 1
- do
- txtCount.Text intCount2.ToString(N0)
- intCount2
- while (intCount2 lt 4)
3
1 2 3
16For Loop
- Good when a task needs to be completed a fixed
number of times. - Good when counting with fixed increments.
- Compares to a While Pretest loop.for
(initialization condition action) body
17For Loop
- //For includes initialization and ending
condition -
- for (int intCount 1 intCount lt 4 intCount)
-
- txtCount.Text intCount.ToString(N0)
-
- Output
- 1 2 3
18For Loop
- Controlling variable is initialized.
- Pretest - controlling condition is evaluated
before executing loop body. - Condition must be true to enter loop body.
- It is possible that body is not executed at all.
- Condition must be false to get out of loop.
- The action (intCount) is automatically executed
when the bottom of the loop is reached, and then
the condition is evaluated again. - Do not alter the value of controlling variable
within the body. - Compares to a While Pretest loop.
19Flowchart For Pretest Loop
count 1
while count lt 4
False
True
Output count
Skip or Exit Loop
Represents Loop
count 1
Next statement
20Nested For-Next Loops
- For loops may contain other For loops.
- The second loop must be completely contained
inside the first loop. - The second loop should have a different
controlling variable. - Should indent inner loops for readability.
21Nested For-Next Layout
- for (i 1 i lt 10 i)
- for (j 1 j lt 20 j) for (k 1 k lt
15 k) //body
22List and Combo Boxes
23List and Combo Boxes
- Both allow you to have a list of items from which
the user can make a selection. - Items in the list can be set at design time or
loaded at run-time. - Space on the form and input options will help
determine which to use. - A scroll bar automatically added for long lists.
- Since the two are similar, well only review the
combo boxes here. - See textbook additional options and details.
24Collection of items
- The list of items that is displayed is called a
collection. - A collection is an object that has properties and
methods. - Properties item count, selected index
- Methods add, remove, clear
- Each item in the list is referred to an element.
- Each element is referenced by providing an index
value. - The first one is referenced with an index value
of zero.
25Design Time Use Items Property
- In the properties window there is a property
named Items. - Use the Collection Editor button to open a window
that allows you to enter the items. - One item per line (cboCatalog example) Odds and
Ends Solutions Camping Needs
26Collection Concepts
Position Index Text
1 0 Odds and Ends
2 1 Solutions
3 2 Camping Needs
There are 3 items. The first is referenced with
an index value of zero. The last one is
referenced with an index value of Items.Count
1 The item the user picks is recorded in
SelectedIndex.
27Run Time methods
- To add an item to the end of the list at run time
use the Items.Add method. cboCatalog.Items.Add(T
oolTime) cboCatalog.Items.Add(Spiegel) - To insert an item in a particular place in the
list use Items.Insert. cboCatalog.Items.Insert(1,
The Outlet) - Use the cboCatalog.Items.RemoveAt(index) or
cboCatalog.Items.Remove(strValue) to remove items
from list. - Use cboCatalog.Items.Clear( ) to remove all items
in the list.
28Selected Item
- When we are ready to process the form, well want
to record the item the user selected. - The SelectedIndex can be usedstrCatalog
cboCatalog.Items(cboCatalog.SelectedIndex) - The Text property also holds the text of the
selected item.strCatalog cboCatalog.Text
29Printing Documents
- Printing documents is not as easy as creating
forms. - There are other tools that can be used to create
reports, such as Crystal Reports. - However, there will be times when information
from an application may need to be printed. - Use the PrintDocument control by adding it to the
component tray.
30Print Page Event
- The PrintDocument control is named like other
controls, such as printDocument1. - Add a Print option using a button or menu item.
- From the button or menu click method, call the
Print method. printDocument1.Print( ) - This will fire the PrintPage event.
- In the PrintPage event method is where we place
the code to print a page.
31Graphics Page
- A graphics page is built in memory and then the
page is sent to the printer. - You must specify the exact location on the
graphics page for each element that you want to
print. - There are various methods to draw on the graphics
page, but well just cover the introductory ones
here.
32X and Y Coordinates
X
Y
33e.Graphics.DrawString Method
- Use the DrawString method to send a line of text
to the graphics page. - Pass the upper-left corner of where you want the
string placed as X and Y coordinates. - e.Graphics.DrawString(strToPrint, Font, Brush, X,
Y) - Font can be specified
- Brush is the color
34PrintPage Event Logic
- Declare coordinate, line height and font
variables. - float fltX, fltY, fltLineHeight
- Font printFont new Font(Arial, 12)
- Get left margin and top margin values using some
of the properties of the page event. - fltX e.MarginBounds.Left
- fltY e.MarginBounds.Top
- Call DrawString to place a line on the graphics
page. - Move down the page by increasing Y.Assign the
font height to line height. fltLineHeight
printFont.GetHeight( )Add the height of the
line just printed to Y. fltY fltLineHeight - When the procedure is exited, the graphics page
is sent to the printer.
35Print Preview
- Place a PrintPreviewDialog control in the
component tray. - The control is named printPreviewDialog1.
- Add a menu or button control for the user to
select, and for its event use the
code printPreviewDialog1.Document
printDocument1 printPreviewDialog1.ShowDialog(
) - Use the same PrintDocument control declared for
the printer output. - Assign the PrintDocument to the Document property
of the printPreviewDialog1 and call ShowDialog. - ShowDialog will fire the PrintPage event, so the
same code to create the page is executed for
print preview and an actual print.
36Summary
- While Loop
- Do While Loop
- For Loop
- Pretest vs Posttest
- Nested Loops
- List and Combo Boxes
- Printing Controls
- I may have over simplified the Print process in
this presentation. The best way to learn it is
to practice it (CS10).