Глава 5
Вверх
for(i=-5; i <= 5; i++)
{
Testing -5: negative
Анализ:
Вверх
for(i=-5; i <= 5; i++)
{
Testing -5: negative
Анализ:
Вверх
for(num = 2; num < 12; num++)
{
Smallest factor of 2 is 2.
Анализ:
Вверх
for(i=0; i<10; i++)
i is zero
Анализ:
Вверх
for(ch='A'; ch<= 'E'; ch++)
ch is A
Анализ:
Вверх
for(i=1; i < 5; i++)
i is 1, 2 or 3
Анализ:
Вверх
for(x = 100; x > -100; x -= 5)
100
Анализ:
Вверх
for(num = 2; num < 20; num++)
{
// see if num is evenly divisible
2 is prime.
Анализ:
Вверх
for(i=0, j=10; i < j; i++, j--)
i and j: 0 10
Анализ:
Вверх
num = 100;
smallest = largest = 1;
if((largest == 1) & ((num % j) == 0))
largest = j;
Largest factor: 50
Анализ:
Вверх
class forDemo
{
for(i=0, j=100; !done; i++, j--)
{
if(i*i >= j) done = true;
Console.WriteLine("i, j: " + i + " " + j);
i, j: 0 100
Анализ:
Вверх
using System;
class Empty
{
for(i = 0; i < 10; )
{
Pass #0
Анализ:
Вверх
using System;
class Empty2
{
i = 0; // move initialization out of loop
Pass #0
Анализ:
Вверх
using System;
class Empty3
{
// sum the numbers through 5
Console.WriteLine("Sum is " + sum);
Sum is 15
Анализ:
Вверх
using System;
class ForVar
{
// compute the factorial of the numbers through 5
// but, i is not known here.
Console.WriteLine("Sum is " + sum);
Sum is 15
Анализ:
Вверх
using System;
class WhileDemo
{
num = 435679;
Console.WriteLine("Number: " + num);
while(num > 0)
{
Console.WriteLine("Magnitude: " + mag);
Number: 435679
Анализ:
Вверх
using System;
class Power
{
for(int i=0; i < 10; i++)
{
while(e > 0)
{
Console.WriteLine("2 to the " + i +
" power is " + result);
2 to the 0 power is 1
Анализ:
Вверх
using System;
class DoWhileDemo
{
num = 198;
Console.WriteLine("Number: " + num);
Console.Write("Number in reverse order: ");
do
{
while(num > 0);
Number: 198
Анализ:
Вверх
using System;
class BreakDemo
{
// use break to exit this loop
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done
Анализ:
Вверх
i = -10;
-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 Done
Анализ:
Вверх
for(int i=2; i < num/2; i++)
{
Smallest factor is 2
Анализ:
Вверх
Outer loop count: 0
Анализ:
Вверх
0
Анализ:
Вверх
In case 1
In case 2
In case 3
In default
Анализ:
Вверх
Результат:
i, j, k: 0 0 0
listing 1
// Determine if a value is positive or negative.
using System;
class PosNeg
{
if(i < 0) Console.WriteLine("negative");
else Console.WriteLine("positive");
Результат:
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
{
if(i < 0) Console.WriteLine("negative");
else if(i == 0) Console.WriteLine("no sign");
else Console.WriteLine("positive");
Результат:
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
{
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 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
{
switch(i)
{
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 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
{
switch(ch)
{
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 B
ch is C
ch is D
ch is E
ch is D
listing 6
// Empty cases can fall through.
using System;
class EmptyCasesCanFall
{
switch(i)
{
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 4
listing 7
// A negatively running for loop.
using System;
class DecrFor
{
Console.WriteLine(x);
Результат:
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
{
int i;
int factor;
bool isprime;
factor = 0;
for(i=2; i <= num/2; i++)
{
isprime = false;
factor = i;
if(isprime)
Console.WriteLine(num + " is prime.");
else
Console.WriteLine("Largest factor of " + num +
" is " + factor);
Результат:
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
{
Console.WriteLine("i and j: " + i + " " + j);
Результат:
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
{
int smallest, largest;
int num;
for(i=2, j=num/2; (i <= num/2) & (j >= 2); i++, j--)
{
Console.WriteLine("Largest factor: " + largest);
Console.WriteLine("Smallest factor: " + smallest);
Результат:
Smallest factor: 2
listing 11
// Loop condition can be any bool expression.
using System;
bool done = false;
Результат:
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.
i++; // increment loop control var
Результат:
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.
for(; i < 10; )
{
i++; // increment loop control var
Результат:
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.
int sum = 0;
for(i = 1; i <= 5; sum += i++) ;
Результат:
listing 15
// Declare loop control variable inside the for.
int fact = 1;
for(int i = 1; i <= 5; i++)
{
fact *= i;
Console.WriteLine("Factorial is " + fact);
Результат:
Factorial is 120
listing 16
// Compute the order of magnitude of an integer
int mag;
mag = 0;
num = num / 10;
Результат:
Magnitude: 6
listing 17
// Compute integer powers of 2.
int result;
e = i;
e--;
Результат:
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.
int nextdigit;
Console.Write(nextdigit);
num = num / 10;
Console.WriteLine();
Результат:
Number in reverse order: 891
listing 19
// Using break to exit a loop.
for(int i=-10; i <= 10; i++)
{
Console.Write(i + " ");
Console.WriteLine("Done");
Результат:
listing 20
// Using break to exit a do-while loop.
using System;
class BreakDemo2
{
do
{
Console.Write(i + " ");
i++;
while(i <= 10);
Console.WriteLine("Done");
Результат:
listing 21
// Find the smallest factor of a value.
using System;
class FindSmallestFactor
{
int num = 1000;
break; // stop loop when factor is found
Console.WriteLine("Smallest factor is " + factor);
Результат:
listing 22
// Using break with nested loops.
using System;
class BreakNested
{
Console.Write(" Inner loop count: ");
int t = 0;
while(t < 100)
{
Console.Write(t + " ");
t++;
Console.WriteLine();
Console.WriteLine("Loops complete.");
Результат:
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
{
for(int i = 0; i <= 100; i++)
{
Console.WriteLine(i);
Результат:
2
4
6
8
10
12
14
...
92
94
96
98
100
listing 24
// Use goto with a switch.
using System;
class SwitchGoto
{
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 3
In default
In case 1
In case 3
In default
In default
listing 25
// Demonstrate the goto.
using System;
class Use_goto
{
if(k == 3) goto stop;
stop:
Console.WriteLine("Stopped! i, j, k: " + i + ", " + j + " " + k);
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