Ok first real planned class!
This is meant to give some structure to my lesson, but if you wish you can follow the steps by yourself.
If statements
What are ifs statements? You've most likely heard of them, as they are one of the most important fundamentals in coding!
Until now, everything we've done was pretty linear, 1st line, 2nd line, etc. But what if I had a specific need that fit only some criteria?
Let's say that I wanted to code a simple program that allowed users to retrieve some money from their bank account.
int main() { // variables (remember to give them names!) std::string name = "arel"; double balance = 100.00; double withdraw; std::cout << "hi " << name << "! your current balance is: " << balance << std::endl; std::cout << "how much money do you want to take out? " << std::endl; // grabbing the user input std::cin >> withdraw;
Let's take a look at this code. Understanding a code is also something important to master at some point.
First thing, you might've noticed this -> //. Those are comments, they allow people to just type a comment in the code. This can be very helpful in a team project, or even to make your own code simpler.
After grabbing the user's answer, a very simple code could follow it:
balance = balance - withdraw; std::cout << "success! your new balance is: " << balance << std::endl;
..But what happens if the user says they want to withdraw 999$? Or even -1000$, both of these options shouldn't even be possible!
These are two new tools that will help us solve this problem!
if (condition) { //code } else { // code }
The if statement is structure this way. If (the condition happens), then this part of the code in the { } brackets will happen. Else, the other { } brackets will run. Pretty simple.
This was the first tool, the second will help us properly declare a condition!
- == (different than =) : This basically means return true if equal
- !=: Similar to ==, but return true if NOT equal
- && : It's the logical "And"
- || : The logical "or"
Now with all those tools, you should be able to reproduce something like this:
You got this!!
Triangles
Equilateral Triangles

Isosceles Triangles

Scalene Triangles

Important fact!! If statements have to be in the right order. The "most important one" (that takes in account as many possible "trues" needs to be first. Kind of like organizing music:
Let's "find" bts:
- Are you an music artist (very global) (goal is yes)
- Are you kpop (ok, more precise but not too much) (goal is yes)
- Are you a girl group or a boy group (goal is boy)
- When did you debut (goal is 2013)
- How many members (goal is 7)
- Have you released an album in 2026 (goal is yes)
- Is one of the songs named Jump? (goal is yes)
- Are you kpop (yes)
- Is one of your songs named Jump (yes)
- Are you a music artist (yes)
- Are you a girl group or a boy group (no)


hey wow this is the best lesson ever
ReplyDelete