Lab05 C#
Lab05 C#
1. while Loops
The while loop is used in a situation where we need to execute a statement (or a block of
statements) repeatedly as long as the specified condition holds true. A while statement has
the following form:
while (condition )
statement ;
while (condition ) {
statement1 ;
statement2 ;
:
statementN ;
}
Example 1.1: (Counting loop) Write a C# program to print out numbers from 1 to N , where
N is given by the user.
Lab #5 1 of 8
Sample output
Please input N: 5
1
2
3
4
5
After the program starts, it asks the user to enter a value for the variable N at line 7. It
then gives an initial value 1 to the variable i at line 8. It is now ready to enter the while loop,
in which it executes statements at lines 10 and 11 until the condition (i ≤ N ) becomes false.
Exercise 1.1: Modify the program in Example 1.1 so that it will display the num-
bers from N to 1 instead. Determine what should be put in the blanks marked
(a)–(c).
1 using System ;
2 class While2 {
3 static void Main () {
4 int N ;
5 int i ;
6 Console . Write ( " Please input N : " );
7 N = int . Parse ( Console . ReadLine ());
8
9 ___ ( a ) ___ ;
10 while ( ___ ( b ) ___ ) {
11 Console . W r i t e L i n e( i );
12 _______ ( c ) _______ ;
13 }
14 }
15 }
Blank Expression/Statement
___(a)___
___(b)___
___(c)___
Sample output
Please input N: 5
5
4
3
2
1
Lab #5 2 of 8
Please input N: 2
2
1
Example 1.2: The program used in Example 1.1 can be slightly modified to compute the sum-
mation of 1 . . . N in addition to printing them out.
1 using System ;
2 class While3 {
3 static void Main () {
4 int N , i , sum ;
5 Console . Write ( " Please input N : " );
6 N = int . Parse ( Console . ReadLine ());
7 i = 1;
8 sum = 0;
9 while ( i <= N ) {
10 Console . W r i t e L i n e( i );
11 sum = sum + i ;
12 i ++;
13 }
14 Console . W r i t e L i n e( " Sum from 1 to {0} = {1} " , N , sum );
15 }
16 }
Sample output
Please input N: 4
1
2
3
4
Sum from 1 to 4 = 10
Example 1.3: (Sentinel loop) Write a program to take input numbers from the user until a
negative number is entered. The program then terminates.
1 using System ;
2 class While4 {
3 static void Main () {
4 int N = 0;
5 while ( N >= 0) { // stop when N is negative
6 Console . Write ( " Please input N : " );
7 N = int . Parse ( Console . ReadLine ());
8 }
9 Console . W r i t e L i n e( " Bye Bye !!! " );
10 }
11 }
Lab #5 3 of 8
Sample output
Please input N: 3
Please input N: 2
Please input N: 3000
Please input N: 9999
Please input N: -50
Bye Bye!!!
1 using System ;
2 class While5 {
3 static void Main () {
4 int N = 0 , sum ;
5 ___ ( a ) ___ ;
6 while ( N >= 0) { // Exit while loop when N is negative
7 Console . Write ( " Please input N : " );
8 N = int . Parse ( Console . ReadLine ());
9 _ _ _ _ _ _ _ _ _ _ _ _( b ) _ _ _ _ _ _ _ _ _ _ _ _;
10 }
11 _ _ _ _ _ _ _ _ _ _ _ _ ( c ) _ _ _ _ _ _ _ _ _ _ _ _;
12 Console . W r i t e L i n e( " Bye Bye !!! " );
13 }
14 }
Blank Expression/Statement
___(a)___
___(b)___
___(c)___
Sample output
Please input N: 3
Please input N: 2
Please input N: 599
Please input N: 0
Please input N: -5
Sum = 604
Bye Bye!!!
Lab #5 4 of 8
Exercise 1.3: Modify the previous program so that the summation is calculated
on even numbers only. Again, the negative value used as a sentinel must not be
included in the calculation. Write your complete code in the box provided.
Sample output
Please input N: 4
Please input N: 2
Please input N: 599
Please input N: 1024
Please input N: -1
Sum even = 1030
Bye Bye!!!
2. do-while Loops
do {
statement1 ;
statement2 ;
:
statementN ;
} while (condition );
Lab #5 5 of 8
Example 2.1: The following program is a do-while version of the program in Example 1.3
1 using System ;
2 class DoWhile1 {
3 static void Main () {
4 int N = 0;
5 do {
6 Console . Write ( " Please input N : " );
7 N = int . Parse ( Console . ReadLine ());
8 } while ( N >= 0);
9 Console . W r i t e L i n e( " Bye Bye !!! " );
10 }
11 }
Exercise 2.1: Modify the program in Example 2.1 to compute the summation of all
the numbers entered except the last one (similar to Exercise 1.2). However, your
program must use a do-while loop. Write down your C# code in the box below.
Lab #5 6 of 8
3. Programming Tasks
Task 3.1: Write a C# program to read student scores from the user until the value
-1 is given. Then report the number of students and the average score with two
decimal places.
Sample output
Lab #5 7 of 8
Task 3.2: Modify the program you wrote in Task 3.1 so that it also reports the
minimum score and the maximum score (also with two decimal places).
Sample output
Enter a score, or -1 to quit: 76
Enter a score, or -1 to quit: 56.7
Enter a score, or -1 to quit: 87.4
Enter a score, or -1 to quit: 53.5
Enter a score, or -1 to quit: 90.8
Enter a score, or -1 to quit: 99
Enter a score, or -1 to quit: -1
Number of students is 6
Average score is 77.23
Min score is 53.50
Max score is 99.00
Lab #5 8 of 8