infinite loop

Discussion in 'Scripting & Programming' started by Mof, Oct 2, 2008.

  1. Mof

    Mof Megabyte Poster

    526
    2
    49
    this code should print 1 to 10 but for some reason its a infinity loop, i should use break but cant work out where. soits time to call on my betters.:rolleyes:


    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
     vector <int> vec(10);
     int i=0;
     while (i<vec.size());
       {
        i++;
        vec[i-1]=i;
        cout << "|" << vec.at(i-1);
       }
    return 0;
    }
     
    WIP: C++ and A+
  2. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Thats because this :-

    Code:
    while (i<vec.size());
    Is an infinite loop. The ; terminates the loop, meaning there is no body or increment for the loop. You just check that i which is zero is less than 10 again and again infinitely.

    Also note that vec.size() is unsigned and i is signed, comparisons where i is negative will not perform as you may think ! In general you should not mix signed and unsigned values.
     
  3. The Zig

    The Zig Kilobyte Poster

    305
    19
    46
    I must've spent hours at uni, staring at c/c++ code that wouldn't work.
    It almost always traced back to a missing/misplaced semi-colon in the end...


    ... they're sneaky.
     
    Certifications: A+; Network+; Security+, CTT+; MCDST; 4 x MTA (Networking, OS, Security & Server); MCITP - Enterprise Desktop Support; MCITP - Enterprise Desktop Administrator; MCITP - Server Administrator; MCSA - Server 2008; MCT; IOSH; CCENT
    WIP: CCNA; Server 2012; LPIC; JNCIA?
  4. Mof

    Mof Megabyte Poster

    526
    2
    49
    Cheers Dmarsh thats been bafferling me for 2 nights, pulled half my hair out, I knew the semi-colon termanated the process. but thought it would proceed to the next instruction. Thanks for poiting it out to me, thanks again.:slidedrin:morebeer
     
    WIP: C++ and A+
  5. Mof

    Mof Megabyte Poster

    526
    2
    49

    sneeky putting it politely:mad:cussing
     
    WIP: C++ and A+
  6. dmarsh
    Honorary Member 500 Likes Award

    dmarsh Petabyte Poster

    4,305
    503
    259
    Just think of it like this :-

    Code:
    while (i<vec.size()){/* No body, NO-OP! */}
    You don't blindly insert semicolons on every line, they are not line terminators, they are statement terminators ! :D

    You can often put in extra semicolons without bad effects as they act as a NO-OP if they are extraneous. eg.

    Code:
    cout << "Hello World!" << endl;;;;
    You can put in extra scoping code blocks {} also without bad effects, your intended loop body became a code block.

    However these two points are not recommended as good readable code, when coding you should aim to express the solution as clearly and cleanly as possible without any unecessary fluff. Code blocks are seldom used outside of control structures, you should generaly instead create a function using an extract method refactoring.

    See here :- http://www.jdl.co.uk/briefings/briefing5day.html

    I expect you intended something like this :-

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    {
     vector<int> vec(10);
    
     for(unsigned int i=0; i<vec.size(); i++)
     {
        vec[i]=i;
        cout << "|" << vec.at(i);
     }
      
     return 0;
    }
    
    Generally most C++ programmers prefer to use for loops over while loops.

    Iterators are preferred over indexes to avoid off by one errors, I'll leave it as an exercise for you to rewrite using an STL vector forward iterator.
     
  7. Mof

    Mof Megabyte Poster

    526
    2
    49

    Cheers Ill have a look at this tonight.
     
    WIP: C++ and A+
  8. Mathematix

    Mathematix Megabyte Poster

    969
    35
    74
    Although a perfectly legitimate reason, it isn't the reason why the loop is infinite. The reason why it is 'infinite' is because the value of 'i' isn't updated. Execute this bit of code to see what I mean.

    [edit]
    Very similar to what you said. Ignore me.
    [/edit]

    Code:
    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main()
    {
     vector <int> vec(10);
     int i=0;
     while (i<vec.size(), i++);	// 'While' condition maintained in addition to 'i' being incremented. Loop is now finite.
       {
        i++;
        vec[i-1]=i;
        cout << "|" << vec.at(i-1);
       }
    return 0;
    }
    
    Don't ever code like this, btw! It's only to illustrate the exact reason why it was infinite.

    Not as far as I'm aware. 'for loops' are used when you wish to perform a task a set number of times within a range of values, and this iteration can be performed zero or more times. A 'while' loop will perform an iteration only if the condition holds true, and its body may also only be iterated through zero or more times.

    Well, no. Iterators are preferred because they provide ways of iterating through array-like structures using a standard set of routines, like getting the value at the end of the array, counting elements, etc.
     
    Certifications: BSc(Hons) Comp Sci, BCS Award of Merit
    WIP: Not doing certs. Computer geek.

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.