Transcript PPT 10
CIS162AD
Loops, Lists, Printing 10_loops.ppt
Overview of Topics
While Loop Do While Loop For Loop Pretest vs Posttest Nested Loops List and Combo Boxes Printing Controls CIS162AD 2
Flowcharting
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.
CIS162AD 3
Flowchart Symbols
Input or Output Begin or End CIS162AD Processing Decision Branch or Direction of Flow 4
Flow Control Structures
The order in which statements are executed.
There are four structures.
1. Sequence Control Structure 2. Selection Control Structure • Also referred to as branching (if and if-else) 3. Case Control Structure (select) 4. Repetition Control Structure (loops) CIS162AD 5
4. Repetition Control (loops)
Loops are the 4 th 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. CIS162AD 6
While is a Pretest Loop - Example
intCount = 1; //initialize controlling variable
while
{ (intCount
<
4) txtCount.Text = intCount.ToString(“N0”); intCount++; //add one } Output: 1 2 3 CIS162AD 7
While is a Pretest Loop
Pretest - controlling condition is evaluated before executing loop body.
Controlling variable must be initialized.
Condition must be There is
true NO semi-colon
to enter loop body.
It is possible that body is Condition must be loop.
false
after the condition.
not
executed at all.
to get out of loop.
Controlling variable should be modified within the Execution continues with next statement after Loop.
CIS162AD 8
Flowchart – While Pretest Loop
count = 1 Initialization important for While Pretest Loop Represents Loop while count < 4 True False Output count Skip or Exit Loop count +1 Next statement CIS162AD 9
Do-While is a Posttest Loop -Example
intCount = 1; // initialize controlling variable do { txtCount.Text = intCount.ToString(“N0”); intCount++; } w
hile
(intCount
< 4
); Output: 1 2 3 CIS162AD 10
Do-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 There
false is a semi-colon
to get out of loop.
after the condition.
Controlling variable should be modified within the loop.
Execution continues with next statement after Loop.
CIS162AD 11
Flowchart – Do-While Posttest Loop
count = 1 Output count count + 1 Loop will be executed at least one time, because the condition is at the bottom.
Represents Loop True while count < 4 False - Exit Loop Next statement CIS162AD 12
Pretest 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.
CIS162AD 13
Loop Summary
When to use a While or Do-While will become evident as we continue to use and learn each loop. 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.
Nested Loop is a loop inside another loop (see next slide).
CIS162AD 14
} {
Nested Loops
intCount = 1; while (intCount < 4) 1 1 2 3 2 1 2 3 3 1 2 3 txtCount.Text = intCount.ToString(“N0”); intCount++; intCount2 = 1; do { txtCount.Text = intCount2.ToString(“N0”); intCount2++; } while (intCount2 < 4); CIS162AD 15
For 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 } CIS162AD 16
For Loop
//For includes initialization and ending condition for ( int intCount = 1; intCount < 4; intCount ++;) { txtCount .Text = intCount.ToString(“N0”); } Output: 1 2 3 CIS162AD 17
For 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 Condition must be
false not
executed at all.
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.
CIS162AD 18
Represents Loop CIS162AD
Flowchart – For Pretest Loop
count = 1 while count < 4 True False Output count Skip or Exit Loop count +1 Next statement 19
Nested 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.
CIS162AD 20
CIS162AD
Nested For-Next Layout
for (i = 1; i <= 10; i++) { for (j = 1; j <= 20; j++) { for (k = 1; k <= 15; k++) { //body } } } 21
CIS162AD
List and Combo Boxes
22
List 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, we’ll only review the combo boxes here.
See textbook additional options and details.
CIS162AD 23
Collection 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.
CIS162AD 24
Design 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 CIS162AD 25
Collection Concepts
Position Index Text 1 2 3 0 1 2 Odds and Ends Solutions 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.
CIS162AD 26
Run Time –methods
To add an item to the end of the list at run time use the Items.Add method.
cboCatalog.Items.Add(“ToolTime”); 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.
CIS162AD 27
Selected Item
When we are ready to process the form, we’ll want to record the item the user selected.
The SelectedIndex can be used: strCatalog = cboCatalog .Items(cboCatalog.SelectedIndex); The Text property also holds the text of the selected item.
strCatalog = cboCatalog.Text; CIS162AD 28
Printing 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.
CIS162AD 29
Print 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.
CIS162AD 30
Graphics 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 we’ll just cover the introductory ones here.
CIS162AD 31
CIS162AD
X and Y Coordinates
X Y 32
e.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 CIS162AD 33
PrintPage 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.
CIS162AD 34
Print Preview
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.
Place a PrintPreviewDialog control in the component tray.
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.
CIS162AD 35
Summary
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).
CIS162AD 36