Show Menu
Cheatography

Programming 1 Cheat Sheet by

Sept-Dec, Introduction to C#

Variable types

 
Page 35
A variable stores a value during the running of a program
Type
Descri­ption
string
contains alpha numeric charac­ters, persons name
int
a number value without decimal points, persons age
double
a number with decimal points, an amount of money
boolean
either true or false
c# is a strongly typed language. All variables must have a type.
We cannot "­mix­" types.

Simple Print to Console

 
Page 45
Examples
Console.WriteLine("Hello World");
Consol­e.W­rit­e("and Hello Moon");
Line
Descri­ption
1
insert a carriage return and leave cursor on next line
2
leave cursor on same line

Placeh­olders

 
Page 49
$"{v­arN­ame1} {varNa­me2­}"
Examples
Consol­e.W­rit­eLi­ne(­${"Name : {studentName}");
Console.WriteLine($"GPA : {stude­ntG­pa}­");
Line
Descri­ption
1
{ and } enclose the variable name to display
You can use multiple placeh­olders in one string.

Logical operators

 
Page 62
Operand
Descri­ption
A==B
checks two operands to see if equal. false
A!=B
check operands to see if not equal. true
A<B
is A less than B? false
A>=B
is A greater than or equal to B. true
A<=B
is A less than or equal to B? false
An operand is variable or value involved in operation. In examples above - A=10, B=5

Loops - While Loop

Pages 79-83,86-87
/*
* loop through a statement block 10 times
* if condition is not satisfied, statements will not be executed
*/
int counter=0;
while (counter <=10)
{
Consol­e.W­rit­eLi­ne(­$"Co­unter value is {count­er}­");
counter ++;
}

Methods - declar­ation

 
Page 93
[static] [publi­c|p­rivate] return­-type MethodName ([param-list]}
Type
Descri­ption
[static]
no need to create instance, call directly
[public | private]
can only be called from within this class
[void | int | double | string]
return type, void infers nothing returned
MethodName
use PascalCase for naming the method
([param-list])
specify paramater type, separate with commas

Tips n' Tricks for CA #1

Tip
Reason
Comment your code
allows you or someone else to more easily understand the code now or in the future
Watch your variables and constant naming convention
Use camelCase for ordinary variables, and UPPERCASE for constants - it's easy then to tell them apart
 

Variable Definition & Assignment

 
Pages 43-45
[type] <va­rNa­me> = <va­lue­>;
Examples
int studentAge=19;
string studentName="Walter";
double studen­tGp­a=7­8.68;
boolean studen­tRe­gis­tered;
Item
Descri­ption
type
common types int, string, double, boolean
<va­rNa­me>
the name of variable in which we store value
studen­tRe­gisterd is not initia­lised at declar­ation time above. it could be true or false.

Read Keyboard Input - Strings

 
Page 54
Example
studentName=Console.ReadLine();
Line
Descri­ption
1
read input from keyboard, assign to studen­tName
Consol­e.R­ead­Line() is a method without parame­ters.
It takes input from the keyboard as a string

Common Formatting Codes

   
Page 58-59
$"{x:c} {y:p­} {z:n3}"
Code
Format
Output
C or c
currency
€1,245.44
P or p
percent
4.00%
N or n
Number
103,42­3.346
Formatting improve the output for the user.

Above x=1245.443, y=0.04 and z=10342­3.3456.

If stements - combining expres­sions

 
Page 64-65
if ((cond­ition1 && condtion2)
{
// execute if condition1 AND condition2 true
}
else if ((cond­ition3 II condtion4)
{
// execute if condition3 OR condition4
}
else
// then execute this statement

Loops - Do While

Page 84
/*
* loop through a statement block 10 times
* statement block will always execute at least once
* even if counter was initially 11!!
*/
int counter=0;
do
{
Consol­e.W­rit­eLi­ne(­$"Co­unter value is {count­er}­");
counter ++; //
} while (counter <=10)

Methods - related termin­ology

 
Page 92
Item
Descri­ption
return type
a method can return a value - of type int, double, string. if nothing returned, then return­-type is void
sharing data between methods
parameters - values passed to a method call. also known as arguments.
class level variables - available to all methods, scope is global
calling a method
We must call the method to invoke it.
predefined methods
Includes Consol­e.W­rit­eLi­ne(­"­Hello) - has parameters
Consol­e.R­ead­line() - no parameters

Tips n' Tricks for CA #2

Tip
Reason
Use code indent­ation
When using condit­ional (If), Loops (While, Do While, For) and Methods - indent your code. Make it easier to read for everyone.
Follow the recipe
Make your input and output actually look like what is presented on the CA question.
 

Using naming conven­tions, comments

 
Pages 39,53,93,30
Type
Descri­ption
variables
camelCase, first letter is lowercase, other words first letter uppercase
constants
use uppercase, e.g. VATRATE
methods
Pascal­Case, first letter of each word is uppercase
comment our code
// what does this code do?
/* reminds collea­gues, our future selves */
Coding conven­tions are important within a team.

It is part of the common language of writing software code.

Read Keyboard input - Numbers

 
Page 55
Example
studentAge = int.Parse(Console.ReadLine());
studentGrade = double.Pa­rse­(Co­nso­le.R­ea­dLi­ne());
Line
Descri­ption
1
Integer value is returned and assigned
2
Double value may contain decimals
Extract a number from the keyboard input with .Parse()

Neater Printing in Tables

 
Page 57
$"<t­ext­>{<­exp­res­sio­n>,­<fi­eld­-wi­dth­>}<­tex­t>..."
Examples
$"Name :{stud­ent­Nam­e,2­0}"
$"{"N­ame­"­,-20} :{stud­ent­Nam­e}"
Line
Descri­ption
1
right justify, 20 leading spaces before student name
2
left justify, 20 spaces after label "­Nam­e"

If statements - Examples

Pages 66-73
if (stude­ntG­pa>=70)
{
Consol­e.W­rit­eLi­ne(­"­Hon­our­s");
}
else if (stude­ntG­pa<70 && studen­tGp­a>=50)
{
Consol­e.W­rit­eLi­ne(­"­Dis­tin­cti­on");
}
else Consol­e.W­rit­eLi­ne(­"­Fai­l");

Loops - For Loop

Pages 85,87
/*
* initial value of counter set in for statement
* counter is increm­ented then statement block complete
*/
int counter;
for (count­er=0; counter <=10; counter++)
{
Consol­e.W­rit­eLi­ne(­$"Co­unter value is {count­er}­");
}

Methods - full example

class Program
{
 static void Main(string[] args)
 {
  static string salutation="Hello"; 
  string name=GetName();
  PrintGreeting(name);
 } 

 static private string GetName() 
 {
   Console.WriteLine("Enter First Name : ");
   string firstName=nameConsole.ReadLine();
   return firstName;
 }
static private void PrintGreeting(string name)
 {
  Console.WriteLine($"{salutation} {name}!");
 }
}} // end class

Bits and pieces

Consol­e.O­utp­utE­nco­din­g=S­yst­em.T­ex­t.E­nco­din­g.UTF8; // display special symbols like currency
Carriage Return or "­\n" // A carriage return moves the cursor onto the next line in our console display.
Lab worksheet is a solution. Each question is a project
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          Numeric Formats Cheat Sheet
          C# & Unity MonoBehaviour Cheat Sheet