Starting Out With Visual: Fourth Edition
Starting Out With Visual: Fourth Edition
Fourth Edition
Chapter 5
Loops, File, and Random
Numbers
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Topics
5.1 More About ListBoxes
5.2 The while Loop
5.3 The and Operators
5.4 The for Loop
5.5 The do-while Loop
5.6 Using Files for Data Storage
5.7 The OpenFileDialog and SaveFileDialog Controls
5.8 Random Numbers
5.9 The Load Event
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.1 More About ListBoxes
• ListBox controls have various methods and properties that you can use in
code to manipulate the ListBox’s contents
• The Items.Add method allows you to add an item to the ListBox control
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.2 The while Loop
• The while loop causes a statement or set of statements to repeat as
long as a Boolean expression is true
• The simple logic is: While a Boolean expression is true, do some task
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Structure of a while Loop
• In C#, the generic format of a while loop is:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
The while Loop is a Pretest Loop
• A while loop tests its condition before performing an iteration.
• It is necessary to declare a counter variable with initial value
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Sample Code (1 of 4)
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Infinite Loops
• An infinite loop is a loop that will repeats until the program is
interrupted
• There are few conditions that cause a while loop to be an infinite
loop. A typical scenario is that the programmer forgets to write code
that makes the test condition false
• In the following example, the counter is never increased. So, the
Boolean expression is never false.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
and
5.3 The increment Operators
and decrement
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Postfix Mode v s Prefix Mode
ersu
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.4 The for Loop
• The for loop is specially designed for situations requiring a counter variable
to control the number of times that a loop iterates
• You must specify three actions:
– Initialization: a one-time expression that defines the initial value of the
counter
– Test: A Boolean expression to be tested. If true, the loop iterates.
– Update: increase or decrease the value of the counter
• A generic form is:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Sample Code (2 of 4)
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Other Forms of Update Expression
• In the update expression, the counter variable is typically
incremented by 1. But, this is not a requirement.
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.5 The do-while Loop
• The do-while loop is a posttest loop, which means it performs an iteration
before testing its Boolean expression.
• In the flowchart, one or more statements are executed before a Boolean
expression is tested
• A generic format is:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Sample Code (3 of 4)
• Will you see the message box?
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.6 Using File for Data Storage
• When a program needs to save data for later use, it writes the data in a file
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
File Accessing
• A file object is an object that is associated with a specific file
and provides a way for the program to work with that file
• The .NET Framework provide two classes to create file objects
through the System.IO namespace
– StreamWriter: for writing data to a text file
– StreamReader: for reading data from a text file
• You need to write the following directives at the top of your
program
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Writing Data to a File
• Start with creating a StreamWriter object
• Use one of the File methods to open the file to which you will
be writing data. Sample File methods are:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Sample Code (4 of 4)
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
CreateText v s AppendText ersu
• The previous code uses the File.CreateText method for the following
reasons:
– It creates a text file with the name specified by the argument. If the file
already exists, its contents are erased
– It creates a StreamWriter object in memory, associated with the file
– It returns a reference to the StreamWriter object
• When there is a need not to erase the contents of an existing file, use the
AppendText method
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Specifying the Location of an Output File
• If you want to open a file in a different location, you can
specify a path as well as filename in the argument
• Be sure to prefix the string with the @ character
•
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Reading Data from a File
• Start with creating a StreamReader object
• Use the File.OpenText method to open the file to which you will be
writing data
• Use the Read or ReadLine method to write items of data to the file
– StreamReader.ReadLine: Reads a line of characters from the
current stream and returns the data as a string.
– StreamReader.Read: Reads the next character or next set of
characters from the input stream.
• Close the connection
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Reading a File with a Loop
• StreamReader objects have a Boolean property named
EndOfStream that signals whether or not the end of file has
been reached
• You can write a loop to detect the end of the file.
• Or
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.7 The OpenFileDialog and SaveFileDialog
Controls
• The OpenFileDialog and SaveDialog controls allow your application to display
standard Windows dialog boxes for opening and saving files
• Unlike Label, Button, and TextBox, they are invisible controls
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Displaying an Open Box (1 of 2)
• When adding an OpenFileDialog control to the form, it does
not appear on the form, but in an area at the bottom of the
Designer called the component tray
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Displaying an Open Box (2 of 2)
• In code, you can display an Open dialog box by calling the
ShowDialog method:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Detecting the User’s Selection
• The showDialog method returns a value that indicates which button
the user clicks to dismiss the dialog box
– If the user clicked the Open button, the value
DialogResult.OK is returned
– If the user clicked the Cancel button, the value
DialogResult.Cancel is returned
– The following is an example that calls the ShowDialog method
to determine the user’s choice:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
The Filename and InitialDirectory Property
• When the user selects a file with the Open dialog box, the file’s path
and filename are stored in the control’s Filename property
• The following is an example of how to open the selected file:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Displaying a Save as Dialog Box
• Use the following to call the SaveFileDialog control’s
ShowDialog method
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.8 Random Numbers
• The .NET Framework provides the Random class to generate random
numbers.
• To create an object, use:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Syntax of Random.Next Method
• Random.Next generates a random number whose value ranges
from zero to 2,147,483,647
• It also allow you to generate a random number whose value
ranges from zero to some other positive number. The syntax is:
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
5.9 The Load Event
• When running an application, the application’s form is loaded into
memory and an event known as Load takes place
• To create a Load event handler, simply double click the form in the
Designer
• An empty Load event handler looks like:
• Any code you write inside the Load event will execute when
the form is launched. For example,
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved
Copyright
Copyright © 2017, 2014, 2012 Pearson Education, Inc. All Rights Reserved