Retro video games delivered to your door every month!
Click above to get retro games delivered to your door ever month!
X-Hacker.org- Telix/SALT v3.15 & RS-232, Hayes - <b>if</b> http://www.X-Hacker.org [<<Previous Entry] [^^Up^^] [Next Entry>>] [Menu] [About The Guide]
  IF

  An 'if' statement is used when a statement or group of statements
  should be evaluated only if a condition is true. The format for an
  'if' statement is as follows:

       if (<expression>)
         <statement>

  <statement> is any statement as described above and below (that is,
  an expression, if, while, do...while, for, return, break, or con-
  tinue statement), and will only be executed if <expression> eval-
  uates to non-zero. By using curly braces around them, a whole group
  of statements may be conditionally evaluated. Some examples are:

       if (result == -1)
         prints("ERROR!");

       if (num_tries > maximum)
         return 0;

       if (month > 10 && day < 20)
        {
         clear();
         prints("In range.");
         return 1;
        }

  An alternate form of the if statement is:

       if (<expression>)
         <statement1>
       else
         <statement2>

  In this case, if <expression> evaluates to non-zero (TRUE),
  <statement1> is executed, otherwise <statement2> is executed. Again,
  multiple statements may be used instead by grouping them in curly
  braces. Some examples are:

       if (stat == -1)
         prints("Error status returned.");
       else
         prints("Function finished without problems.");

       if (level < 10)
        {
         alarm(1);
         prints("Warning!");
        }
       else
         prints("Everything's ok.");

  Since the statement to be executed conditionally can be of any type,
  that means that any number of if statement can be nested if needed.
  For example:

       if (num < 10)
         if (!error)
           if (read != 0)
             return 1;

  This also means that something like the following is legal:

       if (value == 10)
         do_this();
       else if (value == 100)
         do_that();
       else if (value == 1000)
         do_something_else();
       else
         do_whatever();

  What is really happening here is that each if statement is being
  nested after the else portion of the previous one. The above example
  could also be written as:

       if (value == 10)
         do_this();
       else
         if (value == 100)
           do_that();
         else
           if (value == 1000)
             do_something_else();
           else
             do_whatever();

  Any amount of nesting is theoretically legal, but the compiler does
  have a limit due to memory constraints.

  While you may write the code in any way which suits you, it is
  recommended that you use indenting, for clarity. Indenting your code
  at the proper places makes it a lot easier to read.

  A very common error to watch out for is accidentally placing a semi-
  colon after the parenthesis ending the expression. For example, if
  the following is run:

       if (num == 10);
         prints("Num is equal to 10);

  the string would always be printed, no matter what num was equal to.
  This is because the semicolon after the parenthesis ending the
  expression signifies the end of the statement. In the above case, it
  would just be a null (empty) statement.

See Also: statements expressions operators

Online resources provided by: http://www.X-Hacker.org --- NG 2 HTML conversion by Dave Pearson