Advertisement
bhok

Loop Tutorial

Jun 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. // More Tutorials at Brandonhok.com
  2.  
  3. #include <iostream>
  4.  
  5. // There are a total 3 looping structures in C++
  6.  
  7. // While, for, do...while
  8.  
  9. // So, what does a loop has?
  10. // The name of control variable / loop counter
  11. // Loop-coninuation condition
  12. // increment or decrement control variable
  13.  
  14.  
  15. int main()
  16. {
  17.     // Code Set 1
  18.     // Examine what is a 'while' statement
  19.  
  20.     int counter = 0; // loop counter
  21.  
  22.     while (counter < 5) // condition
  23.     {
  24.         std::cout << "Popcorn power" << std::endl;
  25.         ++counter; // increment variable
  26.     }
  27.  
  28.     // Code Set 2
  29.     // Now check out the 'for' statement
  30.  
  31.     for (int counter = 1; counter <= 10; counter++)
  32.     {
  33.         std::cout << "This is MEGA! \n";
  34.     }
  35.  
  36.     // Code Set 3
  37.     // Write down the similarities between 'do...while' and 'while'
  38.     int counter2 = 0;
  39.     do
  40.     {
  41.         std::cout << "This is how we do it";
  42.         ++counter2;
  43.     } while (counter2 <= 10);
  44.  
  45.  
  46.     // Try making some of your own code now
  47.     // A good challenge is seeing if you can get an 'if' statement working
  48.     // with these.
  49.     // How about using true and false?
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement