Csharp/C Sharp/Language Basics/While — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 11:39, 26 мая 2010
Содержание
- 1 a do...while loop
- 2 Compute integer powers of 2
- 3 Compute the order of magnitude of an integer
- 4 Continue in while
- 5 Display the digits of an integer in reverse order
- 6 Do While Tester
- 7 Simplest do while
- 8 Simplest while
- 9 Using break to exit a do-while loop
- 10 while loop to calculate and display the Fibonacci numbers less than 50
- 11 While loop to display 1 to 5
- 12 While Signal
- 13 While Tester
- 14 While true test
a do...while loop
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_10.cs illustrates the use of
a do...while loop
*/
public class Example4_10
{
public static void Main()
{
int counter = 1;
do
{
System.Console.WriteLine("counter = " + counter);
counter--;
}
while (counter > 1);
}
}
Compute integer powers of 2
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Compute integer powers of 2.
using System;
public 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);
}
}
}
Compute the order of magnitude of an integer
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Compute the order of magnitude of an integer
using System;
public 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);
}
}
Continue in while
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_4___Program_Control
{
public class MyMainClass11
{
static void Main(string[] args)
{
WhileContinue();
WhileBreak();
WhileGoto();
}
static void WhileContinue()
{
int a = 0;
while(a < 10)
{
a++;
if (a == 5)
{
a++;
continue;
}
}
}
static void WhileBreak()
{
int a = 0;
while (a < 10)
{
a++;
if (a == 5)
break;
}
a++;
}
static void WhileGoto()
{
int a = 0;
while (a < 10)
{
if (a == 5)
goto cleanup;
}
cleanup :
Console.WriteLine(a);
}
}
}
Display the digits of an integer in reverse order
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Display the digits of an integer in reverse order.
using System;
public 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();
}
}
Do While Tester
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class DoWhileTester
{
public static void Main()
{
int counterVariable = 11;
// display the message and then test that the value is
// less than 10
do
{
Console.WriteLine("counterVariable: {0}",counterVariable);
counterVariable++;
} while (counterVariable < 10);
}
}
Simplest do while
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_4___Program_Control
{
public class DoWhile
{
static void Main(string[] args)
{
int a = 0;
do
{
a++;
Console.WriteLine(a);
} while (a < 10);
}
}
}
Simplest while
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
*/
using System;
namespace Client.Chapter_4___Program_Control
{
public class Whiles
{
static void Main(string[] args)
{
int a = 0;
while(a > 10)
{
a++;
Console.WriteLine(a);
}
}
}
}
Using break to exit a do-while loop
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using break to exit a do-while loop.
using System;
public 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");
}
}
while loop to calculate and display the Fibonacci numbers less than 50
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_9.cs illustrates the use of
a while loop to calculate and display
the Fibonacci numbers less than 50
*/
public class Example4_9
{
public static void Main()
{
// initialize the first two numbers in the sequence
int oldNumber = 1;
int currentNumber = 1;
int nextNumber;
System.Console.Write(currentNumber + " ");
while (currentNumber < 50)
{
System.Console.Write(currentNumber + " ");
// calculate the next number by adding the
// current number to the old number
nextNumber = currentNumber + oldNumber;
oldNumber = currentNumber;
currentNumber = nextNumber;
}
}
}
While loop to display 1 to 5
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_8.cs illustrates the use of
a while loop to display 1 to 5
*/
public class Example4_8
{
public static void Main()
{
int counter = 1;
while (counter <= 5)
{
System.Console.WriteLine("counter = " + counter);
counter++;
}
}
}
While Signal
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class WhileSignalTester
{
public static int Main()
{
string signal = "0"; // initialize to neutral
while (signal != "X") // X indicates stop
{
Console.Write("Enter a signal. X = stop. A = Abort: ");
signal = Console.ReadLine();
// do some work here, no matter what signal you
// receive
Console.WriteLine("Received: {0}", signal);
if (signal == "A")
{
// faulty - abort signal processing
// Log the problem and abort.
Console.WriteLine("Fault! Abort\n");
break;
}
if (signal == "0")
{
// normal traffic condition
// log and continue on
Console.WriteLine("All is well.\n");
continue;
}
// Problem. Take action and then log the problem
// and then continue on
Console.WriteLine("{0} -- raise alarm!\n",
signal);
}
return 0;
}
}
While Tester
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class WhileTester
{
public static void Main()
{
int counterVariable = 0;
// while the counter variable is less than 10
// print out its value
while (counterVariable < 10)
{
Console.WriteLine("counterVariable: {0}",counterVariable);
counterVariable++;
}
}
}
While true test
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class WhileTrueTester
{
public static void Main()
{
int counterVariable = 0; // initialization
while (true)
{
Console.WriteLine(
"counter: {0} ", counterVariable++); // increment
if (counterVariable > 10) // test
break;
}
}
}