Jason
Posted on March 3, 2021
Intro & Disclaimer
Lately, I have been relearning some code that I taught myself a while ago so I can start looking for more varied work. Over the course of my learning, I would like to share my progress and thoughts here. I may also be making huge gaps from here to there along the way so please keep that in mind.
Today, I wanted to share the relearning of 'for' loops in C++. I made this rather simpler example to play with user input nested 'for' loops.
The Goal
The goal of the code is to ask the user for 'x' and 'y' lengths that will be used to control the size of the loops.
User Input
NOTE: The following code will be put inside the main() function
To start, let's take a look at the first few lines.
First I create the variables that will hold the lengths of the 'for' loops.
I will call them 'x_Len' and 'y_Len', as well as, make them integers for ease of use down the line.
int x_Len;
int y_len;
After that, I prompt the user to enter a number for the length. I do this for 'x' and 'y'.
I use 'std::' because I don't want to talk about the alternitive for it just yet.
std::cout << "Enter a number for the x length: ";
std::cin >> x_len;
std::cout << "Enter a number for the y length: ";
std::cin >> y_len;
The 'for' Loops
Now that I have those I can finally create the first loop.
I'll use the 'x_Len' as the cap for the loop.
This is also what I will call the 'x' loop, solely because it is first.
Technically this would be the 'y' axis because it would be the 'height' of a screen.
At least that is what I understand.
for (int x = 0; x < x_Len; x++){
}
NOTE: If you feel I am wrong or you have questions about how these work feel free to leave a comment below!
Inside this 'for' loop I will add another loop but for 'y_Len' instead.
It looks something like this:
for (int x = 0; x < x_Len; x++){
for (int y = 0; y < y_Len; y++){
}
}
Again, technically this would be the 'x' axis because it would be the 'width' of a screen.
I am only inverting them here because that is my preference.
To fix this just swap the loops like this:
for (int y = 0; y < y_Len; y++){
for (int x = 0; x < x_Len; x++){
}
}
If you want even more clarity you can do this:
for (int height = 0; height < y_Len; height ++){
for (int width = 0; width < x_Len; width++){
}
}
Anyway, back to the topic at hand!
With this, we now have a nested 'for' loop inside another 'for' loop.
To get what I am looking for I added a few extra lines of code.
One is to print an '#' to the screen so that I can see visually what the loop does.
The second is to return the line after the second 'for' loop.
The loops should now look something like this:
for (int x = 0; x < x_len; x++){
for (int y = 0; y < y_len; y++){
std::cout << "#";
}
std::cout << "\n";
}
After all that we should be able to run the code and get a result like this:
Enter a number for the x length: 5
Enter a number for the y length: 5
#####
#####
#####
#####
#####
Thank you
Let me know what you think in the comments and maybe how I can improve in the future!
Thank you for reading!
The Code
Here is the link to my code (I'm sorry I didn't comment this well.):
#include <iostream>
int main() {
int x_len;
int y_len;
std::cout << "Enter a number for the x length: ";
std::cin >> x_len;
std::cout << "Enter a number for the y length: ";
std::cin >> y_len;
for (int x = 0; x < x_len; x++){
for (int y = 0; y < y_len; y++){
std::cout << "#";
}
std::cout << "\n";
}
}
Posted on March 3, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.