+ 2
Can anyone make me understand looping in C++ The 11th grade requirements only
4 Respuestas
+ 6
Loops are simply a way to run a piece of code over and over, and are an integral part of programming in any programming language (or, at least, all the ones I know of).
Say I want to print a sentence 10 times:
cout << "Hello, world!" << endl;
I could just copy and paste this 10 times in my code, but that's generally considered very ineffiecient and sloppy (why type it more than once if we don't have to?). Plus, what if we decided we needed to print it 1000 times, or we don't know how many times we'll need to print it before the program starts? That's where loops come in.
There are three major kinds of loops:
- for loops
- while loops
- do-while loops
A for loop has three parts (don't be intimidated if you don't know these words yet. I swear it's simple): the initilizer, the condition, and the incrementor.
Here is what it would look like printing our code setence 10 times with this loop:
for (int x = 1; x <= 10; x++)
{
cout << "Hello, world!" << endl;
}
To break it down: "int x = 1" is the initilizer. It's basically what number the loop starts from. "x <= 10" is our condition. As long as this condition is true, the loop will run again. Finally, "x++" is our incrementor. This says that after each loop, increment x by 1. Eventually, as the loop continues to run, x will no longer be <= 10. At that point, the loop will stop.
Here's another example:
for (int init = 0; init < 100; init++)
{
cout << init << endl;
}
The loop above would print every integer from 0 to 99 (not 100, because we used < instead of <=). For loops are useful for running a block of code a specific number of times.
We could also use variables that change in our for loops. Maybe even take in some user input? We could let the user decide how many times to print the sentence.
int input;
cin >> input;
for(int x = 1; x <= input; x++)
{
cout << x << endl;
}
You really should test these out yourselves, but hopefully you can see how useful loops can get with more and more code.
+ 2
While loops work a little different. Using the same example as before (printing out a sentence), here is what a while loop would look like:
int x = 0;
while(x < 10)
{
cout << "Hello, world!" << endl;
x++;
}
A while loop is a little different, because it really only test to see if a condition is true or not. The initilizer and incrementor aren't really included in the loop. So if I had forgotten to put the "x++" at the bottom of my code, we'd have an infinite loop!
Now, you can actually do the same with for loops (that is, not include the incrementor and cause an infinite loop). At first glance, it can be hard to see why a while loop may be more useful than a for loop in some situations. In the above case, either one kind of works just as well.
So instead, let's imagine we were making a game. And let's say while the player was walking, we wanted to play a specific animation, or even several. A while loop may make more sense here:
while(player.isWalking)
{
player.playAnimation("walking");
player.ActivateParticleEffect("glow1");
}
I just made up nonsense code in the code block there, but hopefully you get the idea. While the player is walking, the code will loop through. When the player stops, the code stops looping.
Finally, we have the do-while loop. Very similar to a while loop, but with one nifty exception. It looks like this:
int x = 0;
do
{
cout << "Hello, world!" << endl;
x++
} while (x < 10);
Just like our while loop, except the "while (x<10)" condition is at the bottom. What's neat about this loop is that it will always run at least one time! So let's say we changed the condition at the bottom of the last do-while loop to just "false" (while (false);). It would still print out the sentence at least one time before it ever checks to see if the condition is true. Esentially, it runs the code block first, and then starts checking the condition before it continues to loop.
And there you have it. Hopefully I helped you some :D
+ 2
Thanks to all
I just understood it
+ 1
so help full thanks