C++ Do While Syntax

Spread the love

Elements of a c++ do while loop


do {
statement(s);
} while( condition );

#include <iostream>

int main(){
   int userCounter = 0,cnt=0;
   std::cout << "how many times should we loop ?\n";
   std::cin >> userCounter;
  
   do {
       std::cout << "Counting to " << userCounter 
                 << " now in the loop for " 
                 << cnt + 1 << " times\n";
       cnt++;
   }while( cnt < userCounter );

}


So Line 13 above, was the critical one here. Note that all code executes before any tests are made.



Explica por me, Por Favor – Yes, I will since you’re so nice

C++ do while syntax explained …

While there is initially no condition, all code is executed. When the execution point reaches the while conditional loop construct at the end, if the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.

Would strongly suggest once again, since this code block is short

  • Memorize
  • Repeat line by line out loud twice
  • Write it on paper
  • Write it into your c++ code compiler/Editor
  • Tell somebody, discuss somewhere, write your comments below


Homework — Yes, We do that here too

  • 1) Memorize this short code
  • 2) Rewrite the code above into your c++ compiler/editor
  • 3) Change the variable names used for counters
  • 4) change the text used in the cout statement and
  • 5) make the loop occur 14 times (not a code change)


– > Lesson 8 C++ Array of Strings


Top