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!!
How to use debug and what is an array
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.
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.
Vocabulary: In C++ (and most other programming languages), the count starts by 0, and not 1!
What is a for 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++) { }
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.
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?
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:
- To be asked how many types of items she bought
- To be asked the name of the item
- The cost of the item
- How many items she bought
- To get a 10% discount if she's spending more than 100$
- To get a receipt with taxes (you can reuse the past code, but with a more efficient code!)
// 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