C# Programming Purvis Samsoodeen
C# Programming Purvis Samsoodeen
listing 1
// Read a character from the keyboard.
using System;
class KbIn {
static void Main() {
char ch;
listing 2
// Guess the letter game.
using System;
class Guess {
static void Main() {
char ch, answer = 'K';
listing 3
// Guess the letter game, 2nd version.
using System;
class Guess2 {
static void Main() {
char ch, answer = 'K';
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';
// A nested if.
if(ch < answer) Console.WriteLine("too low");
else Console.WriteLine("too high");
}
}
}
listing 5
// Demonstrate an if-else-if ladder.
using System;
class Ladder {
static void Main() {
int x;
listing 6
// Demonstrate the switch.
Page 2 of 14
C# Programming Purvis Samsoodeen
using System;
class SwitchDemo {
static void Main() {
int i;
}
}
listing 7
// Use a char to control the switch.
using System;
class SwitchDemo2 {
static void Main() {
char ch;
Page 3 of 14
C# Programming Purvis Samsoodeen
case 'E':
Console.WriteLine("ch is E");
break;
}
}
}
listing 8
// Categorize lowercase letters into vowels and consonants.
using System;
class VowelsAndConsonants {
static void Main() {
char ch;
listing 9
// A simple help system.
using System;
class Help {
static void Main() {
char choice;
Console.WriteLine("Help on:");
Console.WriteLine(" 1. if");
Console.WriteLine(" 2. switch");
Console.Write("Choose one: ");
choice = (char) Console.Read();
Console.WriteLine("\n");
switch(choice) {
case '1':
Console.WriteLine("The if:\n");
Page 4 of 14
C# Programming Purvis Samsoodeen
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;
default:
Console.Write("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;
listing 11
// Use commas in a for statement.
using System;
class Comma {
static void Main() {
int 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;
Console.WriteLine("Press S to stop.");
listing 13
// Parts of the for can be empty.
using System;
class Empty {
static void Main() {
int i;
listing 14
// Move more out of the for loop.
using System;
class Empty2 {
static void Main() {
int i;
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;
listing 16
// Declare loop control variable inside the for.
using System;
class ForVar {
static void Main() {
int sum = 0;
int fact = 1;
listing 17
// Demonstrate the while loop.
using System;
class WhileDemo {
static void Main() {
char 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;
listing 19
// Demonstrate the do-while loop.
using System;
class DWDemo {
static void Main() {
char ch;
do {
Console.Write("Press a key following by ENTER: ");
ch = (char) Console.Read(); // 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 {
Console.WriteLine("I'm thinking of a letter between A and Z.");
Console.Write("Can you guess it: ");
Page 8 of 14
C# Programming Purvis Samsoodeen
do {
ch = (char) Console.Read();
} while(ch == '\n' | ch == '\r');
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 {
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\n");
Console.Write("Choose one: ");
do {
choice = (char) Console.Read();
} while(choice == '\n' | choice == '\r');
} while( choice < '1' | choice > '5');
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;");
Page 9 of 14
C# Programming Purvis Samsoodeen
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;
}
}
}
listing 22
// Using break to exit a loop.
using System;
class BreakDemo {
static void Main() {
int num;
num = 100;
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) Console.Read();
if(ch == 'q') break;
}
Console.WriteLine("You pressed q!");
}
}
listing 24
// Using break with nested loops.
using System;
class Break3 {
static void Main() {
int t = 0;
while(t < 100) {
if(t == 10) break;
Console.Write(t + " ");
t++;
}
Console.WriteLine();
}
Console.WriteLine("Loops complete.");
}
}
listing 25
// Use continue.
using System;
class ContDemo {
static void Main() {
int i;
// Iterate if i is odd.
if((i%2) != 0) continue;
Console.WriteLine(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;
stop:
Console.WriteLine("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 {
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');
Console.WriteLine("\n");
Page 12 of 14
C# Programming Purvis Samsoodeen
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();
}
}
}
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() {
Page 14 of 14