PCP − Lecture 03

Fall 2020

Clément Aubert

August 25, 2020

Last Time - First Program

Datatypes Nomenclature

(In italics, the one we will mainly be using.)

Integers are “whole” numbers ( = {…,  − 1, 0, 1, 2, 3, …}), floating point numbers are real numbers (), strings are “text messages”, …

Please refer to the “Datatypes in C#” cheatsheet for more information about datatypes.

String and Int Variables

Literals are fixed values (“Hi Mom”, 40, 1.2404, …) in the source code.

using System;
class MyFirstVariables{
    static void Main(){
    // Declaration
       int myAge;
       string myName;
    // Assignemnt
       myAge = 40;
       myName = "Clément";
    // Displaying
       Console.WriteLine($"I am {myAge} old and my name is {myName}.");
    }
}

A variable has a name (which must be an identifier), a type, a size, and a value.

Variable Name Variable Type Variable Size Variable Value
myAge int 32 bit 40
myName string Variable1 "Clément"

Variable Initialization

You can declare and assign a variable in one statement using what is called an “initialization statement”.

string myMessage = "Hey Mom";
int myValue = 12;

There is now one additional rule when it comes to chosing a valid identifier for your variable name: you can not take an identifier that was already used. That is, you can have only one variable named myMessage: if you want to re-assign a variable, you can not use an initialization again (that would re-declare the variable), you need to use an assignment statement again.

Remarks


  1. It’s actually 20 + (n/2) * 4 bytes, for n the number of characters in the string, so 7 in that case.↩︎