Содержание

Глава 5

Инструкции управления

  1. Determine if a value is positive or negative.
  2. Determine if a value is positive, negative, or zero.
  3. Determine smallest single-digit factor.
  4. Demonstrate the switch.
  5. Use a char to control the switch.
  6. Empty cases can fall through.
  7. A negatively running for loop.
  8. Determine if a number is prime. If it is not, then display its largest factor.
  9. Use commas in a for statememt.
  10. Use commas in a for statememt to find the largest and smallest factor of a number.
  11. Loop condition can be any bool expression.
  12. Parts of the for can be empty.
  13. Move more out of the for loop.
  14. The body of a loop can be empty.
  15. Declare loop control variable inside the for.
  16. Compute the order of magnitude of an integer
  17. Compute integer powers of 2.
  18. Display the digits of an integer in reverse order.
  19. Using break to exit a loop.
  20. Using break to exit a do-while loop.
  21. Find the smallest factor of a value.
  22. Using break with nested loops.
  23. Use continue.
  24. Use goto with a switch.
  25. Demonstrate the goto.

Вверх


listing 1
// Determine if a value is positive or negative.
using System;

class PosNeg {
public static void Main() {
int i;

for(i=-5; i <= 5; i++) {

Console.Write("Testing " + i + ": ");

if(i < 0) Console.WriteLine("negative");
else Console.WriteLine("positive");
}

}
}


Результат:

Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: positive
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive

Анализ:

Вверх


listing 2
// Determine if a value is positive, negative, or zero.
using System;

class PosNegZero {
public static void Main() {
int i;

for(i=-5; i <= 5; i++) {

Console.Write("Testing " + i + ": ");

if(i < 0) Console.WriteLine("negative");
else if(i == 0) Console.WriteLine("no sign");
else Console.WriteLine("positive");
}

}
}


Результат:

Testing -5: negative
Testing -4: negative
Testing -3: negative
Testing -2: negative
Testing -1: negative
Testing 0: no sign
Testing 1: positive
Testing 2: positive
Testing 3: positive
Testing 4: positive
Testing 5: positive

Анализ:

Вверх


listing 3
// Determine smallest single-digit factor.
using System;

class Ladder {
public static void Main() {
int num;

for(num = 2; num < 12; num++) {

if((num % 2) == 0)
Console.WriteLine("Smallest factor of " + num + " is 2.");
else if((num % 3) == 0)
Console.WriteLine("Smallest factor of " + num + " is 3.");
else if((num % 5) == 0)
Console.WriteLine("Smallest factor of " + num + " is 5.");
else if((num % 7) == 0)
Console.WriteLine("Smallest factor of " + num + " is 7.");
else
Console.WriteLine(num + " is not divisible by 2, 3, 5, or 7.");
}
}
}


Результат:

Smallest factor of 2 is 2.
Smallest factor of 3 is 3.
Smallest factor of 4 is 2.
Smallest factor of 5 is 5.
Smallest factor of 6 is 2.
Smallest factor of 7 is 7.
Smallest factor of 8 is 2.
Smallest factor of 9 is 3.
Smallest factor of 10 is 2.
11 is not divisible by 2, 3, 5, or 7.

Анализ:

Вверх


listing 4
// Demonstrate the switch.

using System;

class SwitchDemo {
public static void Main() {
int i;

for(i=0; i<10; i++)
switch(i) {

case 0: Console.WriteLine("i is zero");
break;
case 1:
Console.WriteLine("i is one");
break;
case 2:
Console.WriteLine("i is two");
break;
case 3:
Console.WriteLine("i is three");
break;
case 4:
Console.WriteLine("i is four");
break;
default:
Console.WriteLine("i is five or more");
break;
}

}
}


Результат:

i is zero
i is one
i is two
i is three
i is four
i is five or more
i is five or more
i is five or more
i is five or more
i is five or more

Анализ:

Вверх


listing 5
// Use a char to control the switch.

using System;

class SwitchDemo2 {
public static void Main() {
char ch;

for(ch='A'; ch<= 'E'; ch++)
switch(ch) {

case 'A':
Console.WriteLine("ch is A");
break;
case 'B':
Console.WriteLine("ch is B");
break;
case 'C':
Console.WriteLine("ch is C");
break;
case 'D':
Console.WriteLine("ch is D");
break;
case 'E':
Console.WriteLine("ch is E");
break;
}
}
}


Результат:

ch is A
ch is B
ch is C
ch is D
ch is E
ch is D

Анализ:

Вверх


listing 6
// Empty cases can fall through.

using System;

class EmptyCasesCanFall {
public static void Main() {
int i;

for(i=1; i < 5; i++)
switch(i) {

case 1:
case 2:
case 3: Console.WriteLine("i is 1, 2 or 3");
break;
case 4: Console.WriteLine("i is 4");
break;
}

}
}


Результат:

