Advertisement
ItzEdInYourBed

69 - C++ Loops

Jul 12th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.49 KB | None | 0 0
  1. [SIZE=7][B]Introduction to Loops[/B][/SIZE]
  2. 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”.
  3.  
  4. 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.
  5.  
  6. In this lesson, we will learn about two types of loops: [ICODE]while[/ICODE] loops and [ICODE]for[/ICODE] loops!
  7.  
  8. [SIZE=7][B]While Loop Demo[/B][/SIZE]
  9. So first up… the [ICODE]while[/ICODE] loop!
  10. Before we dive deep into the syntax of the [ICODE]while[/ICODE] loop, let’s do a demo.
  11. 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.
  12. [B]Note:[/B] You don’t need to understand the code right now.
  13. [URL unfurl="true"]https://pastebin.com/06XEAbKA[/URL]
  14.  
  15. [SIZE=7][B]Guess Number[/B][/SIZE]
  16. So now that we got a demo of loops, let’s write one!
  17. 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].
  18. 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].
  19. Here is what a [ICODE]while[/ICODE] loop looks like:
  20. [CODE]while (condition) {
  21.  
  22. statements
  23.  
  24. }[/CODE]
  25. In other words, instead of executing [I]if[/I] something is true, it executes [I]while[/I] that thing is true.
  26. [CODE=cpp]while (guess != 8) {
  27.  
  28. std::cout << "Wrong guess, try again: ";
  29. std::cin >> guess;
  30.  
  31. }[/CODE]
  32. 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.
  33.  
  34. [SIZE=7][B]For Loop Demo[/B][/SIZE]
  35. [IMG]https://i.imgur.com/Qk8Ljcz.png[/IMG]
  36. ([FoxTrot](https://www.foxtrot.com))
  37. Iterating over a sequence of numbers is so common that C++, like most other programming languages, has a special syntax for it.
  38.  
  39. 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:
  40. [CODE=cpp]for (int i = 0; i < 20; i++)
  41. {
  42.  
  43. std::cout << "I will not throw paper airplanes in class.\n";
  44.  
  45. }[/CODE]
  46. Let’s take a closer look at the first line:
  47. [CODE=cpp]for (int i = 0; i < 20; i++)[/CODE]
  48. There are three separate parts to this separated by [ICODE];[/ICODE]:
  49.  
  50. [LIST]
  51. [*]The initialization of a [I]counter[/I]: [ICODE]int i = 0[/ICODE]
  52. [*]The continue condition: [ICODE]i < 20[/ICODE]
  53. [*]The change in the counter (in this case an increment): [ICODE]i++[/ICODE]
  54. [/LIST]
  55. 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.
  56. [URL unfurl="true"]https://pastebin.com/nF4JQ63M[/URL]
  57.  
  58. [SIZE=7][B]99 Bottles[/B][/SIZE]
  59. 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:
  60. Incrementing [ICODE]for[/ICODE] loop:
  61. [CODE=cpp]for (int i = 0; i < 20; i++)
  62. {
  63. // Statements
  64. }[/CODE]
  65. Decrementing [ICODE]for[/ICODE] loop:
  66. [CODE=cpp]for (int i = 20; i > 0; i--)
  67. {
  68. // Statements
  69. }[/CODE]
  70. [URL unfurl="true"]https://pastebin.com/ZMsuTCNh[/URL]
  71. [URL unfurl="true"]https://pastebin.com/cZFG6whu[/URL]
  72.  
  73. [SIZE=7][B]Review[/B][/SIZE]
  74. Great job! [IMG]https://codecademy-content.s3.amazonaws.com/courses/learn-cpp/loops/review1.png[/IMG]
  75. Key concepts covered in this lesson:
  76. [LIST]
  77. [*]Loops perform repetitive actions so we don’t have to code those actions manually every time.
  78. [*]How to write [ICODE]while[/ICODE] loops with a continue condition.
  79. [*]How to write [ICODE]for[/ICODE] loops with a counter that increments or decrements.
  80. [/LIST]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement