music
OSdata.com: programming text book 

OSdata.com

while loop

summary

    This subchapter looks at while loop.

free computer programming text book project

table of contents
If you like the idea of this project,
then please donate some money.
more information on donating

Google

stub section

    This subchapter is a stub section. It will be filled in with instructional material later. For now it serves the purpose of a place holder for the order of instruction.

    Professors are invited to give feedback on both the proposed contents and the propsed order of this text book. Send commentary to Milo, PO Box 1361, Tustin, California, 92781, USA.

while loop

    The next important repetition or loop structure is the DO WHILE loop.

    Historically the first looping structure available was the iterative do loop. The DO WHILE loop was the form of loop used in the original proof of the concept of structured programming.

    In the DO WHILE loop there is a condition test and the loop is repeated over and over again as long as the condition remains true.

    We can use the DO WHILE loop to add flexibility to our average example. So far we have always had to have exactly five numbers to average. Using this method, we would need to write a whole new program for a list of six numbers to average and another program for a list of ten numbers to average. This would get ridicously out of hand in a hurry.

    By using a DO WHILE loop, we can write one program for any size list of numbers to average:

    Initialize;
    BEGIN
        Subtotal := 0;
        Count := 0;
        IF there are no numbers available to average
            THEN REPORT an ERROR and STOP;
    END;
    DO WHILE there are still numbers available;
    BEGIN
        Add the next number to the Subtotal;
        Increment (add one to) the Count;
    END;
    Finish;
    BEGIN
        IF count NOT EQUAL zero (0)
            THEN average := subtotal / count;
        ELSE
            Report ERROR;
    END;

    An important note regarding the DO WHILE loop (and loops in general) is that the loop needs to come to an end some time. If the loop never ends it is called an infinite loop and is one of the reasons that a program or computer freezes. The computer hasn’t actually stopped working (which is implied by the word freeze). Rather the program is stuck in a loop that it will never finish (until you turn the computer off, reboot, stop the program, etc.).

    It is important to check for and avoid inifinite loops. Make sure that any loop you write will eventually come to an end.

C

    Note that unlike most programming languages, C does not have an assignment statement, but rather has an assignment operator. This allows assignment to be compacted into other programming constructs, which can lead to confusion.

    This feature can be used to make very terse (and confusing) code:

while (*p++ = *q++)   ;

    The above loop copies copies data from one string or block of data to another until a zero is encountered.

Stanford C essentials

    Stanford CS Education Library This [the following section until marked as end of Stanford University items] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

No Boolean -- Use int

    C does not have a distinct boolean type-- int is used instead. The language treats integer 0 as false and all non-zero values as true. So the statement…

    i = 0;
    while (i - 10) {
       ...

    will execute until the variable i takes on the value 10 at which time the expression (i - 10) will become false (i.e. 0).

While Loop

    The while loop evaluates the test expression before every loop, so it can execute zero times if the condition is initially false. It requires the parenthesis like the if.

    while (<expression>) {
       <statement>
    }

Break

    The break statement will move control outside a loop or switch statement. Stylistically speaking, break has the potential to be a bit vulgar. It’s preferable to use a straight while with a single test at the top if possible. Sometimes you are forced to use a break because the test can occur only somewhere in the midst of the statements in the loop body. To keep the code readable, be sure to make the break obvious -- forgetting to account for the action of a break is a traditional source of bugs in loop behavior.

    Stanford CS Education Library This [the above section] is document #101, Essential C, in the Stanford CS Education Library. This and other educational materials are available for free at http://cslibrary.stanford.edu/. This article is free to be used, reproduced, excerpted, retransmitted, or sold so long as this notice is clearly reproduced at its beginning. Copyright 1996-2003, Nick Parlante, nick.parlante@cs.stanford.edu.

end of Stanford C essentials

Stanford Perl essentials

    This [the following section until marked as end of Stanford University items] is document #108 [Essential Perl] in the Stanford CS Education Library --see http://cslibrary.stanford.edu/108/. This document is free to be used, reproduced, or sold so long as this paragraph and the copyright are clearly. Copyright 2000-2002, Nick Parlante, nick.parlante@cs.stanford.edu.

6. If/While Syntax

    Perl’s control syntax looks like C’s control syntax. Blocks of statements are surrounded by curly braces { }. Statements are terminated with semicolons (;). The parenthesis and curly braces are required in if/while/for forms. There is not a distinct “boolean” type, and there are no “true” or “false” keywords in the language. Instead, the empty string, the empty array, the number 0 and undef all evaluate to false, and everything else is true. The logical operators &&, ||, ! work as in C. There are also keyword equivalents (and, or, not) which are almost the same, but have lower precedence.

Loops

    These work just as in C…

    while (expr ) {
      stmt;
      stmt;
    }

    The “next” operator forces the loop to the next iteration. The “last” operator breaks out of the loop like break in C. This is one case where Perl (last) does not use the same keyword name as C (break).

    This [the above section] is document #108 [Essential Perl] in the Stanford CS Education Library --see http://cslibrary.stanford.edu/108/. This document is free to be used, reproduced, or sold so long as this paragraph and the copyright are clearly. Copyright 2000-2002, Nick Parlante, nick.parlante@cs.stanford.edu.

end of Stanford Perl essentials


free music player coding example

    Coding example: I am making heavily documented and explained open source code for a method to play music for free — almost any song, no subscription fees, no download costs, no advertisements, all completely legal. This is done by building a front-end to YouTube (which checks the copyright permissions for you).

    View music player in action: www.musicinpublic.com/.

    Create your own copy from the original source code/ (presented for learning programming).


return to table of contents
free downloadable college text book

view text book
HTML file

Because I no longer have the computer and software to make PDFs, the book is available as an HTML file, which you can convert into a PDF.

previous page next page
previous page next page

free computer programming text book project

Building a free downloadable text book on computer programming for university, college, community college, and high school classes in computer programming.

If you like the idea of this project,
then please donate some money.

send donations to:
Milo
PO Box 1361
Tustin, California 92781

Supporting the entire project:

    If you have a business or organization that can support the entire cost of this project, please contact Pr Ntr Kmt (my church)

more information on donating

Some or all of the material on this web page appears in the
free downloadable college text book on computer programming.


Google


Made with Macintosh

    This web site handcrafted on Macintosh computers using Tom Bender’s Tex-Edit Plus and served using FreeBSD .

Viewable With Any Browser


    †UNIX used as a generic term unless specifically used as a trademark (such as in the phrase “UNIX certified”). UNIX is a registered trademark in the United States and other countries, licensed exclusively through X/Open Company Ltd.

    Names and logos of various OSs are trademarks of their respective owners.

    Copyright © 2010, 2011 Milo

    Created: October 31, 2010

    Last Updated: march 3, 2011


return to table of contents
free downloadable college text book

previous page next page
previous page next page