Csharp/C Sharp/Language Basics/for
Содержание
- 1 a for loop to display 1 to 5
- 2 A negatively running for loop
- 3 Compute the sum and product of the numbers from 1 to 10.
- 4 Declare loop control variable inside the for
- 5 Demonstrate the for loop
- 6 Determine if a number is prime. If it is not, then display its largest factor
- 7 Determine smallest single-digit factor
- 8 For loop for array
- 9 For Loop Tester
- 10 For with empty condition
- 11 For without increase
- 12 For without init value
- 13 If inside a for
- 14 Loop condition can be any bool expression
- 15 loop with letter char as the control variable
- 16 Move more out of the for loop
- 17 Parts of the for can be empty
- 18 Simplest for
- 19 The body of a loop can be empty
- 20 The finished C# statement Help system that processes multiple requests
- 21 Update two parameters in for loop
- 22 Use commas in a for statememt
- 23 Use commas in a for statememt to find the largest and smallest factor of a number
- 24 Use continue
- 25 Using break to exit a loop
a for loop to display 1 to 5
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example4_11.cs illustrates the use of
a for loop to display 1 to 5
*/
public class Example4_11
{
public static void Main()
{
for (int counter = 1; counter <= 5; counter++)
{
System.Console.WriteLine("counter = " + counter);
}
}
}
A negatively running for loop
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// A negatively running for loop.
using System;
public class DecrFor {
public static void Main() {
int x;
for(x = 100; x > -100; x -= 5)
Console.WriteLine(x);
}
}
Compute the sum and product of the numbers from 1 to 10.
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Compute the sum and product of the numbers from 1 to 10.
using System;
public class ProdSum {
static void Main() {
int prod;
int sum;
int i;
sum = 0;
prod = 1;
for(i=1; i <= 10; i++) {
sum = sum + i;
prod = prod * i;
}
Console.WriteLine("Sum is " + sum);
Console.WriteLine("Product is " + prod);
}
}
Declare loop control variable inside the for
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Declare loop control variable inside the for.
using System;
public 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);
}
}
Demonstrate the for loop
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Demonstrate the for loop.
using System;
public class ForDemo {
public static void Main() {
int count;
for(count = 0; count < 5; count = count+1)
Console.WriteLine("This is count: " + count);
Console.WriteLine("Done!");
}
}
Determine if a number is prime. If it is not, then display its largest factor
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Determine if a number is prime. If it is not,
then display its largest factor.
*/
using System;
public 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);
}
}
}
Determine smallest single-digit factor
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Determine smallest single-digit factor.
using System;
public 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.");
}
}
}
For loop for array
/*
* 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_3___Structs__Enums__Arrays_and_Classes
{
public class Arrays2Chapter_3___Structs__Enums__Arrays_and_Classes
{
static void Main(string[] args)
{
//multidimensional array
int[,] MyIntArray = new int[5, 5];
for (int x = 0, y = 0; x < 5; x++, y++)
{
MyIntArray[x, y] = 0;
}
}
}
}
For Loop Tester
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class ForTester
{
public static void Main()
{
for (int counter=0; counter<10; counter++)
{
Console.WriteLine(
"counter: {0} ", counter);
}
}
}
For with empty condition
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class ForEmptyTester
{
public static void Main()
{
int counterVariable = 0; // initialization
for ( ;; )
{
Console.WriteLine(
"counter: {0} ", counterVariable++); // increment
if (counterVariable > 10) // test
break;
}
}
}
For without increase
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class ForWithoutIncrTester
{
public static void Main()
{
for (int counter = 0; counter<10; ) // no increment
{
Console.WriteLine(
"counter: {0} ", counter);
// do more work here
counter++; // increment counter
}
}
}
For without init value
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class ForWithoutTester
{
public static void Main()
{
int counter = 0;
// some work here
counter = 3;
// more work here
for ( ; counter<10; counter++)
{
Console.WriteLine(
"counter: {0} ", counter);
}
}
}
If inside a for
/*
Learning C#
by Jesse Liberty
Publisher: O"Reilly
ISBN: 0596003765
*/
using System;
public class ForIfTester
{
public static void Main()
{
for (int counter=0; counter<10; counter++)
{
Console.WriteLine(
"counter: {0} ", counter);
// if condition is met, break out.
if (counter == 5) {
Console.WriteLine("Breaking out of the loop");
break;
}
}
Console.WriteLine("For loop ended");
}
}
Loop condition can be any bool expression
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Loop condition can be any bool expression.
using System;
public 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);
}
}
}
loop with letter char as the control variable
using System;
using System.Collections.Generic;
using System.Text;
class Program {
static void Main(string[] args) {
string greetingText = "www.nfex.ru";
for (int i = (int)"z"; i >= (int)"a"; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingText = greetingText.Replace(old1, new1);
}
for (int i = (int)"Z"; i >= (int)"A"; i--) {
char old1 = (char)i;
char new1 = (char)(i + 1);
greetingText = greetingText.Replace(old1, new1);
}
Console.WriteLine("Encoded:\n" + greetingText);
}
}
Move more out of the for loop
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Move more out of the for loop.
using System;
public 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
}
}
}
Parts of the for can be empty
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Parts of the for can be empty.
using System;
public class Empty {
public static void Main() {
int i;
for(i = 0; i < 10; ) {
Console.WriteLine("Pass #" + i);
i++; // increment loop control var
}
}
}
Simplest for
/*
* 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 Fors
{
static void Main(string[] args)
{
for (int a = 0; a < 10; a++)
{
Console.WriteLine(a);
}
}
}
}
The body of a loop can be empty
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// The body of a loop can be empty.
using System;
public 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);
}
}
The finished C# statement Help system that processes multiple requests
/*
C# A Beginner"s Guide
By Schildt
Publisher: Osborne McGraw-Hill
ISBN: 0072133295
*/
/*
Project 3-3
The finished C# statement Help system
that processes multiple requests.
*/
using System;
public class Help3 {
public static void Main() {
char choice;
for(;;) {
do {
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.WriteLine(" 3. for");
Console.WriteLine(" 4. while");
Console.WriteLine(" 5. do-while");
Console.WriteLine(" 6. break");
Console.WriteLine(" 7. continue");
Console.WriteLine(" 8. goto\n");
Console.Write("Choose one (q to quit): ");
do {
choice = (char) Console.Read();
} while(choice == "\n" | choice == "\r");
} while( choice < "1" | choice > "8" & choice != "q");
if(choice == "q") break;
Console.WriteLine("\n");
switch(choice) {
case "1":
Console.WriteLine("The if:\n");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case "2":
Console.WriteLine("The switch:\n");
Console.WriteLine("switch(expression) {");
Console.WriteLine(" case constant:");
Console.WriteLine(" statement sequence");
Console.WriteLine(" break;");
Console.WriteLine(" // ...");
Console.WriteLine("}");
break;
case "3":
Console.WriteLine("The for:\n");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case "4":
Console.WriteLine("The while:\n");
Console.WriteLine("while(condition) statement;");
break;
case "5":
Console.WriteLine("The do-while:\n");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case "6":
Console.WriteLine("The break:\n");
Console.WriteLine("break;");
break;
case "7":
Console.WriteLine("The continue:\n");
Console.WriteLine("continue;");
break;
case "8":
Console.WriteLine("The goto:\n");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}
}
}
Update two parameters in for loop
using System;
class CommaOp1
{
const int StartChar = 33;
const int EndChar = 125;
const int CharactersPerLine = 5;
static public void Main()
{
for (int i = StartChar, j = 1; i <= EndChar; i++, j++)
{
Console.Write("{0}={1} ", i, (char)i);
if (0 == (j % CharactersPerLine))
{
Console.WriteLine("");
}
}
}
}
Use commas in a for statememt
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use commas in a for statememt.
using System;
public 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);
}
}
Use commas in a for statememt to find the largest and smallest factor of a number
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
/*
Use commas in a for statememt to find
the largest and smallest factor of a number.
*/
using System;
public class Comma1 {
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);
}
}
Use continue
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Use continue.
using System;
public 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);
}
}
}
Using break to exit a loop
/*
C#: The Complete Reference
by Herbert Schildt
Publisher: Osborne/McGraw-Hill (March 8, 2002)
ISBN: 0072134852
*/
// Using break to exit a loop.
using System;
public 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");
}
}