many novice c programmers seem to have trouble understanding the side effects originating from a...

Upload: polepoleta

Post on 30-May-2018

213 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    1/10

    Many novice C programmers seem to have trouble understanding the side effects originating from abasic flow control mechanism such as the 'for' cycle. This article is an attempt at clarifying theseconcepts once and for all.For Loop Syntax in C-Like Programming Languages

    In all C-like programming languages, the for construct is composed of three parts, each divided by a

    semicolon:for(initialization; test; update) { ... }

    * In the first section, the initialization, we initialize an index variable (which is usually being namedi, j or k) to its initial value;

    * In the second part, we test whether a variable (usually the same we have just initialized in the firstpart) satisfies a certain condition: if it does, we enter the loop one more time, otherwise we exit from it;

    * In the third and last part, we update the variable usually by incrementing or decrementing it by1.

    How the For Loop WorksanggiKnowing the syntax of the for loop is not enough: in fact, what is usually the major source of confusion

    among novice programmers is not the syntax itself, but rather the runtime behavior of the construct.

    for(i = 0; i < 10; i++) { printf("%d ", i); }

    The snippet above is a very basic example of such a loop. Let's see what happens when we run thiscode, assuming that the block enclosed within curly brackets doesn't otherwise modify the value of theindex i:Ads by GoogleUpdated Football NewsGet Updates On Each Team And Group. News And Info As They HappenWorldCup.Tweetbeat.comPremium Soy SauceKorean Represented Traditional Food Soy Sauce, BBQ Sauce, Chilli Saucewww.sempio.com

    1. The variable i is initialized to 0;2. The test "i < 10" is performed, and the code enclosed in curly brackets is executed if the test is

    successful, exiting the loop otherwise;3. The index i is incremented (i++);4. The points 2-3 are repeated until the test "i < 10" fails and we exit the loop.

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    2/10

    As a result, as it can be easily verified, the output would be:0 1 2 3 4 5 6 7 8 9

    Notice that the number 10 is not being printed, because i is first incremented from 9 to 10, and thentested. When we exit the loop, we do so because the condition i < 10 is no more true, since i is now

    equal to 10.Special For Loops: While Loops and Infinite Loops

    The peculiar syntax of the for loop can be adapted to a wide variety of situations, and can even used tobehave in the exact same fashion as a while() loop. For instance, consider what happens when wewrite:for(;i < 10;) { ... }

    This somewhat cryptic code is equivalent to while(i < 10) { ... } in that, since the first and last part areabsent, we simply test for i < 10 without initializing nor updating the variable. For this reason, we saythat the while() loop is nothing but a special case of the for loop.

    When using the for construct, novice programmers must always be careful to choose a condition that isguaranteed to terminate the loop at some point in time: the risk is of incurring in a so-called infiniteloop. Consider for instance this piece of code:for(;;);

    which is a clear programming error that will loop indefinitely and consume all the available processortime until it will be manually terminated by the user.Share Article |The copyright of the article Introduction to 'For' Loops in C in Computer Programming is owned byDario Borghino. Permission to republish Introduction to 'For' Loops in C in print or online must begranted by the author in writing.Ads by GoogleHire a Freelancer

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    3/10

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    4/10

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    5/10

    anggi

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    6/10

    Find Freelance Writers, Developers Post Projects for Free. Earn Incomewww.surefirehire.com

    Renal or Kidney Problem

    No donor. Homeopathy can treate CKD without dialysis & transplantationwww.cancertb.com

    Freelance Writing JobMake a full time income writing Up to $20/page. Start Earning Now!www.academia-research.com

    Stem cell and plateletCutting edge regenerative stem cell Platelet Rich Plasma, or PRP, is blwww.StemCellOrthopedic.com

    What do you think about this article?

    anggi

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    7/10

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    8/10

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    9/10

  • 8/9/2019 Many Novice C Programmers Seem to Have Trouble Understanding the Side Effects Originating From a Basic Flow

    10/10