Friday, March 27, 2026

Lesson 3: Loops - pt.1 (for, arrays)

In our earlier lesson, we asked the user for 3 values, corresponding to each side of a triangle. It was quite repetitive.

std::cout << "enter side a: "; std::cin >> a; std::cout << "enter side b: "; std::cin >> b; std::cout << "enter side c: "; std::cin >> c; // boringg

But programmers do not like to waste time and effort if not needed...What if I told you I could summarize this in 4 lines:

double side[3]; // an array for (int i = 0; i < 3; i++) { std::cout << "enter the side " << i + 1 << ": "; std::cin >> side[i]; }

I imagine it must be pretty confusing to read this, and that's very normal. I have yet to introduce you to loops and arrays!! 

mint yoongi and orange jimin

Let's break it down. But before, I'd like you to check by yourself how it works.

How to use debug and what is an array

When you open Clion, you might've noticed a very special symbol next to the "play" icon:


It's called the "debugger" or simply the debug.
The debug is a way to "slow down" the code at a certain part of the code you wish to inspect. 

You can copy the code I've written before (the "for" loop) and comment your past code. 

To comment an entire code (and not have to // manually), you can select the code you wish to comment and there will be an icon prompting you to comment. If you wish after to decomment the same code, you can follow the same procedure.



 When you hover your mouse next to the numbers that are aligned next to the lines of code, there's a red dot that appears under that you can click.


I'd like you to click on the line right before the loop I've written. 

When this is done, you can finally click on debug. It will show you this (there's more but this is what's important):


Threads and variables are what's happening inside of your code. Console is what you'd usually see, the "user" interface.

I've yet to explain what an array is, but if you run your code, you will come across this variable:


I've named my array side. You will see the number of "square" is equal to the ones that are "in" my array side. ( double side[3] )For now, forget the value inside of each square. Since I haven't initalized any of them, your computer will put "random" values (what we call garbage values).
Vocabulary: Initalizing a variable means putting it equal to a value. For exemple:
    int x; //this is not initalized, has garbage number     int y = 0; // initalized

 Once you see this, you can click on the forward button.


It basically reads the next line of code. You will the see the int variable I created, the "i". At some point, you won't be able to go forward because the code will now be awaiting an input:


You can switch back to the console tab. That's what you'd usually see. I will put as my input the number "67".


Once you do that, you'll see that the yellow arrow will go back to the top. That's completly normal, that's what loops do! Let's switch back to the other tab. 


Well how interesting, the first element of the array now has the value I input!
Vocabulary: In C++ (and most other programming languages), the count starts by 0, and not 1! 

What is a for loop

Now let's understand the loop:
   This is a specific type of loop, like I mentionned earlier. The for loop has 3 arguments:
for (int i = 0; i < n; i++) { }
The int i = 0
This is the temporary variable that counts how many loops we've done. We initalize it at 0. It usually is not an int, but a size_t, but for it to be more simple to you, let's keep it an int.

The i < n
This is tells the loop how many times they should loop it. It basically means "Do it for "i" is smaller than n. " So if my  n was = 5, how many values would my variable i would have?

    i = 0, i = 1, i = 2, i = 3, i = 4, i = .. 5? 
       1       2       3       4       5
    
Nope! 5 is not smaller than 5, it's equal to 5! Which makes sense, because I want my loop to be done 5 times, which is why it's important to understand that C++ array's start at 0.

The i ++ 

This explains what to do once the loop is done. Usually (in 95% of the cases), we wanna increment the i. It's the equivalent to doing i = i+1. Isn't it much quicker to just type i++ tho?

You can also do i-- ( i = i-1)

Okay!! You should be able to understand the code now!!

Can you tell me in simple words how this code works ? Walk me through all the loops it's gonna do.

double side[3]; // an array for (int i = 0; i < 3; i++) { std::cout << "enter the side " << i + 1 << ": "; std::cin >> side[i]; }




Yayy the worse part is done!!

Let's do something more fun! Let's work on your implementation skills!


Let's make a sort-of similar code as last time, with a receipt at the end. 

Arel wants to buy a lot kpop merch in bulk. She wants:

  1. To be asked how many types of items she bought
  2. To be asked the name of the item
  3. The cost of the item
  4. How many items she bought
  5. To get a 10% discount if she's spending more than 100$
  6. To get a receipt with taxes (you can reuse the past code, but with a more efficient code!)

*You might wanna use this to find the size (how many elements) you have in your array.

// Get the number of elements using std::size()
    size_t numberItemsTotal = std::size(arr);





Next week, we are going to learn about another type of loops, while and do while!



 

No comments:

Post a Comment