i is 1, 2 or 3
i is 1, 2 or 3
i is 1, 2 or 3
i is 4

Анализ:

Вверх


listing 7
// A negatively running for loop.

using System;

class DecrFor {
public static void Main() {
int x;

for(x = 100; x > -100; x -= 5)
Console.WriteLine(x);

}
}


Результат:

100
95
90
85
80
75
...
25
20
15
10
5
0
-5
-10
-15
...
-85
-90
-95

Анализ:

Вверх


listing 8
/*
Determine if a number is prime. If it is not,
then display its largest factor.
*/

using System;

class FindPrimes {
public static void Main() {
int num;
int i;
int factor;
bool isprime;

for(num = 2; num < 20; num++) {

isprime = true;
factor = 0;

// see if num is evenly divisible
for(i=2; i <= num/2; i++) {

if((num % i) == 0) {
// num is evenly divisible -- not prime
isprime = false;
factor = i;
}
}

if(isprime)
Console.WriteLine(num + " is prime.");
else
Console.WriteLine("Largest factor of " + num +
" is " + factor);
}
}
}


Результат:

2 is prime.
3 is prime.
Largest factor of 4 is 2
5 is prime.
Largest factor of 6 is 3
7 is prime.
Largest factor of 8 is 4
Largest factor of 9 is 3
Largest factor of 10 is 5
11 is prime.
Largest factor of 12 is 6
13 is prime.
Largest factor of 14 is 7
Largest factor of 15 is 5
Largest factor of 16 is 8
17 is prime.
Largest factor of 18 is 9
19 is prime.

Анализ:

Вверх


listing 9
// Use commas in a for statememt.

using System;

class Comma {
public static void Main() {
int i, j;

for(i=0, j=10; i < j; i++, j--)
Console.WriteLine("i and j: " + i + " " + j);

}
}


Результат:

i and j: 0 10
i and j: 1 9
i and j: 2 8
i and j: 3 7
i and j: 4 6

Анализ:

Вверх


listing 10
/*
Use commas in a for statememt to find
the largest and smallest factor of a number.
*/

using System;

class Comma {
public static void Main() {
int i, j;
int smallest, largest;
int num;

num = 100;

smallest = largest = 1;

for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--) {

if((smallest == 1) & ((num % i) == 0)) smallest = i;

if((largest == 1) & ((num % j) == 0)) largest = j;

}
Console.WriteLine("Largest factor: " + largest);
Console.WriteLine("Smallest factor: " + smallest);
}
}


Результат:

Largest factor: 50
Smallest factor: 2

Анализ:

Вверх


listing 11 // Loop condition can be any bool expression.
using System;

class forDemo {

public static void Main() {
int i, j;
bool done = false;

for(i=0, j=100; !done; i++, j--) {

if(i*i >= j) done = true;

Console.WriteLine("i, j: " + i + " " + j);

}

}
}


Результат:

i, j: 0 100
i, j: 1 99
i, j: 2 98
i, j: 3 97
i, j: 4 96
i, j: 5 95
i, j: 6 94
i, j: 7 93
i, j: 8 92
i, j: 9 91
i, j: 10 90

Анализ:

Вверх


listing 12
// Parts of the for can be empty.

using System;

class Empty {

public static void Main() {
int i;

for(i = 0; i < 10; ) {

Console.WriteLine("Pass #" + i);
i++; // increment loop control var
}
}
}


Результат:

Pass #0
Pass #1
Pass #2
Pass #3
Pass #4
Pass #5
Pass #6
Pass #7
Pass #8
Pass #9

Анализ:

Вверх


listing 13
// Move more out of the for loop.

using System;

class Empty2 {

public static void Main() {
int i;

i = 0; // move initialization out of loop
for(; i < 10; ) {

Console.WriteLine("Pass #" + i);
i++; // increment loop control var
}
}
}


Результат:

Pass #0
Pass #1
Pass #2
Pass #3
Pass #4
Pass #5
Pass #6
Pass #7
Pass #8
Pass #9

Анализ:

Вверх


listing 14
// The body of a loop can be empty.

using System;

class Empty3 {

public static void Main() {
int i;
int sum = 0;

// sum the numbers through 5
for(i = 1; i <= 5; sum += i++) ;

Console.WriteLine("Sum is " + sum);

}
}


Результат:

Sum is 15

Анализ:

Вверх


listing 15
// Declare loop control variable inside the for.

using System;

class ForVar {

public static void Main() {
int sum = 0;
int fact = 1;

// compute the factorial of the numbers through 5
for(int i = 1; i <= 5; i++) {

sum += i; // i is known throughout the loop
fact *= i;
}

// but, i is not known here.

Console.WriteLine("Sum is " + sum);
Console.WriteLine("Factorial is " + fact);

}
}


Результат:

Sum is 15
Factorial is 120

Анализ:

Вверх


listing 16
// Compute the order of magnitude of an integer

using System;

