Thread: Why new programming students aren't fairing well.

  1. #16
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by Adak View Post
    Code:
    int b=5;
    -b++;
    printf("b equals: %d",b);
    b equals: 6

    In Pelles C.
    If you're replying to what I said, you should note that I said that b is 6 but that expression evaluates to -5. i.e.

    Code:
    int result, b = 5;
    result = -b++;
    printf("%d\n%d\n", result, b); // prints -5 and 6, respectively

  2. #17
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Quote Originally Posted by KCfromNC View Post
    This is wrong. Hopefully you just misunderstood and they were saying that you can code any while loop as a for loop or vice versa, but what you've described above is not how compilers work.
    I'm going to go with this is a common misconception then. I've heard it a few times before, and for instance this article:

    c - Example of a while loop that cannot be replaced by a for loop - Stack Overflow

    Someone clearly says "Most compilers actually expand for loops to while loops", which doesn't really leave it open for interpretation. This doesn't mean that what he said is correct, but do you have supporting evidence?

  3. #18
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Epy
    Someone clearly says "Most compilers actually expand for loops to while loops", which doesn't really leave it open for interpretation. This doesn't mean that what he said is correct, but do you have supporting evidence?
    Supporting evidence would be that compilers usually translate to some intermediate language and work with that, so any "expand some type of loop to another type of loop" would be in terms of that intermediate language, not C, and it may just involve goto or an equivalent, not specific loop constructs.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #19
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thanks Epy, that cleared it up.

  5. #20
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    I don't know why I even brought up the thing about the for loops. Pretty much all loops end up using the jump if something instruction (jne, jle, whatever), right?

    Edit: point is, it basically ends up being the same machine code in the end, how can for loops be more efficient than while loops? There's always some sort of conditional that's executed every iteration of the loop.

  6. #21
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Epy View Post
    I don't know why I even brought up the thing about the for loops. Pretty much all loops end up using the jump if something instruction (jne, jle, whatever), right?

    Edit: point is, it basically ends up being the same machine code in the end, how can for loops be more efficient than while loops? There's always some sort of conditional that's executed every iteration of the loop.
    A for-loop communicates intent. Both to other programmers and to the compiler itself. It only takes 10 minutes of fooling with the compiler to see that in some cases it generates different code depending how you write a loop. Whether this is just happenstance or indicates something deliberate inside the compiler, I don't know. It's true that everything compiles to some intermediate format which is where most optimizations happen, there's nothing that says the compiler can't take advantage of additional information if it wants to.

    For instance, in some big complex while-loop it may be unclear which variable plays the role of indexer. In a for-loop it is usually very obvious which variable is the indexer.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #22
    Registered User
    Join Date
    Mar 2010
    Posts
    583
    Quote Originally Posted by Tclausex View Post
    It has amazed me how poorly prepared students are for the more advanced programming classes I've taken, but recently I've had to go back and take a basic C course for my degree. Now I know why.
    Wow. That is very scary stuff. You should complain to someone.... there are enough plain WRONG points there.

    Quote Originally Posted by Tclausex View Post
    7. Never, ever, ever use break or continue. ( I'm sure there are others who feel the same way on this ).
    Heh - I remember my supervisor at university saying that to me too. Well, not "never ever ever", but "break and continue are bad!". I asked him why, he said that it is difficult to formally prove correctness of a loop with breaks/continues. I don't know if that's widely true or if it was just true of the particular methods he was using.
    I suppose then that "never, ever, ever use break or continue" might be good advice if you were working on a safety critical system where code needed to be proven.

    "Never ever ever use goto", I'll stand by

  8. #23
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by smokeyangel View Post
    Heh - I remember my supervisor at university saying that to me too. Well, not "never ever ever", but "break and continue are bad!". I asked him why, he said that it is difficult to formally prove correctness of a loop with breaks/continues. I don't know if that's widely true or if it was just true of the particular methods he was using.
    I suppose then that "never, ever, ever use break or continue" might be good advice if you were working on a safety critical system where code needed to be proven.
    Most normal uses of break/continue can easily be rewritten to not use them. They're meant to allow you to write clearer code, not to enable you to do things you couldn't otherwise do. There are probably cases where break/continue is the only way to accomplish a certain control flow, but I can't imagine them, and it's usually possible to transform a piece of code that uses them into a piece of code that doesn't.

    For instance, a loop like this:

    Code:
    for (...)
    {
        foo();
        if (bar())
            break;
        baz();
    }
    Can be replaced with this:

    Code:
    bool fBar = false;
    for (...)
    {
        if (!fBar)
        {
            foo();
            if (bar())
                fBar = true;
            else
                baz();
        }
    }
    It's inefficient, more difficult to read, but it does not use break. But I'd question the sanity of somebody who told me the second loop is easier to analyze than the first.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  9. #24
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Your replacement isn't correct, brewbuck, as it doesn't terminate the loop. You probably intended something like

    Code:
    bool fBar = false;
    for (!fBar && ...)
    {
            foo();
            if (bar())
                fBar = true;
            else
                baz();
    }
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    All 1 to 7 are all very wrong. No surprise there, as that's the whole reason Tclausex posted them.

    1, 2, 3, and 5 can be disproven using an appropriate code sample.
    4 Disagrees with a decent percentage of standard library functions.
    6 can be disproved using an appropriate compiler. Alternatively one can take the stance that for is not more efficient than do..while because they are not interchangeable.
    7 If I catch anyone avoiding an appropriate use of break and instead inserting a dumb flag variable then they get a flogging.
    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"

  11. #26
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    To be perfectly fair, most schools start the basic programming with Java.

  12. #27
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Strahd View Post
    To be perfectly fair, most schools start the basic programming with Java.
    The Perils of JavaSchools - Joel on Software

  13. #28
    Registered User
    Join Date
    Sep 2011
    Posts
    81
    We have to take multiple C classes before we can take upper level Programming classes that include other languages. There is an intro to OOP with Java but you have to take the first C course first. I'm glad it's this way at my uni.

  14. #29
    Registered User
    Join Date
    Sep 2011
    Posts
    111
    Quote Originally Posted by manasij7479 View Post
    Now now I do agree, when I said "start out with java" I meant as the first basic intro to programming class.

    Some schools I've seen it goes

    Intro (java)

    Intermed (java)
    Data Struct (C)

    Large Programs (Java)
    Intro to Computing Systems (Assembly)

    And they hit other langs, like VHDL, MATLAB, SCHEME, etc during the progress.

  15. #30
    Make Fortran great again
    Join Date
    Sep 2009
    Posts
    1,413
    Appalling that CS students are even exposed to MATLAB. Fortran is a mans language when it comes to numerical computing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. O_o cin and cout aren't declared?
    By charlybob in forum C++ Programming
    Replies: 6
    Last Post: 10-03-2006, 11:25 AM
  2. Vindictive bunch, aren't we?
    By samGwilliam in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 04-05-2005, 06:59 PM
  3. they aren't teh
    By neandrake in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 11-21-2004, 09:42 AM
  4. Any Computeach International Students/ex students??
    By stevey in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 03-26-2002, 04:12 PM
  5. Why aren't we all here 24/7?
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-30-2001, 10:49 PM