Like Tree7Likes

Why new programming students aren't fairing well.

This is a discussion on Why new programming students aren't fairing well. within the C Programming forums, part of the General Programming Boards category; Originally Posted by Adak Code: int b=5; -b++; printf("b equals: %d",b); b equals: 6 In Pelles C. If you're replying ...

  1. #16
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    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
    21,734
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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.
    rags_to_riches likes this.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #22
    Registered User
    Join Date
    Mar 2010
    Posts
    535
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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,301
    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%.

  10. #25
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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
    Tclausex likes this.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    963
    Appalling that CS students are even exposed to MATLAB. Fortran is a mans language when it comes to numerical computing.

Page 2 of 3 FirstFirst 123 LastLast
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, 08: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, 03: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, 09:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21