Sunday, March 22, 2026

Lesson 2 (if statements)

  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. 


#include <iostream> #include <string> 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:

//withdraw is withing 0 and 100

hi arel! your current balance is: 100 how much money do you want to take out? 90 success! your new balance is: 10 thank you for using our bank! Process finished with exit code 0

//withdraw is lower than 0
hi arel! your current balance is: 100 how much money do you want to take out? -100 error: you cannot retrieve negative money! thank you for using our bank! Process finished with exit code 0

//withdraw is higher than 100
hi arel! your current balance is: 100 how much money do you want to take out? 999 error: you don't have enough money for that! thank you for using our bank! Process finished with exit code 0

You got this!!

Triangles

Let's do another exemple, this time with triangles. I know... math.
Here's a picture that's gonna make you feel better before we do math:

Alright, let's continue!

I'd like you to read a section of this AlloProf (😓) section:

Equilateral Triangles

The name of this triangle comes from the combination of the prefix equi, which means equal, and lateral, which means side.

Definition

An equilateral triangle is a triangle that has three congruent (isometric) sides.

image
Important!

An equilateral triangle will always be an equiangular triangle since all of its angles will necessarily have the same measurement.

Isosceles Triangles

The name of this triangle comes from the combination of the Greek prefix isos, which means equal, and the word skelos, which means legs.

Definition

An isosceles triangle is a triangle that has both two congruent (isometric) sides and two congruent angles.

image

Note that if sides are indicated using the same number of bars, then these sides have the same measurement.

Important!

An isosceles triangle will always have two angles with the same measure.

Scalene Triangles

Definition

The measurements of all the angles and sides of a scalene triangle are distinct. That is, they all measure differently.

image
(end of AlloProf insert)

Alright, a bit boring, but we can deduce some if statements!

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: 

  1. Are you an music artist (very global) (goal is yes)
  2. Are you kpop (ok, more precise but not too much) (goal is yes)
  3. Are you a girl group or a boy group (goal is boy)
  4. When did you debut (goal is 2013)
  5. How many members (goal is 7)
  6. Have you released an album in 2026 (goal is yes)
  7. Is one of the songs named Jump? (goal is yes)
It wouldn't make sense if we placed them in a different order like: (let's say the input is Blackpink)
  1. Are you kpop (yes)
  2. Is one of your songs named Jump (yes)
  3. Are you a music artist (yes)
  4. Are you a girl group or a boy group (no)
End of code here, while it could've been dealt with faster... Anyways you get it.

Enough of kpop, let's go back to triangles.

Can you tell me what would be the statements and in what order?












The order would look like this: -> Equilaterale -> Isocele -> Scalene

I'll give you the variables to use:

double a, b, c; // those are the lengths of each side of the triangle

Now your turn! Include cin and cout, and an answer depending on the lenghts.




Odd and Even numbers 

Alright, let's do this last one, shouldn't take too long.

Create a program that detects if an entire number is odd or even.

Reminder: % (modulo)
If you aren't sure how it works, feel free to check the notes tab or look on Google! It's your best friend in coding.











Congrats! You are now done with our 2nd lesson!! 





1 comment: