You are on page 1of 4

▪ Variables are used to store data temporarily in random access memory (RAM)

▪ Data Types

int – for whole numbers with no decimal point


double – for numbers with decimal points
char – for single characters
string – for more than one character
bool – for true/false
▪ Case –

studentName (Camel Casing)


StudentName (Pascal Casing)
STUDENTNAME (Uppercase)
• Use Camel Casing (e.g. studentName)
• Do not use the underscore ( _ ) character
• Do not use abbreviations or acronyms
• Be consistent
• char studentInitial = ‘N’;
• string studentSurname = “Mostert”;
• int studentAge = 19;
• double mark1 = 50.60;
• Assignment operator: the equal sign (=) in most languages
• Coding e.g.: Double answer
o Int x = 5, y = 2;
o Answer = x / (Double)y; //You are “casting” the y to a double.

Prompting the user - Console.Write(“Please enter your name: “ );

• Assigning Username as a variable:


String name
Console.Write(“Please enter your name: “ );
Name = console.readline();
• Obtaining a number:
o int age;
Console.write(“Please enter your age: “);
Age = int.parse(console.readline());
• Console.writeline skips a line.
• Accessing your variables:
String name;
Console.write(“Please enter Name “);
Name = Console.readline();
Console.writeline(“Hello {0} “, name);

More coding:
String name;
Double height;
Console.write(“Please enter name: “);
Name = Console.readline();
Console.write(“Please enter your height: “);
Height = double.parse(Console.readline());
Console.writeline(“Hello {0} you are {1} tall”, name, height);

• Display output as currency:


Double amountDue;
Console.writeline(“Please pay {0:C}”, amountDue);
• Rounding off:
Double number = 782.324;
Console.writeline(“Rounded to 2 decimal places {0:f2}”,number);
• Spacing:
Int Number = 2;
Console.writeline(“Number 1 {0;5}”,number);

• Spacing like #9:

String friend1;
Int age;
Concole.writeline(“Name/tAge”);
Console.writeline(“====/t====”);
Console.writeline(“{0}/t{1}”,friend1,age);

• Skipping lines:

Console.writeline(“Proudly/nSouth/nAfrican”);

• Adding inverted commas:

Console.writeline(“He said: \”HellO!\”loudly”);

• Concole.title = (“My first program”);


• Console.forground = consolecolor.Green;

• Console.Clear();

Trace Tables

▪ Trace tables - can be used to test an algorithm to make sure that the algorithm is free of
logical errors.

▪ Use a trace table to determine what the values of a, b, and c will be once the code below has
been executed.

int a = 10, b = 5, c = 3; a b c
a = b + c * 2;
b = b + (10 - c);
10 5 3
c = b + b;
11 12 24
▪ Write a program that accepts a number of hours (hoursWorked) and rate of pay (rateOfPay)
as input, and displays the employee wage.

static void Main(string[] args)


{
// declare variables
int hoursWorked;
double rateOfPay, wage;

// accept user input (prompt and get)


Console.Write("Enter hours worked: ");
hoursWorked = int.Parse(Console.ReadLine());
Console.Write("Enter rate of pay: ");
rateOfPay = double.Parse(Console.ReadLine());

// calculation
wage = hoursWorked * rateOfPay;

// display output
Console.WriteLine();
Console.WriteLine("Employee Wage: {0:c}", wage);

Console.ReadLine(); }  //Don’t Forget


• Write a program that prompts the user for a team name, the number of games won and
the number of games lost. The percentage of games won must be calculated. The team
name and percentage of games won must be displayed

static void Main(string[] args)


{
// declare variables
string teamName;
int gamesWon, gamesLost;
double percWon;

// accept user input (prompt and get)


Console.Write("Enter team name: ");
teamName = Console.ReadLine();
Console.Write("Enter games won: ");
gamesWon = int.Parse(Console.ReadLine());
Console.Write("Enter games lost: ");
gamesLost = int.Parse(Console.ReadLine());

// calculation
percWon = gamesWon / (double)(gamesWon + gamesLost) * 100;

// display output
Console.WriteLine("{0} won {1} percent of its games", teamName, percWon);

Console.ReadLine();
}

You might also like