[phpBB Debug] PHP Warning: in file [ROOT]/ext/alfredoramos/seometadata/event/listener.php on line 114: Undefined array key 8475810 [phpBB Debug] PHP Warning: in file [ROOT]/ext/alfredoramos/seometadata/event/listener.php on line 114: Trying to access array offset on value of type null MS Coding Camp(working title) - Mafiascum.net
If you’ve completed the lesson, feel free to copy and paste your code in a post here or to screenshot the results of your code running. If you feel like experimenting and do anything neat, please share that too. And obviously if you have any questions just shout em out and we’ll do our best to help.
If there’s anything in the format/presentation/or really any aspect of these lessons that you think could be better/improved that would also be greatly appreciated as we’re still pretty new to this kind of stuff! [or at least I am zzz]
We're starting up a discord if people want to hang out while working on things or if you just would prefer to ask questions in real time there we figured it might be easier for some! Ask myself, chamber or errant for linkage if you're interested!
Happy coding.
Last edited by Cheetory6 on Mon Nov 14, 2016 12:52 pm, edited 1 time in total.
We're mostly aiming to just get the result with the first lesson and to go into detail on exactly what the code is doing as we go more in-depth with coding in general in future lessons.
Today we're going to be covering control structures, which are an important set of tools used to direct the flow of your program. To understand what control structures do, we first have to cover exactly what the flow of a program is in general. Let’s revisit our code from the first lesson:
From your experience in the first two tutorials, you know that when you run this code, you’ll receive the Hello World message in the console. From later examples, you may have inferred that from the following code, you will receive the following messages in the following order in the following program:
void Start () {
Debug.Log("Hello world!");
Debug.Log("How's it going?");
}
OUTPUT:
1 - "Hello world!"
2 - "How's it going?"
Given this, you probably understand that a program flows from top to bottom and in this case, the program will flow from the first open bracket to the second open bracket of the Start codeblock:
{ == START
step1
step2
step3
..
..
stepN
} == FINISH
The program will go line by line until it reaches that terminal bracket and then the program will terminate.
Spoiler: Codeblocks
Before we go any further with this example, now is probably a good time to elaborate a little bit on exactly what a codeblock is. Essentially, this is any grouping of code trapped between two matching curly brackets “{ }”. The outermost brackets will delineate their own codeblock and the innermost brackets will delineate their own codeblock.
IE:
{
==============
BLUE
{
==============
BLUE
RED
{
==============
BLUE
RED
GREEN
==============
}
BLUE
RED
==============
}
BLUE
==============
}
Anywhere marked as blue is within the blue brackets’ codeblock, anything marked as red is within the red brackets’ codeblock and so on.
Spoiler: If Statements
Most programs you're going to typically want to do something a little more complicated than just go from the start of the program to the finish every single time. Sometimes you're going to want to direct the flow of your program with these aforementioned control structures to achieve results that would otherwise require an absurd number of lines of code or would simply be impossible.
Recall back to our variables lesson. We covered a few different types of variables including booleans. At the time you may have wondered why a boolean would be considered important. It can be true or false. You might be wondering, “what’s so special about that?”
Well, booleans play a big role in the first control structure, the If statement [and the others too but we’ll get to that later]. An If statement is by definition “a programming conditional statement that, if proved true, performs a function or displays information.” In layman’s terms, an if statement is a coding tool that will run a section of code
ONLY
if it is given a boolean input that has a value of True.
if(true)
{
Debug.Log("I am the code in the first If statement. You get to hear me.");
}
if(false)
{
Debug.Log("I am the code in the second If statement. You do not get to hear me");
}
The brackets beside the If term contain the boolean value that is used to determine whether the code within the curly braces will be run or not. The first If statement has a value of true, and the contents associated with it will be run. The second If statement has a value of true, so the contents associated with it will not be run.
Hence, our output will be just:
“I am the code in the first If statement. You get to hear me.”
By extension of the last lesson, you can also provide a boolean variable as input to an if statement and you can even use full boolean expressions.
Spoiler: Garbage opinions that probably don’t matter much to new programmers
Something to consider is that doing the latter can lead to some bulky and ugly code in. A variable containing a boolean argument with a smart name will usually be easier to understand on revisit than a big block of boolean expressions sitting unlabeled within an If statement.
I’ll spare you the horror of some really nasty examples [partially because I don’t have time to go hunting for some of my worst offenses q.q], but trust me that you’ll thank me later if you really get into more complicated programming. Really try to practice putting complicated expressions into well named variables, especially if you’re ever planning on returning to code at a later date!
Spoiler: ElseIf/Else Statements
ANYWAY.
Continuing on with If statements, we’ll next be considering the Else statement. Essentially, an Else statement is an If statement in its own way. It’s a statement that if given a value of true, will run the code contained within its braces, except in this case an Else statement will
ALWAYS
follow an If statement of some kind and will
ONLY
run if the If statement that preceded it was skipped over because it was given a value of True as input.
If that’s potentially unclear, just consider the following example:
if(false)
{
Debug.Log("I am the code in the If statement. You do not get to hear me this time.");
}
else
{
Debug.Log("I am the code in the Else statement. I am finally heard! >:D");
}
The else statement in this example has no condition, which makes sense given that it’s condition is kind of inherent. When the If statement before it is run, the Else statement is skipped. When the If statement before it is skipped, the Else statement is run. In this case, the latter is run and the second message is played:
"I am the code in the Else statement. I am finally heard! >:D"
Next up we’re covering the combination of the previous two concepts, the Else If statement. This may read a little confusing to you, so let’s start off with an example:
bool condition1 = false;
bool condition2 = true;
if(condition1)
{
Debug.Log("I am the code in the If statement. You do not get to hear me this time.");
}
else if(condition2)
{
Debug.Log("I am the code in the Else If statement. My turn, nerd.");
}
else
{
Debug.Log("I am the code in the Else statement. I am also not heard this time.");
}
An Else If statement will be run if all If/Else If statements before it are skipped over due to having a boolean input of false
AND
its own boolean input gives a value of true. In this case, condition1 is false, so the first If statement is skipped over and condition2 is true, so the Else If statement is run.
Spoiler: While Loops
For the other two control structures, we will be using a similar concept of using boolean expressions to guide the movement of our program, but we’ll adding the concept of Loops/Looping on top. A loop is essentially a section of code that gets repeated in the flow of program’s operation. We will first consider the While loop:
int i = 0;
while(i < 4){
Debug.Log(i);
i++;
}
Debug.Log(“Escaped!!”);
The basic idea of the while loop, is that while the condition within the brackets is true the code within the curly brackets will repeat.
In this case, the while loop will repeat the code within its curly brackets until the variable i is no longer less than 4.
OUTPUT:
1 - "1"
2 - "2"
3 - "3"
4 - "4"
You have to be really careful when using while loops! There is the potential to create a set of conditions where a while loop will never have its condition become true and then the code within will repeat indefinitely and lock up your program. If you remove the contents of the code within the while loop above as follows, you will never get the “Escaped!!” message.
int i = 0;
while(i < 4){
}
Debug.Log(“Escaped!!”);
OUTPUT:
Spoiler: Arithmetic Tangent
Before we move onto the last control structure, we need to cover a bit of a tangent. Programming has a lot of focus on condensing large amounts of code into something more compact and easier to digest. This extends as far as simple arithmetic. Programmers just don’t want to write out something like adding or subtracting a 1 to a number. Your first option for decreasing the size of your arithmetic-based expressions is to get rid of the variable and the operator on the right side of the equation by doing the following:
x+=1;
This statement essentially implies that the value of x is increasing by one.
You can do this with any value or any of the four basic arithmetic operators. Programmers weren’t satisfied with that, so they created increment/decrement operators [++ and -- respectively]. These essentially let you implicitly increase or decrease the value of a number without writing out an entire expression.
Consider the following example:
x++;
is equivalent to
x = x + 1;
which is equivalent to
x+=1;
The moral of the story is that coders want to be lazy [or neat, depending on how you want to look at it!].
Spoiler: For Loops
The final control structure we’ll be covering today is the For loop, which in many ways is very similar to the while loop, but with a little more in terms of working parts. Instead of just a generic boolean input, the For loop has three sections that have to be set before the code within can start repeating.
The most generic for loop declaration is as follows:
for(int i = 0; i < 4; i++)
Let’s break it down.
i) Initializing the counter variable - int i = 0
Set i to an initial value that it will take on at the beginning of the loop’s run.
ii) Setting the boolean condition - i < 4
Set a boolean variable that must no longer be considered true before the loop can stop running.
iii) Incrementing the counter variable - i++
A step that will occur every time the loop reaches the bottom bracket and goes to start the next iteration through the loop’s code.
So what does this mean in effect? Well, basically the For loop in this example will run the exact same as the While loop did in the last example. It will run the code inside its braces four times. One difference to consider is that the variable i will only exist within the For loop. If you try to access it before or afterwards, you’ll get an error. [If you experiment you may be able to find a similar effect in the If and While control structures!] We’ll go into more detail on why this is the case in future lessons, just something to note for now!
So, now we have a bunch of control structures. Big woop.
What can we do with them?
Well, surprisingly a lot, really. These are tools that you’ll find yourself using again and again and again as a programmer and it’s best if you really try to get a sense of how they work as best you can. Experimenting really helps here.
Spoiler: Nested Control Structures
One final thing to consider with control structures is that you can embed them within each other. [think back to the codeblocks example]
IE:
The While Loop is within the first If’s codeblock and the If/Else are within the While Loop’s codeblock and the outer If’s codeblock.
This is called nesting or nested control structures.
If you’ve reached this point then you’ve completed this lesson! Congrats!
Spoiler: Challenges
1) Last week you were asked to print to the screen the largest possible value for an int. To do this you likely had to just calculate it outside of the program and then manually print that number. This week do the same thing, but do the calculation in unity using control structures.
2) Simplify the following hypothetical If/If Else statements into just two If statements [by hand, not in Unity]:
if (condition) {
if (conditionA) {
codeBlockW;
}
else if (conditionB) {
codeBlockX;
}
}
else {
if (conditionA) {
codeBlockX;
}
else if (conditionB) {
codeBlockW;
}
}
3) A Fibonacci sequence is a series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8, etc. Print all of the Fibonacci numbers under 100 to the console.
4) Find the biggest prime number under 100.
5) Add together all numbers between 1 - 1000 that are not divisible by 3 or 5, except for numbers that are divisible by both 3 and 5.
include : 1,2,4,7,8,11,13,14,15
exclude 3,5,6,9,10,12
Last edited by Cheetory6 on Thu Dec 01, 2016 3:24 pm, edited 1 time in total.