Thread: Don't understand Loops

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    Somewhere in Malaysia
    Posts
    16

    Don't understand Loops

    Can anyone tell me what exaxtly IS loops? I did learn the tutorial in http://www.cprogramming.com/tutorial/lesson3.html . But i don't seemed to understand what's the function of "endl". I also don't understand why why is endl put in, for example, cout<< x <<endl;. And what is x++, x = x + 10, or even x = random ( 5 ). What do they mean when they says "The variable update section is the easiest way for a for loop to handle changing of the variable"?
    I fully understand DO..WHILE loops, and i think I understand a bit of While loops.
    But still, can anyone explain what's While loops is? Thanks in advance people =)

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    it is not While. C++ is case sensitive.
    Code:
    while (/*condition*/(1!=2 && 2 = (3-1))
    { // starting brace
       //code for while loop
    } // ending brace
    a while loops is identical to a do while, except to put while at the top of the section, and take out the do.
    a for loop is self explanatory.

    as for you firsts question.
    loops is just a word to suggest the use of a while loop, a for loop, of a do while loop

    std::endl is a function that clear the output buffer, and carries your cursor to a new line. ( endl == endline )

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Code:
    cout << x << endl;
    Send through the standard output stream some representation of x and then a new line.

    Code:
    x++;
    Add one unit to x. Return old value.

    Code:
    x = x + 10;
    Assign a value to x that is ten more than what x currently is.

    Code:
    x = random(5);
    I am not familiar with this, but...
    Code:
    x = rand() % 5;
    would be a rough (and usually naive) way to assign a value to x that is between 0 and 4, inclusive.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Registered User
    Join Date
    May 2007
    Location
    Somewhere in Malaysia
    Posts
    16

    Unhappy

    Quote Originally Posted by Raigne View Post
    Code:
    while (/*condition*/(1!=2 && 2 = (3-1))
    a for loop is self explanatory.

    std::endl is a function that clear the output buffer, and carries your cursor to a new line. ( endl == endline )
    I still don't get it. The condition you've given, which is (1!=2 && 2 = (3-1), does what? I know that in the bracket, 1 != 2 means that when 1 is not equal to 2. I also know that in the bracket, && means AND, which states that both value must be true in order to get a true. But i really don't understand what's that 2 = (3-1) thing. Why is that equation doing there and what is it purpose? Oh, and when I look at the tutorial, they had something like x++, and x = rand or something. What are all those???

    Oh, and you said that endl is a function that clear the output buffer, and carries your cursor to a new line. ( endl == endline) Do you mean that it is something like cout<<"blah\n". Do you mean that it posses the same function as "\n" in the cout, which also means the next line?

    Man, i'm soo noob T.T. Can I really learn C++? It looks really hard and indeed, it's hard to learn. I really want to learn it well. Those examples given in thsoe tutorials arn't enough. Can you guys add me in MSN, so that you guys can teach me please? I am really desparate in learning C++. I want to learn all its C++ function. <<snipped>>

    Oh, and by the way, I'm a complete newbie/noob in programming. I have no previous programming knowledge. This is my first time learning how to programme...So please guys, lend a helping hand. I really need help~~
    Last edited by Salem; 06-13-2007 at 01:01 AM. Reason: This is a message board, not a dating service for swapping messenger contacts.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by Raigne View Post
    it is not While. C++ is case sensitive.
    a while loops is identical to a do while, except to put while at the top of the section, and take out the do.
    Nonsense. A do-while loop is guarenteed to have its contents executed at least once. A while loop on the other hand can quite happily execute the loop contents zero times, if the condition is initially false. It's all about whether you evaulate the condition beforehand or not.

    2 = (3-1) is incorrect because of the missing double-equals (==), and will not compile. It would have to be 2 == (3-1)
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    Registered User
    Join Date
    May 2007
    Location
    Somewhere in Malaysia
    Posts
    16
    zomg, still I only understand the while loop and the for loop. I really dun understand for loops>.< Can you guys explain in details and give some working examples, made it so that newbie can understand it well. Oh, and seriously, what's the real function of endl? Is it the same function as "\n" in the cout, like the next line?

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Quote Originally Posted by ofayto View Post
    zomg, still I only understand the while loop and the for loop. I really dun understand for loops>.< Can you guys explain in details and give some working examples, made it so that newbie can understand it well.
    Print "Hello world" 100 times:
    Code:
    for (int i = 0; i < 100; ++i) {
        cout << "Hello world" << endl;
    }
    Print "Hello world while user doesn't type "N":
    Code:
    char choice = 'Y';
    do {
        cout << "Hello world" << endl;
        cout << "Do again? Y/N ";
        cin >> choice;
    } while (choice != 'N');
    Oh, and seriously, what's the real function of endl? Is it the same function as "\n" in the cout, like the next line?
    endline. Same as "\n", except it flushes the output buffer, forcing immediate write to the screen. Therefore "\n" may be more efficient, as the buffer is written to the screen when it gets full (or some other condition is met - program termination, input statement etc)

    You should probably read some tutorials and experiment with code snippets yourself.
    Last edited by anon; 06-13-2007 at 08:07 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  8. #8
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Quote Originally Posted by CodeMonkey
    I am not familiar with this, but...
    Code:
    x = rand() % 5;
    would be a rough (and usually naive) way to assign a value to x that is between 0 and 4, inclusive.
    Thats how I would generate a random number (0-4) in C/C++. Is there a better way to do this?

  9. #9
    Registered User
    Join Date
    Jan 2006
    Location
    Sweden
    Posts
    92
    Quote Originally Posted by ofayto View Post
    zomg, still I only understand the while loop and the for loop. I really dun understand for loops>.< Can you guys explain in details and give some working examples, made it so that newbie can understand it well. Oh, and seriously, what's the real function of endl? Is it the same function as "\n" in the cout, like the next line?
    A for loop works like this:
    Code:
    for (*called when initialized*; *condition to be true*; *called after each loop*)
    {
    }
    
    //Example:
    for (int i = 0; i < 5; i++)
    {
        cout << i << endl;
    }
    On initialization we declare a variable i and set it to zero, then we say that to run the code i should be less than five. And at last we say that after each loop i should be increased by one.
    Inside the loop we simply print out the value of i, followed by an endline.

  10. #10
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Thats how I would generate a random number (0-4) in C/C++. Is there a better way to do this?
    Beware of C/C++. But yes, there is a better way. It is because not all of the bits in a value given by std::rand() have a good period. This has been discussed on the board many times.
    Read under "Standard Library" here: http://eternallyconfuzzled.com/tuts/..._tut_rand.aspx

    *edit* even better, just read this: http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx
    Last edited by CodeMonkey; 06-13-2007 at 10:08 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #11
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Cheers. That sort of stuff is good to know

  12. #12
    Registered User
    Join Date
    Jun 2007
    Posts
    61
    So there is someone else having trouble with C++ can anyone do tutoring one on one--need help with the same trouble as above and much more--I'll pay for the help

Popular pages Recent additions subscribe to a feed