Basic Programming Fundamentals - Detailed Notes
Tags: beginners, tutorial, programming
Published: 2022-09-08 18:25:16

Programming is cool and you might want to learn some coding language like Python or JavaScript, but before all of that stuff, you need to understand the fundamentals that will apply to any code, whether it's game dev or machine learning, or if it's C# or JavaScript. So, here are the basic fundamentals of computer programming!

**Code samples will be in Java, but the concepts apply to all languages**


**Data Types**: The type that anything being stored in memory has, in code.

 - Integer: Any non-decimal number, commonly known as `int`

<br/><br/>
 - String: Any combination of multiple characters. Always in double quotes or single quotes

<br/><br/>
 - Char: Any one character

<br/><br/>
 - Float: Any decimal number, ex. 6.4, 2.0, -4.0

<br/><br/>
 - Boolean: Any true or false value

**Variables**: Memory locations in your code with a name that store some value.

<br/><br/>

<br/><br/>
 - Always have some sort of data type, depending on the value. Ex. a variable stores `"death"`, this would mean that the variable has the data type of string.

<br/><br/>
 - Static/constant variables are variables that do not change throughout the whole program and remain constant. Dynamic variables are variables that are well, dynamic. They change based on different conditions in the program.

<br/><br/>
 - The convention for variable names is to have it start with a letter, and separate words in the name with an underscore. For constant vars, the name should be all capital letters, for dynamic, all lowercase. **Naming convention may be different based on the language.**

<br/><br/>
 - Declaring a variables means creating the variable and making a memory location for it. Assigning means to assign a value to a variable.

<br/><br/>
 - To declare a variable, you could do `int num_of_sandwiches` (int is the data type and num_of_sandwiches is the name) then assign it later in the program with `num_of_sandwiches = 3`. If you want to assign the variable at the same time you declare it, you could do `int num_of_sandwiches = 3` in one line.

**If/else/else if**: Every real-world program uses this. It is basically a block where if a condition is true, it runs some code. Here's an example to help you understand.

Let's say there is a variable called `is_life_good` and the value of that variable is `true`. Our task is to show a message saying "Life is good! :D" only if `life_is_good` has the value of true. Here's the code to do this

```java
boolean life_is_good = true;
if (life_is_good) {
    System.out.println("Life is good! :D");
}
```
output
```
Life is good! :D
```

In the first line, the variable is initialized and assigned. Then, there is an if statement, the condition just being the variable `life_is_good`, which has a value of true. As said previously, the if statement only runs if the condition is true, and since it is, it runs `System.out.println("Life is good! :D");`.

Now let's say you want to print "Life's not good :(" if `life_is_good` is false, and if it's not true or false, you want to say "You don't have a life lmao", how do you do that? You can use `elif` and `else`. `elif` is if you want 3+ conditions. If the `if` condition is false, then it will go to the `elif` condition(you can have unlimited `elif`s), if none of the `elif` conditions are true, it will run the `else` code.

```java
boolean life_is_good = false;
if (life_is_good) {
    System.out.println("Life is good! :D");
} elif (life_is_good == false) {
    System.out.println("Life's not good :(");
} else {
    System.out.println("You don't have a life lmao");
}
```
^^^
This would be the new code with the `elif` and `else`
output
```
Life's not good :(
```

P.S: System.out.println is how you print something to the console in Java

**For loops** - Code gets looped "x" times. Here's an example

You want to print "Hello!" five times, how do you do that? Well, you could use a for loop.

```java
for (int i = 0; i < 5; i++) {
    System.out.println("Hello!");
}
```
output
```
Hello!
Hello!
Hello!
Hello!
Hello!
```

The first line starts the for loop. The `i` variable in there is known as the counter, the first part, `int i = 0`, makes the `i` variable. The second part, `i < 5`, tells the for loop to run if `i` is less than 5. Then, the third part, `i++`, tells it to increase `i` by one each iteration.

**While loops** - Loops that run the code while some condition is true, here's an example.

You want to say "Lucky, less responsibility for you" while the age is less than 18, to do this, you could use a while loop!

```java
int age = 0;
while (age < 18) {
    System.out.println("Lucky, less responsibility for you");
    age++;
}
```
output
```
Lucky, less responsibility for you
```
(repeats 18 times)

The first line is the variable that the condition depends on, `age`. The second line starts the while loop, with the condition being `age < 18`. Then in the loop, we print "Lucky, less responsibility for you", then we do `age++`. This is to increase the age each iteration, if you don't do that, the loop will run forever because the age will always be less than 18, and that breaks your program.

**Functions** - Code blocks that you can reuse and run over and over.

Your task is to run code that will print "okay" if the variable `is_okay` is `true`, and "not okay" if it's `false` 5 times, without using a for loop. You could have 5 if statements, or you could use less code, by putting one if statement in a function, then run that function 5 times, like this.

```java
boolean is_okay = true;
static void coolMethod() {
    if (is_okay) {
        System.out.println("okay");
    } else {
        System.out.println("not okay");
    }
}
coolMethod();
coolMethod();
coolMethod();
coolMethod();
coolMethod();
```
output
```
okay
okay
okay
okay
okay
```

So this is cool, but something that is more useful is functions with parameters, let's see another example.

You want to make a function that with print whatever you give the function when it's ran. How would you go about doing this?

```java
static void amazingMethod(String stuff) {
    System.out.println(stuff);
}

amazingMethod("life");
```
output
```
life
```

The parameter here is `stuff`, and we can use that parameter anywhere in that function.

The `println` thing we are using to print stuff to the console is also a function. Other examples of functions are `Math.pow()`, `Math.random()`, etc.

<br></br>

And yeah, these are some basic programming fundamentals! If you want me to add some more stuff, please comment, and also comment if you have any questions or anything else! I hoped these notes helped you.