0% found this document useful (0 votes)
81 views14 pages

C# Programming Purvis Samsoodeen

The document discusses various C# programming concepts including if/else statements, switch statements, loops (for, while, do-while), and methods to read input from the keyboard. It provides 20 code listings as examples to demonstrate these concepts. The listings show simple programs for reading keyboard input, implementing guessing games, looping through numbers, computing square roots and factorials, and other examples to illustrate control flow and basic programming techniques in C#.

Uploaded by

brianweldt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
81 views14 pages

C# Programming Purvis Samsoodeen

The document discusses various C# programming concepts including if/else statements, switch statements, loops (for, while, do-while), and methods to read input from the keyboard. It provides 20 code listings as examples to demonstrate these concepts. The listings show simple programs for reading keyboard input, implementing guessing games, looping through numbers, computing square roots and factorials, and other examples to illustrate control flow and basic programming techniques in C#.

Uploaded by

brianweldt
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

C# Programming Purvis Samsoodeen

listing 1
// Read a character from the keyboard.

using System;

class KbIn {
static void Main() {
char ch;

Console.Write("Press a key followed by ENTER: ");

// Read a key from the keyboard.


ch = (char) Console.Read();

Console.WriteLine("Your key is: " + ch);


}
}

listing 2
// Guess the letter game.

using System;

class Guess {
static void Main() {
char ch, answer = 'K';

Console.WriteLine("I'm thinking of a letter between A and Z.");


Console.Write("Can you guess it: ");

ch = (char) Console.Read(); // get the user's guess

if(ch == answer) Console.WriteLine("** Right **");


}
}

listing 3
// Guess the letter game, 2nd version.

using System;

class Guess2 {
static void Main() {
char ch, answer = 'K';

Console.WriteLine("I'm thinking of a letter between A and Z.");


Console.Write("Can you guess it: ");

ch = (char) Console.Read(); // get the user's guess

if(ch == answer) Console.WriteLine("** Right **");


else Console.WriteLine("...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';

Console.WriteLine("I'm thinking of a letter between A and Z.");


Console.Write("Can you guess it: ");

ch = (char) Console.Read(); // get the user's guess

if(ch == answer) Console.WriteLine("** Right **");


else {
Console.Write("...Sorry, you're ");

// 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;

for(x=0; x<6; x++) {


if(x==1)
Console.WriteLine("x is one");
else if(x==2)
Console.WriteLine("x is two");
else if(x==3)
Console.WriteLine("x is three");
else if(x==4)
Console.WriteLine("x is four");
else
Console.WriteLine("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:
Console.WriteLine("i is zero");
break;
case 1:
Console.WriteLine("i is one");
break;
case 2:
Console.WriteLine("i is two");
break;
case 3:
Console.WriteLine("i is three");
break;
case 4:
Console.WriteLine("i is four");
break;
default:
Console.WriteLine("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':
Console.WriteLine("ch is A");
break;
case 'B':
Console.WriteLine("ch is B");
break;
case 'C':
Console.WriteLine("ch is C");
break;
case 'D':
Console.WriteLine("ch is D");
break;

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;

Console.Write("Enter a letter: ");


ch = (char) Console.Read();
switch(ch) {
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'y':
Console.WriteLine("Letter is a vowel.");
break;
default:
Console.WriteLine("Letter is a consonant.");
break;
}
}
}

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;

for(num = 1.0; num < 100.0; num++) {


sroot = Math.Sqrt(num);
Console.WriteLine("Square root of " + num +
" is " + sroot);

// Compute rounding error.


rerr = num - (sroot * sroot);
Console.WriteLine("Rounding error is " + rerr);
Console.WriteLine();
}
}
}

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--)


Console.WriteLine("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;

Console.WriteLine("Press S to stop.");

for(i = 0; (char) Console.Read() != 'S'; i++)


Console.WriteLine("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; ) {


Console.WriteLine("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; ) {
Console.WriteLine("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++) ;

Console.WriteLine("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.

Console.WriteLine("Sum is " + sum);


Console.WriteLine("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') {
Console.Write(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--;
}

Console.WriteLine("2 to the " + i +


" power is " + 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: ");

// Read a letter, but skip cr/lf.

Page 8 of 14
C# Programming Purvis Samsoodeen

do {
ch = (char) Console.Read();
} while(ch == '\n' | ch == '\r');

if(ch == answer) Console.WriteLine("** Right **");


else {
Console.Write("...Sorry, you're ");
if(ch < answer) Console.WriteLine("too low");
else Console.WriteLine("too high");
Console.WriteLine("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 {
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;

// 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;

Console.Write(i + " ");


}
Console.WriteLine("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) 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() {

for(int i=0; i<3; i++) {


Console.WriteLine("Outer loop count: " + i);
Console.Write(" Inner loop count: ");

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;

// Print even number between 0 and 100.


for(i = 0; i<=100; 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;

for(i=0; i < 10; i++) {


for(j=0; j < 10; j++ ) {
for(k=0; k < 10; k++) {
Console.WriteLine("i, j, k: " + i + " " + j + " " + k);
if(k == 3) goto stop;
}
}
}

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');

if(choice == 'q') break;

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() {

for(int i=2; i <= 100; i++) {


Console.Write("Factors of " + i + ": ");
for(int j = 2; j <= i/2; j++)
if((i%j) == 0) Console.Write(j + " ");
Console.WriteLine();
}
}
}

Page 14 of 14

You might also like