Thread: need help explaining this code

  1. #1
    Registered User
    Join Date
    Jan 2005

    need help explaining this code

    I'm reading my c++ book and i have come across this code that im not quite sure as to what some parts mean and the book doesnt describe them.

    1. What does the first "for loop" mean?

    i know the basics of the for statement and i know what it does because the book shows me, but i wouldnt know how to re-write that into my own program because i dont know what the specific parts stand for.

    2. What is the significance of the second "for loop"?

    Isn't it the same as the first "for loop"

    Here is the code

    #include <iostream>
    #include <string>
    int main ()
         using namespace std;
         int counter = 1;
         string response;
         cout << "Shoul i start counting? ";
         cin>> response;
         for (int i = 0; i < response.length(); i++) {
              response[i] = toupper (response[i];
         while (response == "YES")  {
         cout << "counter is " << counter << endl;
         cout << "Should i continue? ";
         cin >> response;
         for (int i = 0; i < response.length(); i++) {
              response[i] = toupper (response[i];
    cout << "Thanks for counting with me!";
    return 0;

  2. #2
    Registered User
    Join Date
    Jan 2005
    i think i just got the second for statement! haha. Correct me if im wrong...

    i know the for statement makes the counter increase by one and since the second for statement is within the while statement then everytime the word YES is typed, it will increase the counter by 1 and repeat

  3. #3
    Registered User
    Join Date
    Apr 2003
    that seems right to me

  4. #4
    Registered User
    Join Date
    Jan 2005

    help with while loop

    woops wrong post

  5. #5
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    The first loop converts response character by character into uppercase.

    And now for Professer fuh's Computer Science Biology Class:

    Good morning class. Today we will be studying the anatomy of a for loop in C++.

    We shall first look at a sample loop as a whole.
    for( int i; i<=10; i++ )
    Now we shall proceed and take it apart one piece at a time.

    This declares the for loop, with the "..." standing for the other parts of the loop which we will now dissect.

    int i;
    This declares that we will use int i as the variable to be incremented.

    [Steve Irwin, crocodile hunter, talks]
    Hey there, mate! Some compilers may growl and bite when shown this, complaining about multiple declarations. That makes me say, "Crikey!" If this ever happens you'd do best to follow the compiler's request and remove "int" from the declaration, leaving only the "i".
    [/Steve Irwin, crocodile hunter, talks]

    The second part of the loop will soon have its mysteries revealed to you.

    The purpose of this section is that the loop shall function properly only if i is less than or equal to 10. How does it get to ten, you may ask. Well, that is explained in the third portion of this lesson.

    You, sir! Give me that note! Yes, you! Yes, I do intend to read it in front of the class! No "buts"! Ahem!

    "Hey Mark, isn't the professer off his rocker? What does he think he's talking about, what with the 'i++'s and all! See you later, Joe."

    Well, Joseph has brought up a fine point, even though it has earned him detention. The "i++" shows what will happen when the loop runs through one time. In this case, at the end of every run i will be incremented by one. When the second part of the loop's requirements is fulfilled, the loop stops.

    Please note that you can change the loop in many ways, such as changing "int i" to any other letter of your choice and modifying the requirements for the second part of the loop so that it runs while the code is less than 25, or any other number of your choice. Also, it is possible to make the variable increment by 2, or 3 or even 1000000! Another way to modify the code, although this is not advised, is by making the statement read as follows and the loop will run infinately.


    My, where has the time gone! Is class over already? There will be a test on Tuesday. Go forth and spread your newfound knowledge on the world! Class dismissed.


    Don't you love biology class?

    To answer your second question:
    The second for loop does exactly the same thing as the first one.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Code sippet needs explaining
    By steve1_rm in forum C Programming
    Replies: 6
    Last Post: 02-01-2008, 04:52 AM
  2. Proposal: Code colouring
    By Perspective in forum A Brief History of
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM