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!!

Notes

  

c++ stuff to not forget

data types (the boxes)

  • int - for whole numbers (like how many albums)
  • double - for numbers with decimals (like money: 49.99)
  • bool (true or false)
  • float (similar to double, but less precise. takes less effort to compute!)
  • std::string - for any text or words ("bts", "arel")

remember to name them!
std::string name = "arel"; not std::string = "arel";


operators (the math)

  • + (adds)
  • * (multiplies)
  • / (divides)
  • % (modulo, what's left)
  • = (this one is important, it means "save the answer into...")

so total = price * amount; calculates the math and saves it in total.


cin and cout (talking to the computer)

  • std::cout << : prints stuff onto the screen. arrows point out.
  • std::cin >> : waits for you to type something. arrows point in, to the variable.

possible coding errors

  1. ; : every single instruction needs a semicolon at the end
  2. std:: : goes before cout, cin, string, endl.
  3. std::endl : makes the text jump to a new line on the receipt.
  4. " " : use quotes for words you want to print, but no quotes for variables.
    • std::cout << "total: "; (prints the word total:)
    • std::cout << total; (prints the number saved inside the total variable)