Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SIZE=7][B]Introduction to Loops[/B][/SIZE]
- A [B]loop[/B] is a programming tool that [I]repeats[/I] some code or a set of instructions until a specified condition is reached. As a programmer, you’ll find that you rely on loops all the time! You’ll hear the generic term “iterate” when referring to loops; [I]iterate[/I] simply means “to repeat”.
- When we see that a process has to repeat multiple times in a row, we write a loop. Loops allow us to create efficient code that automates processes to make scalable, manageable programs.
- In this lesson, we will learn about two types of loops: [ICODE]while[/ICODE] loops and [ICODE]for[/ICODE] loops!
- [SIZE=7][B]While Loop Demo[/B][/SIZE]
- So first up… the [ICODE]while[/ICODE] loop!
- Before we dive deep into the syntax of the [ICODE]while[/ICODE] loop, let’s do a demo.
- Inside [B]enter_pin.cpp[/B], we have a program that asks and checks for a password. It uses a [ICODE]while[/ICODE] loop to ask the user for the password over and over again.
- [B]Note:[/B] You don’t need to understand the code right now.
- [URL unfurl="true"]https://pastebin.com/06XEAbKA[/URL]
- [SIZE=7][B]Guess Number[/B][/SIZE]
- So now that we got a demo of loops, let’s write one!
- The [ICODE]while[/ICODE] loop looks very similar to an [ICODE]if[/ICODE] statement. And just like an [ICODE]if[/ICODE] statement, it executes the code inside of it if the condition is [ICODE]true[/ICODE].
- However, the difference is that the [ICODE]while[/ICODE] loop will continue to execute the code inside of it, [I]over and over again[/I], as long as the condition is [ICODE]true[/ICODE].
- Here is what a [ICODE]while[/ICODE] loop looks like:
- [CODE]while (condition) {
- statements
- }[/CODE]
- In other words, instead of executing [I]if[/I] something is true, it executes [I]while[/I] that thing is true.
- [CODE=cpp]while (guess != 8) {
- std::cout << "Wrong guess, try again: ";
- std::cin >> guess;
- }[/CODE]
- In this example, while [ICODE]guess[/ICODE] is not equal to 8, the program will keep on asking the user to input a new number. It will exit the [ICODE]while[/ICODE] loop once the user types in [ICODE]8[/ICODE] and then the program will continue.
- [SIZE=7][B]For Loop Demo[/B][/SIZE]
- [IMG]https://i.imgur.com/Qk8Ljcz.png[/IMG]
- ([FoxTrot](https://www.foxtrot.com))
- Iterating over a sequence of numbers is so common that C++, like most other programming languages, has a special syntax for it.
- When we know exactly how many times we want to iterate (or when we are counting), we can use a [ICODE]for[/ICODE] loop instead of a [ICODE]while[/ICODE] loop:
- [CODE=cpp]for (int i = 0; i < 20; i++)
- {
- std::cout << "I will not throw paper airplanes in class.\n";
- }[/CODE]
- Let’s take a closer look at the first line:
- [CODE=cpp]for (int i = 0; i < 20; i++)[/CODE]
- There are three separate parts to this separated by [ICODE];[/ICODE]:
- [LIST]
- [*]The initialization of a [I]counter[/I]: [ICODE]int i = 0[/ICODE]
- [*]The continue condition: [ICODE]i < 20[/ICODE]
- [*]The change in the counter (in this case an increment): [ICODE]i++[/ICODE]
- [/LIST]
- So here we are creating a variable [ICODE]i[/ICODE] that starts from 0. We will repeat the code inside over and over again when [ICODE]i[/ICODE] is less than 20. At the end the [ICODE]for[/ICODE] loop, we are adding 1 to [ICODE]i[/ICODE] using the ++ operator.
- [URL unfurl="true"]https://pastebin.com/nF4JQ63M[/URL]
- [SIZE=7][B]99 Bottles[/B][/SIZE]
- In the last exercise, we saw an example of an incrementing [ICODE]for[/ICODE] loop so here we are going to show you how to write a [ICODE]for[/ICODE] loop where the counter goes down. When we know exactly how many times we want to iterate (or when we are counting), we can use a [ICODE]for[/ICODE] loop instead of a [ICODE]while[/ICODE] loop:
- Incrementing [ICODE]for[/ICODE] loop:
- [CODE=cpp]for (int i = 0; i < 20; i++)
- {
- // Statements
- }[/CODE]
- Decrementing [ICODE]for[/ICODE] loop:
- [CODE=cpp]for (int i = 20; i > 0; i--)
- {
- // Statements
- }[/CODE]
- [URL unfurl="true"]https://pastebin.com/ZMsuTCNh[/URL]
- [URL unfurl="true"]https://pastebin.com/cZFG6whu[/URL]
- [SIZE=7][B]Review[/B][/SIZE]
- Great job! [IMG]https://codecademy-content.s3.amazonaws.com/courses/learn-cpp/loops/review1.png[/IMG]
- Key concepts covered in this lesson:
- [LIST]
- [*]Loops perform repetitive actions so we don’t have to code those actions manually every time.
- [*]How to write [ICODE]while[/ICODE] loops with a continue condition.
- [*]How to write [ICODE]for[/ICODE] loops with a counter that increments or decrements.
- [/LIST]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement