You are on page 1of 36

Computer Programming

Week 8
Program Control Statements-3

17.11.2014

1

This chapter discusses C#’s program control statements. There
are three categories of program control statements:
1-selection statements, 

if 
switch;
2-iteration statements, 
for, 
while, 
do-while loops;
3-jump statements, 
break, 
continue, 
goto,

17.11.2014

2

The while Loop 

Another of C#’s loops is the while. The general form of the while
loop is
while (condition) statement; 

where statement can be a single statement or a block of
statements, and condition defines the condition that controls the
loop and may be any valid Boolean expression. The statement is
performed while the condition is true. When the condition
becomes false, program control passes to the line immediately
following the loop. 

Here is a simple example in which a while is used to compute the
order of magnitude of an integer (Figure 8.1):

17.11.2014

3

11.2014 4 .17.

11.2014 5 .Figure 8.1 While example 17.

2 While example-2.1 6 .Consider a code segment designed to find the first powers of 3 less than 100 17.11.2014 Figure 8.

11.17.2014 7 .

in which the condition is tested at the top of the loop. thus preventing confusion with the while. they are often used to improve readability of the do-while construct. the do-while loop checks its condition at the bottom of the loop. Unlike the for and the while loops.  Although the braces are not necessary when only one statement is present.3): 17. The do-while loop executes as long as the conditional expression is true.  The following program uses a do-while loop to display the digits of an integer in reverse order (Figure 8. This means that a dowhile loop will always execute at least once. } while(condition). The general form of The do-while loop is do { statements.11.The do-while Loop   The third C# loop is the do-while.2014 8 .

2014 9 .11.17.

Figure 8. this results in the leftmost digit being removed. Next.2014 10 . the value in num is divided by 10. This digit is then displayed. This process repeats until num is 0.3 Do-while example  Here is how the loop works: With each iteration. Since this is an integer division. the leftmost digit is obtained by computing the remainder of an integer division by 10. 17.11.

4 Do-while example 2 17.output the numbers 1–10 by count variable Figure 8.11.2014 11 .

and program control resumes at the next statement following the loop. the loop is terminated.Using break to Exit a Loop  It is possible to force an immediate exit from a loop.  Here is a simple example (Figure 8.2014 12 .  When a break statement is encountered inside a loop.5): 17. by using the break statement. bypassing any code remaining in the body of the loop and the loop’s conditional test.11.

17.11.2014 13 .

6): 17. the break statement will break out of only the inner most loop.  When used inside a set of nested loops. The use of break in this situation prevents the loop from trying any other values once a factor has been found. For example (Figure 8.5 Break example  The break stops the for loop as soon as a factor is found. thus preventing inefficiency.2014 14 .Figure 8.11.

11.17.2014 15 .

2014 16 .Figure 8.11.6 Using break in nested loops 17.

2014 17 . 17. continue is essentially the complement of break.11. This is accomplished using continue. skipping any code in between. The continue statement forces the next iteration of the loop to take place.  For example. bypassing the loop’s normal control structure. the following program uses continue to help print the even numbers between 0 and 100 (Figure 8.7).Using continue  It is possible to force an early iteration of a loop. Thus.

Figure 8.2014 18 .11.7 Using continue 17.

11.8 Using continue-2 17.Skip displaying 5 between 1-10 Figure 8.2014 19 .

However. Thus. Technically.  Thegoto requires a label for operation.  Here is an example that illustrates goto with a switch (Figure 8.9): 17.The goto  The goto is C#’s unconditional jump statement. you cannot use the goto to jump into a switch statement. A label is a valid C# identifier followed by a colon. they can be targets of a goto.11. That is. The label must be in the same method as the goto that uses it and within scope. the goto statement must be executed from within the switch. program flow jumps to the location specified by the goto. When encountered.  The goto can also be used to jump to a case or default statement within a switch. the case and default statements of a switch are labels.2014 20 .

17.2014 21 .11.

2014 22 .Figure 8.11.9 Using goto 17.

2014 23 .11.  One way to solve this problem is to use a special value called a sentinel value (also called a signal value. The user enters grades until all legitimate grades have been entered. a dummy value or a flag value) to indicate “end of data entry. the averaging calculation is likely to produce a number with a decimal point—in other words. a real number or floating-point number. 17.  Although each grade is an integer.Exercise  Develop a class-averaging application that processes grades for an arbitrary number of students each time it’s run.” This is called sentinel-controlled repetition.

11.Introducing class and objects 17.2014 24 .

2014 25 . This chapter introduces the class. 17. Furthermore. The class is the foundation of C# because it defines the nature of an object.11. the class forms the basis for object-oriented programming. Within a class are defined both code and data.

 In general terms.11. most real-world classes contain both. For example. data is contained in data members defined by the class. and code is contained in function members. It is important to state at the outset that C# defines several specific flavors of data and function members. you declare the data that it contains and the code that operates on it.The General Form of a Class  When you define a class. While very simple classes might contain only code or only data.2014 26 . 17. data members (also called fields) include instance variables and static variables.

Access type varN. // declare methods Access ret-type method1(parameters) { // body of method } Access ret-typemethod2(parameters) { // body of method } // .2014 27 . Access ret-typemethodN(parameters) { // body of method } 17. Access type var2.A class is created by use of the keyword class..11. Here is the general form of a simple class definition that contains only instance variables and methods: class classname { // declare instance variables Access type var1.... // .

2014 28 . such as public. the classes that we have been using have had only one method: Main( ). The access specifier determines what type of access is allowed. then the member is private to the class. which specifies how the member can be accessed. notice that the general form of a class does not specify a Main( ) method. which means that they can be used by all other code—even code defined outside the class.11. Notice that each variable and method declaration is preceded with access.  For the examples in this chapter. and if absent.  The access specifier is optional.  As mentioned earlier. class members can be private to a class or more accessible. all members (except for the Main( ) method) will be specified as public.However. Members with private access can be used only by other members of their class. A Main( )method is required only if that class is the starting point for your program. 17. Here. access is an access specifier.  Up to this point.

and it will store three items of information about a building: the number of floors.11. Area.2014 29 . we will be evolving a class that encapsulates information about buildings.  The first version of Building is shown here. such as houses.10) 17. Notice that Building does not contain any methods. offices. stores.Define a Class  To illustrate classes. (Subsequent sections will add methods to it. it is currently a data-only class. It defines three instance variables: Floors. Thus. This class is called Building. the total area.) (Figure 8. and Occupants. and so on. and the number of occupants.

and varname is the variable’s name.  Here.  Here is a complete program that uses the Building class (Figure 8.11): 17. For Building. access specifies the access. Thus.10 Example of data-only class  The instance variables defined by Building illustrate the way that instance variables are declared in general. As explained. this allows them to be accessed by code outside of Building. type specifies the type of variable.2014 30 . you declare an instance variable in the same way that you declare local variables.11.Figure 8. aside from the access specifier. the variables are preceded by the public access modifier. The general form for declaring an instance variable is shown here: access type var-name.

11.17.2014 31 .

11.2014 32 .Figure 8.11 Creating object example 17.

Remember that a class declaration is only a type description. // create an object of type building 17.11. Thus.2014 33 . it does not create an actual object. In this case. A class definition creates a new data type.  To actually create a Building object. the new data type is called Building. You will use this name to declare objects of type Building. you will use a statement like the following: Building house = new Building(). the preceding code does not cause any objects of type Building to come into existence.

The general form of the dot operator is shown here: object. and Occupants. you are creating an object that contains its own copy of each instance variable defined by the class. you will use the member access operator.member 17. Each time you create an instance of a class. The dot operator links the name of an object with the name of a member. To access these variables. Area. every Building object will contain its own copies of the instance variables Floors.2014 34 .  It is commonly referred to as the dot operator.  Thus.11. which is a period.

use the following statement: house.2014 35 . and the member is put on the right. the object is specified on the left. 17.Floors = 2. For example. to assign the Floors variable of house the value 2.11. Thus. you can use the dot operator to access both instance variables and methods.  In general.

17. code inside BuildingDemo can access the members of Building because they are declared public.  Then the code within Main( ) accesses the instance variables associated with house.11. and BuildingDemo would not have been able to use them. This program consists of two classes: Building and BuildingDemo. their access would have been limited to the Buildingclass. Inside BuildingDemo. assigning them values and using those values.  It is important to understand that Building and BuildingDemo are two separate classes. the Main( ) method creates an instance of Building called house.  Although they are separate classes.  If they had not been given the public access specifier.2014 36 . The only relationship they have to each other is that one class creates an instance of the other.