1 / 35

CIS162AD

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. Flowcharting. A flowchart is a pictorial representation of an algorithm or logical steps.

dagan
Download Presentation

CIS162AD

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CIS162AD Loops, Lists, Printing 10_loops.ppt

  2. Overview of Topics • While Loop • Do While Loop • For Loop • Pretest vs Posttest • Nested Loops • List and Combo Boxes • Printing Controls

  3. Flowcharting • A flowchartis 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.

  4. Input or Output Begin or End Decision Processing Branch or Direction of Flow Flowchart Symbols

  5. 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) Flow Control Structures

  6. 4. 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.

  7. While is a Pretest Loop - Example intCount = 3; //initialize controlling variable while (intCount > 0) { txtCount.Text = intCount.ToString(“N0”); intCount--; //subtract one } Output: 3 2 1 

  8. While 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.

  9. Flowchart – While Pretest Loop Initialization important for Do-While Pretest Loop count = 3 while count > 0 False True Output count Represents Loop Skip or Exit Loop count -1 Next statement

  10. Do-While is a Posttest Loop -Example intCount = 3; //initialize controlling variable do { txtCount.Text = intCount.ToString(“N0”); intCount--; } while (intCount > 0); Output: 3 2 1 

  11. 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 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.

  12. Flowchart – Do-While Posttest Loop count = 3 Output count Loop will be executed at least one time, because the condition is at the bottom. count - 1 Represents Loop while count > 0 True False - Exit Loop Next statement

  13. 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.

  14. Loop 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).

  15. Nested Loops 3 1 2 3 2 1 2 3 1 1 2 3 intCount = 3; while (intCount > 0) { txtCount.Text = intCount.ToString(“N0”); intCount--; intCount2 = 1; do{ txtCount.Text = intCount2.ToString(“N0”); intCount2++; } while (intCount2 < 4); }

  16. 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}

  17. For Loop //For includes initialization and ending condition for (int intCount = 1; intCount < 4; intCount++;) { txtCount.Text = intCount.ToString(“N0”); } Output: 1 2 3

  18. 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 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.

  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.

  20. Nested For-Next Layout for (i = 1; i <= 10; i++) {for (j = 1; j <= 20; j++){ for (k = 1; k <= 15; k++) { //body }} }

  21. 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.

  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.

  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

  25. Collection Concepts 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.

  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.

  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;

  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.

  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.

  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.

  31. 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

  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.

  34. Print 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.

  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).

More Related