Arithmetic using a for loop

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • heiro1
    New Member
    • Jan 2013
    • 29

    Arithmetic using a for loop

    Hello Everyone,
    One thing that I was not able to fully understand even though I read through the section on it a few times, is the for loop. I mean, I understand the premise of (statement, condition, update statement). However, I do not quite understand how a math problem is affected by this.

    Can someone clarify with a simple example of how this works using multiplication and division? I would really appreciate it!

    Oh, and lastly, why would you use a do.. while loop? What is it good for when compared to say, a sentinel loop? Thanks!
  • divideby0
    New Member
    • May 2012
    • 131

    #2
    the do while loop test condition is at the bottom and executes at least once. It's useful for user input prompts and waiting for specific values.

    Code:
    int i = 100;
    
    do
    {
       cout << i << "\n";
       i += 10;
       // enter loop at least once
    
    } while(i <= 100);
    I'm not sure what you mean by multiplication and division of the for loop. Do you mean the increment and decrement values?

    Code:
    int i;
    
    for(i=1; i <= 100; i *= 10) // mul
       cout << i << "\n";
    
    for(i = 100; i >= 1; i /= 10) // div
       cout << i << "\n";

    Comment

    Working...