C# Programming Purvis Samsoodeen
listing 1
// Read a character from the keyboard.
using System;
class KbIn {
static void Main() {
char ch;
[Link]("Press a key followed by ENTER: ");
// Read a key from the keyboard.
ch = (char) [Link]();
[Link]("Your key is: " + ch);
}
}
listing 2
// Guess the letter game.
using System;
class Guess {
static void Main() {
char ch, answer = 'K';
[Link]("I'm thinking of a letter between A and Z.");
[Link]("Can you guess it: ");
ch = (char) [Link](); // get the user's guess
if(ch == answer) [Link]("** Right **");
}
}
listing 3
// Guess the letter game, 2nd version.
using System;
class Guess2 {
static void Main() {
char ch, answer = 'K';
[Link]("I'm thinking of a letter between A and Z.");
[Link]("Can you guess it: ");
ch = (char) [Link](); // get the user's guess
if(ch == answer) [Link]("** Right **");
else [Link]("...Sorry, you're wrong.");
}
Page 1 of 14
C# Programming Purvis Samsoodeen
listing 4
// Guess the letter game, 3rd version.
using System;
class Guess3 {
static void Main() {
char ch, answer = 'K';
[Link]("I'm thinking of a letter between A and Z.");
[Link]("Can you guess it: ");
ch = (char) [Link](); // get the user's guess
if(ch == answer) [Link]("** Right **");
else {
[Link]("...Sorry, you're ");
// A nested if.
if(ch < answer) [Link]("too low");
else [Link]("too high");
}
}
}
listing 5
// Demonstrate an if-else-if ladder.
using System;
class Ladder {
static void Main() {
int x;
for(x=0; x<6; x++) {
if(x==1)
[Link]("x is one");
else if(x==2)
[Link]("x is two");
else if(x==3)
[Link]("x is three");
else if(x==4)
[Link]("x is four");
else
[Link]("x is not between 1 and 4");
}
}
}
listing 6
// Demonstrate the switch.
Page 2 of 14
C# Programming Purvis Samsoodeen
using System;
class SwitchDemo {
static void Main() {
int i;
for(i=0; i < 10; i++)
switch(i) {
case 0:
[Link]("i is zero");
break;
case 1:
[Link]("i is one");
break;
case 2:
[Link]("i is two");
break;
case 3:
[Link]("i is three");
break;
case 4:
[Link]("i is four");
break;
default:
[Link]("i is five or more");
break;
}
}
}
listing 7
// Use a char to control the switch.
using System;
class SwitchDemo2 {
static void Main() {
char ch;
for(ch='A'; ch <= 'E'; ch++)
switch(ch) {
case 'A':
[Link]("ch is A");
break;
case 'B':
[Link]("ch is B");
break;
case 'C':
[Link]("ch is C");
break;
case 'D':
[Link]("ch is D");
break;
Page 3 of 14
C# Programming Purvis Samsoodeen
case 'E':
[Link]("ch is E");
break;
}
}
}
listing 8
// Categorize lowercase letters into vowels and consonants.
using System;
class VowelsAndConsonants {
static void Main() {
char ch;
[Link]("Enter a letter: ");
ch = (char) [Link]();
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
[Link]("Letter is a vowel.");
break;
default:
[Link]("Letter is a consonant.");
break;
}
}
}
listing 9
// A simple help system.
using System;
class Help {
static void Main() {
char choice;
[Link]("Help on:");
[Link](" 1. if");
[Link](" 2. switch");
[Link]("Choose one: ");
choice = (char) [Link]();
[Link]("\n");
switch(choice) {
case '1':
[Link]("The if:\n");
Page 4 of 14
C# Programming Purvis Samsoodeen
[Link]("if(condition) statement;");
[Link]("else statement;");
break;
case '2':
[Link]("The switch:\n");
[Link]("switch(expression) {");
[Link](" case constant:");
[Link](" statement sequence");
[Link](" break;");
[Link](" // ...");
[Link]("}");
break;
default:
[Link]("Selection not found.");
break;
}
}
}
listing 10
// Show square roots of 1 to 99 and the rounding error.
using System;
class SqrRoot {
static void Main() {
double num, sroot, rerr;
for(num = 1.0; num < 100.0; num++) {
sroot = [Link](num);
[Link]("Square root of " + num +
" is " + sroot);
// Compute rounding error.
rerr = num - (sroot * sroot);
[Link]("Rounding error is " + rerr);
[Link]();
}
}
}
listing 11
// Use commas in a for statement.
using System;
class Comma {
static void Main() {
int i, j;
for(i=0, j=10; i < j; i++, j--)
[Link]("i and j: " + i + " " + j);
}
}
Page 5 of 14
C# Programming Purvis Samsoodeen
listing 12
// Loop until an S is typed.
using System;
class ForTest {
static void Main() {
int i;
[Link]("Press S to stop.");
for(i = 0; (char) [Link]() != 'S'; i++)
[Link]("Pass #" + i);
}
}
listing 13
// Parts of the for can be empty.
using System;
class Empty {
static void Main() {
int i;
for(i = 0; i < 10; ) {
[Link]("Pass #" + i);
i++; // increment loop control variable
}
}
}
listing 14
// Move more out of the for loop.
using System;
class Empty2 {
static void Main() {
int i;
i = 0; // move initialization out of loop
for(; i < 10; ) {
[Link]("Pass #" + i);
i++; // increment loop control var
}
}
}
listing 15
// The body of a loop can be empty.
using System;
Page 6 of 14
C# Programming Purvis Samsoodeen
class Empty3 {
static void Main() {
int i;
int sum = 0;
// Sum the numbers through 5.
for(i = 1; i <= 5; sum += i++) ;
[Link]("Sum is " + sum);
}
}
listing 16
// Declare loop control variable inside the for.
using System;
class ForVar {
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;
}
// Here, i is not known.
[Link]("Sum is " + sum);
[Link]("Factorial is " + fact);
}
}
listing 17
// Demonstrate the while loop.
using System;
class WhileDemo {
static void Main() {
char ch;
// Print the alphabet using a while loop.
ch = 'a';
while(ch <= 'z') {
[Link](ch);
ch++;
}
}
}
Page 7 of 14
C# Programming Purvis Samsoodeen
listing 18
// Compute integer powers of 2.
using System;
class Power {
static void Main() {
int e;
int result;
for(int i=0; i < 10; i++) {
result = 1;
e = i;
while(e > 0) {
result *= 2;
e--;
}
[Link]("2 to the " + i +
" power is " + result);
}
}
}
listing 19
// Demonstrate the do-while loop.
using System;
class DWDemo {
static void Main() {
char ch;
do {
[Link]("Press a key following by ENTER: ");
ch = (char) [Link](); // read a keypress
} while(ch != 'q');
}
}
listing 20
// Guess the letter game, 4th version.
using System;
class Guess4 {
static void Main() {
char ch, answer = 'K';
do {
[Link]("I'm thinking of a letter between A and Z.");
[Link]("Can you guess it: ");
// Read a letter, but skip cr/lf.
Page 8 of 14
C# Programming Purvis Samsoodeen
do {
ch = (char) [Link]();
} while(ch == '\n' | ch == '\r');
if(ch == answer) [Link]("** Right **");
else {
[Link]("...Sorry, you're ");
if(ch < answer) [Link]("too low");
else [Link]("too high");
[Link]("Try again!\n");
}
} while(answer != ch);
}
}
listing 21
/*
An improved Help system that uses a
a do-while to process a menu selection.
*/
using System;
class Help2 {
static void Main() {
char choice;
do {
[Link]("Help on:");
[Link](" 1. if");
[Link](" 2. switch");
[Link](" 3. for");
[Link](" 4. while");
[Link](" 5. do-while\n");
[Link]("Choose one: ");
do {
choice = (char) [Link]();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '5');
[Link]("\n");
switch(choice) {
case '1':
[Link]("The if:\n");
[Link]("if(condition) statement;");
[Link]("else statement;");
break;
case '2':
[Link]("The switch:\n");
[Link]("switch(expression) {");
[Link](" case constant:");
[Link](" statement sequence");
[Link](" break;");
Page 9 of 14
C# Programming Purvis Samsoodeen
[Link](" // ...");
[Link]("}");
break;
case '3':
[Link]("The for:\n");
[Link]("for(init; condition; iteration)");
[Link](" statement;");
break;
case '4':
[Link]("The while:\n");
[Link]("while(condition) statement;");
break;
case '5':
[Link]("The do-while:\n");
[Link]("do {");
[Link](" statement;");
[Link]("} while (condition);");
break;
}
}
}
listing 22
// Using break to exit a loop.
using System;
class BreakDemo {
static void Main() {
int num;
num = 100;
// Loop while i squared is less than num.
for(int i=0; i < num; i++) {
// Terminate loop if i*i >= 100.
if(i*i >= num) break;
[Link](i + " ");
}
[Link]("Loop complete.");
}
}
listing 23
// Read input until a q is received.
using System;
class Break2 {
static void Main() {
char ch;
Page 10 of 14
C# Programming Purvis Samsoodeen
for( ; ; ) {
ch = (char) [Link]();
if(ch == 'q') break;
}
[Link]("You pressed q!");
}
}
listing 24
// Using break with nested loops.
using System;
class Break3 {
static void Main() {
for(int i=0; i<3; i++) {
[Link]("Outer loop count: " + i);
[Link](" Inner loop count: ");
int t = 0;
while(t < 100) {
if(t == 10) break;
[Link](t + " ");
t++;
}
[Link]();
}
[Link]("Loops complete.");
}
}
listing 25
// Use continue.
using System;
class ContDemo {
static void Main() {
int i;
// Print even number between 0 and 100.
for(i = 0; i<=100; i++) {
// Iterate if i is odd.
if((i%2) != 0) continue;
[Link](i);
}
}
}
listing 26
// Demonstrate the goto.
Page 11 of 14
C# Programming Purvis Samsoodeen
using System;
class Use_goto {
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++) {
[Link]("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}
stop:
[Link]("Stopped! i, j, k: " + i + ", " + j + ", " + k);
}
}
listing 27
/*
The finished C# statement Help system
that processes multiple requests.
*/
using System;
class Help3 {
static void Main() {
char choice;
for(;;) {
do {
[Link]("Help on:");
[Link](" 1. if");
[Link](" 2. switch");
[Link](" 3. for");
[Link](" 4. while");
[Link](" 5. do-while");
[Link](" 6. break");
[Link](" 7. continue");
[Link](" 8. goto\n");
[Link]("Choose one (q to quit): ");
do {
choice = (char) [Link]();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '8' & choice != 'q');
if(choice == 'q') break;
[Link]("\n");
Page 12 of 14
C# Programming Purvis Samsoodeen
switch(choice) {
case '1':
[Link]("The if:\n");
[Link]("if(condition) statement;");
[Link]("else statement;");
break;
case '2':
[Link]("The switch:\n");
[Link]("switch(expression) {");
[Link](" case constant:");
[Link](" statement sequence");
[Link](" break;");
[Link](" // ...");
[Link]("}");
break;
case '3':
[Link]("The for:\n");
[Link]("for(init; condition; iteration)");
[Link](" statement;");
break;
case '4':
[Link]("The while:\n");
[Link]("while(condition) statement;");
break;
case '5':
[Link]("The do-while:\n");
[Link]("do {");
[Link](" statement;");
[Link]("} while (condition);");
break;
case '6':
[Link]("The break:\n");
[Link]("break;");
break;
case '7':
[Link]("The continue:\n");
[Link]("continue;");
break;
case '8':
[Link]("The goto:\n");
[Link]("goto label;");
break;
}
[Link]();
}
}
}
listing 28
// Use nested loops to find factors of numbers between 2 and 100.
using System;
Page 13 of 14
C# Programming Purvis Samsoodeen
class FindFac {
static void Main() {
for(int i=2; i <= 100; i++) {
[Link]("Factors of " + i + ": ");
for(int j = 2; j <= i/2; j++)
if((i%j) == 0) [Link](j + " ");
[Link]();
}
}
}
Page 14 of 14