music
OSdata.com: programming text book 

OSdata.com

else

summary

    This subchapter looks at else.

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.

else

    The IF-THEN-ELSE structure gives two different paths, which must combine back together at the end to maintain structured programming.

    To use our average example, we could text for the count not being zero. If the count is not zero, we perform the THEN portion (single sequential step, block of code, subroutine, etc.), which in this example would be the division step. If the count is zero, then we would perform the ELSE portion (single sequential step, block of code, subroutine, etc.).

    In this case, we could report that there is an error, so that the reader will know why he or she didn’t receive an answer for the average.

    IF count NOT EQUAL zero (0)
        THEN average = subtotal / count
    ELSE
        REPORT an ERROR;

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.

Else Statement

    Both an if and an if-else are available in C. The <expression> can be any valid expression. The parentheses around the expression are required, even if it is just a single variable.

    if (<expression>) <statement>    // simple form with no {}'s or else clause

    if () {    // simple form with {}'s to group statements
       <statement>
       <statement>
    }

    if () {    // full then/else form
       <statement>
    }
    else {
       <statement>
    }

Conditional Expression -or- The Ternary Operator

    The conditional expression can be used as a shorthand for some if-else statements. The general syntax of the conditional operator is:

    <expression1> ? <expression2> : <expression3>

    This is an expression, not a statement, so it represents a value. The operator works by evaluating expression1. If it is true (non-zero), it evaluates and returns expression2 . Otherwise, it evaluates and returns expression3.

    The classic example of the ternary operator is to return the smaller of two variables. Every once in a while, the following form is just what you needed. Instead of…

    if (x < y) {
       min = x;
    }
    else {
       min = y;
    }

    You just say…

    min = (x < y) ? x : y;

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.

IF

    if (expr) {         ## basic if -- ( ) and { } required
      stmt;
      stmt;
    }

    if (expr) {         ## if + elsif + else
      stmt;
      stmt;
    }
    elsif (expr) {      ## note the strange spelling of "elsif"
      stmt;
      stmt;
    }
    else {
      stmt;
      stmt;
    }

    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

PHP

    The PHP else statement is the keyword else immediately followed by a single statement or a block of statements within curly braces {}. The statement of block of statements is run if the original if condition evaluates to false and skipped if the condition evaluates to true (because the statement or block of statements for the if will run instead).

    In the following example, the variable $lastsong is set to numeric zero if there are no GET items in the URL and is set to the lastsong item if it exists.

if(empty($_GET))
  $lastsong = 0;
else
  $lastsong = $_GET["lastsong"];

    This example comes from the simple music player of the music player programming example. Build your own free music player (no ads, no fees, no cost, all legal,, play almost any song).

JOVIAL

    The following material is from the unclassified Computer Programming Manual for the JOVIAL (J73) Language, RADC-TR-81-143, Final Technical Report of June 1981.

    1.1.6 Flow of Control

    For structured flow of control, JOVIAL has an if-statement, a
    case-statement, and a loop-statement with an optional exit-
    statement.  Examples of these statements follow.

    Here is an example of an if-statement:

         IF SPEED < LIMIT;
            FLAG = TRUE;
         ELSE
            BEGIN
            FLAG = FALSE;
            VIOLATION = VIOLATION+1;
            End

    If SPEED is less than LIMIT, this statement sets FLAG to TRUE and
    does nothing else.  If SPEED is not less than LIMIT, the
    statement sets FLAG to FALSE and increments VIOLATION.  The last
    four lines of the example are a compound statement; the BEGIN-END
    pair groups the assignments to FLAG and VIOLATION into a single
    compound statement controlled by the ELSE clause.

    The ELSE clause of an if-statement is optional; when it is
    omitted, no action is taken when the condition is false.
    Furthermore, if-statements can be nested, so complicated control
    structures can be built up.  When if-statements get large and
    complicated, however, you can sometimes use a case-statement to
    clear thigns up.

    Chapter 1 Introduction, page 10

    Commentary: This example interestingly highlights the problem of writing about software without testing it. The author missed the fact that travelling exactly at the speed limit is actually permissible. The test should have been for less than or equal to rather than just less than. This is why you always test the software you write, including test cases at each limit.


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, 2012 Milo

    Created: October 31, 2010

    Last Updated: October 14, 2012


return to table of contents
free downloadable college text book

previous page next page
previous page next page