Basic Coding

A mini document that I had made to quickly explain the basics of programming.

translated quickly and dirty with Google translate

What’s the point ?

Programming automates a task that a computer/machine can perform. To do this it is necessary to translate an algorithm into a language that the computer can understand (binary).

The variables

Variable types

Here is a short example of a program in pseudo-code (fictional language):

print("What's your name?"); // displays What is your name?
string name = input(); // read what the user writes
print(name); // displays its name


print("How old are you?");
int age = input();
print(age);

In this pseudo-code I used 2 different types of variable:

There are mainly 5 of them:

Type Name Example
char Character ‘it’
bool Boolean true
int Integer 35
float Floating number 3.14
string Character string “Bonjour Monde !”

Declare a variable

Declaring a variable allows you to say OK! I’m going to need this variable, I’m not putting a value in it at the moment but I’m going to need it.

To declare a variable in pseudo-code:

type name;

example:

string city;

Assign variable

Assigning a variable means, assigning it a value.

Assignment is done using the = sign.

example:

string city;
city = "Brussels";

To declare AND to affect a variable at once it is necessary

type name = value;

For example

string city = "Brussels";

Functions

A function makes it possible to associate a piece of code with a name, which can make it possible to reuse it without being redundant.

example:

int requestAge() {
     print("How old are you?");
     int age = input();
     return age;
}

It is possible to give arguments to a function.

Here is an example:

int readNumber(phrase) {
     print(sentence)
     int number = input()
     return number;
}

int main() {
     int age = readNumber("What is your name?");
     int zip_code = readNumber("What is your zip code?");
}

The scope of variables

The scope of a variable or in other words lifetime defines the place in the code where you can use the variable.

example

int main() {
     int x = 5;
     {
         int y = 4;
     }
     x=2;
}

bad example:

int main() {
     int x = 5;
     {
         int y = 4;
     }
     x=2;
     y=2; // won't work
}