class WhileDemo {

public static void Main() {
int num;
int mag;

num = 435679;
mag = 0;

Console.WriteLine("Number: " + num);

while(num > 0) {

mag++;
num = num / 10;
};

Console.WriteLine("Magnitude: " + mag);

}
}


Результат:

Number: 435679
Magnitude: 6

Анализ:

Вверх


listing 17
// Compute integer powers of 2.

using System;

class Power {

public static void Main() {
int e;
int result;

for(int i=0; i < 10; i++) {

result = 1;
e = i;

while(e > 0) {

result *= 2;
e--;
}

Console.WriteLine("2 to the " + i + " power is " + result);

}
}
}


Результат:

2 to the 0 power is 1
2 to the 1 power is 2
2 to the 2 power is 4
2 to the 3 power is 8
2 to the 4 power is 16
2 to the 5 power is 32
2 to the 6 power is 64
2 to the 7 power is 128
2 to the 8 power is 256
2 to the 9 power is 512

Анализ:

Вверх


listing 18
// Display the digits of an integer in reverse order.

using System;

class DoWhileDemo {

public static void Main() {
int num;
int nextdigit;

num = 198;

Console.WriteLine("Number: " + num);

Console.Write("Number in reverse order: ");

do {

nextdigit = num % 10;
Console.Write(nextdigit);
num = num / 10;
}

while(num > 0);
Console.WriteLine();

}
}


Результат:

Number: 198
Number in reverse order: 891

Анализ:

Вверх


listing 19
// Using break to exit a loop.

using System;

class BreakDemo {

public static void Main() {

// use break to exit this loop
for(int i=-10; i <= 10; i++) {

if(i > 0) break; // terminate loop when i is positive
Console.Write(i + " ");
}
Console.WriteLine("Done");
}
}


Результат:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done

Анализ:

Вверх


listing 20
// Using break to exit a do-while loop.

using System;

class BreakDemo2 {
public static void Main() {
int i;

i = -10;
do {

if(i > 0) break;
Console.Write(i + " ");
i++;
}
while(i <= 10);

Console.WriteLine("Done");
}
}


Результат:

-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done

Анализ:

Вверх


listing 21
// Find the smallest factor of a value.

using System;

class FindSmallestFactor {
public static void Main() {
int factor = 1;
int num = 1000;

for(int i=2; i < num/2; i++) {

if((num%i) == 0) {
factor = i;
break; // stop loop when factor is found
}
}
Console.WriteLine("Smallest factor is " + factor);
}
}


Результат:

Smallest factor is 2

Анализ:

Вверх


listing 22
// Using break with nested loops.

using System;

class BreakNested {
public static void Main() {
for(int i=0; i<3; i++) {
Console.WriteLine("Outer loop count: " + i);
Console.Write(" Inner loop count: ");

int t = 0;
while(t < 100) {
if(t == 10) break; // terminate loop if t is 10
Console.Write(t + " ");
t++;
}
Console.WriteLine();
}
Console.WriteLine("Loops complete.");
}
}


Результат:

Outer loop count: 0
Inner loop count: 0 1 2 3 4 5 6 7 8 9
Outer loop count: 1
Inner loop count: 0 1 2 3 4 5 6 7 8 9
Outer loop count: 2
Inner loop count: 0 1 2 3 4 5 6 7 8 9
Loops complete.

Анализ:

Вверх


listing 23
// Use continue.

using System;

class ContDemo {
public static void Main() {
// print even numbers between 0 and 100
for(int i = 0; i <= 100; i++) {
if((i%2) != 0) continue; // iterate
Console.WriteLine(i);
}
}
}


Результат:

0
2
4
6
8
10
12
14
...
92
94
96
98
100

Анализ:

Вверх


listing 24
// Use goto with a switch.

using System;

class SwitchGoto {
public static void Main() {
for(int i=1; i < 5; i++) {
switch(i) {
case 1: Console.WriteLine("In case 1");
goto case 3;
case 2:
Console.WriteLine("In case 2");
goto case 1;
case 3:
Console.WriteLine("In case 3");
goto default;
default:
Console.WriteLine("In default");
break;
}

Console.WriteLine();
}

// goto case 1; // Error! Can't jump into a switch.
}
}


Результат:

In case 1
In case 3
In default

In case 2
In case 1
In case 3
In default

In case 3
In default

In default

Анализ:

Вверх


listing 25
// Demonstrate the goto.

using System;

class Use_goto {
public static void Main() {
int i=0, j=0, k=0; for(i=0; i < 10; i++) {
for(j=0; j < 10; j++ ) {
for(k=0; k < 10; k++) {
Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}

stop:
Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);

}
}

Результат:

i, j, k: 0 0 0
i, j, k: 0 0 1
i, j, k: 0 0 2
i, j, k: 0 0 3
Stopped! i, j, k: 0, 0 3



Содержание

Hosted by uCoz