/*
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);
}
}
Language Basics
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."); } } } [/csharp]
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
");
Console.Write("Choose one (q to quit): ");
do {
choice = (char) Console.Read();
} while(choice == '
' | choice == '
');
} while( choice < '1' | choice > '8' & choice != 'q');
if(choice == 'q') break;
Console.WriteLine("
");
switch(choice) {
case '1':
Console.WriteLine("The if:
");
Console.WriteLine("if(condition) statement;");
Console.WriteLine("else statement;");
break;
case '2':
Console.WriteLine("The switch:
");
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:
");
Console.Write("for(init; condition; iteration)");
Console.WriteLine(" statement;");
break;
case '4':
Console.WriteLine("The while:
");
Console.WriteLine("while(condition) statement;");
break;
case '5':
Console.WriteLine("The do-while:
");
Console.WriteLine("do {");
Console.WriteLine(" statement;");
Console.WriteLine("} while (condition);");
break;
case '6':
Console.WriteLine("The break:
");
Console.WriteLine("break;");
break;
case '7':
Console.WriteLine("The continue:
");
Console.WriteLine("continue;");
break;
case '8':
Console.WriteLine("The goto:
");
Console.WriteLine("goto label;");
break;
}
Console.WriteLine();
}
}
}
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; } } } } [/csharp]
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);
}
}
}
}
[/csharp]
Example of using the fixed statement
using System;
public class Starter {
private static int[] numbers = { 5, 10, 15, 20, 25, 30 };
public unsafe static void Main() {
int count = 0;
Console.WriteLine(" Pointer Value
");
fixed (int* pI = numbers) {
foreach (int a in numbers) {
Console.WriteLine("{0} : {1}",
(int)(pI + count), *((int*)pI + count));
++count;
}
}
}
}
Expressions 1
/*
* C# Programmers Pocket Consultant
* Author: Gregory S. MacBeth
* Email: gmacbeth@comporium.net
* Create Date: June 27, 2003
* Last Modified Date:
* Version: 1
*/
using System;
namespace Client.Chapter_2___Operators_and_Excpressions
{
public class Expressions
{
static void Main(string[] args)
{
int MyInt = 12345;
int MyInt2 = 10000;
int Sum = 0;
long MyLong = MyInt;
short MyShort = (short)MyInt;
if (MyInt == MyInt2)
{
Sum = MyInt + MyInt2;
}
else
{
Sum = MyInt - MyInt2;
}
}
}
}