Home Previous Next

CSC110AB::Lecture Note::Week 04
Assignments | Handouts | Resources | Email Thurman {Twitter::@compufoo Facebook::CSzero}
GDT::Bits:: Time  |  Weather  |  Populations  |  Special Dates

Overview Assignment(s): Code If.java {If2.java} | IfCode.java | Age.java | Age1.java

The if Statement and else Clause

The if is a selection control statement. It is a keyword of the language. It has the following syntax.

   if (EXPR) 
      statement';
   statement'';

   If  EXPR  evaluates to  true  then  statement'  is executed 
   followed by  statement''; otherwise,  statement'  is skipped 
   and the flow control of the program jumps to  statement''.

   if (netWorth > 1000000)
      System.out.println("you are rich (at least money wise)"); 
   netWorth = netWorth * 2;   //let us double your net worth

The EXPR of an if statement must evaluate to a boolean value.

   int i;

   //! if (i = 7)   // not legal; EXPR evaluates to an  int
      System.out.println("lucky number");

Typically, there is no semicolon after the if. If there is, then the body of the if is simply a NULL statement. In many cases, this is a defect with your program (i.e. it is done by accident, not design).

   if (netWorth < 0)
      ;  //do nothing...

If more than one statement needs to be executed when an if EXPR is true, then a compound statement . should be used. A compound statement is a collection of zero or more statements enclosed in braces {}.

Compound statements are terminated by the closing brace } -- not a semicolon.

   if (EXPR) {
      statement';
      statement'';
   }                //note that there is no semicolon
   statement''';

   If EXPR evaluates to true, then both statement' and statement''
   are executed followed by statement'''; otherwise, statement' and
   statement'' are skipped and the flow control of the program jumps
   to statement'''.

   isSuperStar = false;
   bonus = 0;

   ...

   if (battingAvg > 300) {
      isSuperStar = true;
      bonus = 10000;
   }
   number = 55;

Every if statement can have a corresponding else clause which is executed whenever the if is false.

   if (EXPR)
      statement';
   else
      statement'';
   statement''';

   If EXPR is  true, then statement' is executed, the else clause is
   skipped, and the flow control of the program jumps to statement''';
   otherwise, statement' is skipped, statement'' is executed and
   the flow control of the program jumps to statement'''.

   if (netWorth > 1000000)
      System.out.println("you are rich (with respect to money)");
   else
      System.out.println("you are rich in other ways");

An else clause cannot be used without a corresponding if statement. Example.

   int i = 10;

   ...

   else   //not allowed -- there is no if statement
      System.out.println("this is not legal");

The body of an if construct can be any type of statement. Examples.

   if (EXPR) EXPR;      //the body is an expression statement
   if (EXPR) System.exit(0);  //body is call to System.exit()
   if (EXPR) ;          //body is a null statement
   if (EXPR) { }        //body is an empty compound statement
   if (EXPR) if (EXPR)  //body is another if statement

An if statement containing another if is referred to as a nested if. There is no limit to the amount of nesting that can occur. Examples.

   if (EXPR)
      if (EXPR')
         statement';
   statement'';

   If EXPR is true, then EXPR' is evaluated; otherwise,
   flow control jumps to statement''.  If EXPR' is evaluated
   and it evaluates to true, then statement' is executed;
   otherwise, flow control jumps to statement''.

   if (age >= 100)
      if (gender == 'M')
         System.out.println("old man look at my life");

   if (age >= 100)
      if (gender == 'M')
         if (iq > 120)
            System.out.println("smart old man look at my life");
   System.out.println();

When using if statements, it is a good programming practice to use indentation to aid readability. The body of an if statement should be indented three or four spaces.

In all cases, each if can have a corresponding else clause.

   if (EXPR)
      if (EXPR')
         statement';
      else
         statement'';
   statement''';

   The top-most  if  does not have an  else  clause, whereas the
   inner-most  if  does.  If EXPR evaluates to  false, then
   flow control jumps to statement'''; otherwise, EXPR' is
   evaluated.  If it evaluates to true, then statement' is
   executed; otherwise, statement' is skipped and statement'' is
   evaluated.  Regardless of which statement is executed,
   flow control jumps to statement'''.

Caution is required to make sure that else clauses match up to the correct if construct. The following code snippet has a defect.

   if (age > 100)
      if (gender == 'M')
         System.out.println("you are an old man");
      else
         System.out.println("you are a young person");

   If age is greater than 100, then  if (gender == 'M')  will
   be executed.  If the gender is 'M', then the correct statement
   will print, but if the gender is not 'M', then the 
   "you are a young person" message will print.

   To correct this problem, a compound statement is needed to
   "tie" the  else  clause with the outer-most  if  statement.

   if (age > 100) {
      if (gender == 'M')
         System.out.println("you are an old man");
   } else
      System.out.println("you are a young person");
   
   Now if age > 100, then the body of the  if  is executed;
   otherwise, flow control jumps to the  else  clause.

The placement of {}'s when using compound statements is a style issue that is usually resolved by the formulation of coding standards. Primary rule: be consistent.

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)


Operators

An operator specifies how the operand or operands of an expression are manipulated.

Java has many of the same operators as does C and C++ with similar precedence and associvity.

Operators are usually represented using symbols (+ - * / %).

Operators and operands form the atomic unit of an expression.

Operators take either one operand (unary), two operands (binary) or three operands (ternary).

When multiple operators are used in an expression, then precedence and associativity is used to bind (or group) operands with operators.

Java evaluates expressions unconditionally left-to-right. [This is not true in C or C++.]

Arithmetic Operators

All of the arithmetic operators are binary operators (i.e. they take two operands).

   +   addition
   -   subtraction
   /   division
   *   multiply
   %   modulus (remainder)

The arithmetic operators work with integral and floating-point values except for the modulus (%) operator which works only on integral operands.

If both operands are integral data types, then the result is integral and an integer division occurs (i.e. any reminder is dropped (truncated) from the result). Examples.

    3 / 2  equals  1
    7 / 3  equals  2
   11 / 3  equals  3

An integer division by 0 causes an Exception. Example: The expression 2002 / 0 causes a program to abnormally terminate. Floating-point division by 0 results in the value infinity without an Exception.

If one operand is integral and other is a floating-point, then a floating-point division is executed. The result is the type of the floating-point operand.

If both operands are floating-point, then the type of the result is the type of the "largest" operand. Example.

   float f = 1.9;
   double d = 1.9;

   f / d   results in a  double
   d / f   results in a  double

The + addition operator can be applied to String objects. If only one operand is a String, then the other is converted to a String. The addition operator when applied to String objects performs a concatentation of the value of the left-hand operand with the value of the right-hand operand. The concatenation results in the creation of a third (new) String object that stores the result of the concatentation. [This is Java's only overloaded operator. Java does not support operator overloading.]

Relational Operators

Relational operators are binary operators that are used to compare the value of two expressions.

   ==  equality
   !=  not equal
   >   greater than
   >=  greater than or equal to
   <   less than
   <=  less than or equal to

Relational operators evaluate to boolean values (i.e. true or false).

   int a = 10;
   int b = 15;

   a == b        evaluates to false
   a != b        evaluates to true
   a > b         evaluates to false
   a < b         evaluates to true

If the operands are not the same type, then the value of the type smallest type is promoted to the larger type before the comparison is executed.

Logical Operators

The binary logical operators are use to test if two expressions, when combined, are either true or false. The unary logical operator is used to reverse the outcome of an expression that evaluates to a boolean values.

   &&  and
   ||  or
   !   not (unary)

The operands of logical operators must evaluate to type boolean values.

Java uses short-circuit evaluation (i.e. an EXPR is evaluated only until the truth or falsehood of the entire EXPR can be unambigously determined).

Bit-wise Operators
   ^  exclusive or
   &  and
   |  or
   >> right shift (sign extension)
   << left shift
   ~  one's complement

   Java also has a  >>>  right shift operator (0 extension).
Miscellaneous Operators
   ++  increment (both prefix and postfix)
   --  decrement (both prefix and postfix)
   ?:  conditional
   +   unary plus
   -   unary minus
   ()  typecast
   compound assignment  (e.g. +=, -=, /=, %=, *=, &=, |=, ^=, 
                              <<=, >>=, >>>=)
Java does not the following operators:
   sizeof()
   sequence (i.e. comma)

   Note:  the sequence operator is supported when used with the
          for()  repetition control statement.
The following Java operators will be covered later:
   new
   instanceof
   ()  /* method call */
   []  /* array */
   .   /* dot */
Operator precedence and associativity:

[]   .    ()(method call) left-to-right
!   ~   ++   --   +(unary)   -(unary)  ()(cast)   new right-to-left
*   /   % left-to-right
+   - left-to-right
<<   >>   >>> left-to-right
<   <=   >   >=   instanceof left-to-right
==   != left-to-right
& left-to-right
^ left-to-right
| left-to-right
&& left-to-right
|| left-to-right
?: left-to-right
= compound assignment operators right-to-left

In Java, the order in which EXPRs are evaluated is always a strict left-to-right. Recall, in C and C++, the order of EXPR evaluation is implementation-dependent. Having the order of EXPR evaluation be well-defined helps make Java portable. Here is an example:

   Given the following EXPR:  f() + g()

   In Java, you are guaranteed that  f()  will be called before  g().
   In C, on some systems  f()  may be called first, but on others it
   might be called after  g()  has been invoked.

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)


Truth Tables and De Morgan's Laws

The following is a "truth table" for the logical operators AND, OR and NOT.

  -------------------------------------------------------------------
  Logical operators:         AND   OR                            NOT 
  -------------------------------------------------------------------
  expr1       expr2          &&    ||                expr       !expr
  -------------------------------------------------------------------
  zero        zero           0     0                 zero         1
  nonzero     zero           0     1                 nonzero      0
  zero        nonzero        0     1 
  nonzero     nonzero        1     1 
  -------------------------------------------------------------------

In most popular programming environments, evaluation of a logical expression stops as soon as the truth is known. This is called short circuit evaluation. For example, given the following logical expression.

   expr1 || expr2

The C and Java languages guarantee that expr1 is evaluated first. If expr1 evaluates to TRUE, then the entire logical expression is TRUE and expr2 is not evaluated. (TRUE logically OR'd with anything is TRUE.)

The || and && logical operators are sequence points of the language. For example, given the following logical expression.

   f() && g()

Function f() is evaluated before function g(); however, the same is not necessarily true given the following arithmetic expression.

   f() + g()

Some implementations will evaluate the function f() first, while others will evaluate function g() first.

Logical operators (and the fact that they are sequence points and are subject to short circuit evaluation) are a critical tool when it comes to transaction processing.

   step1 && step2 && step3 && commit

   don't do step2 if step1 fails
   don't do step3 if step2 fails
   commit the transaction if step3 is successful
De Morgan's Laws

De Morgan's Laws are used to simplify logical expressions.

   !(expr1 || expr2)    is equivalent to     !expr1 && !expr2
   !(expr1 && expr2)    is equivalent to     !expr1 || !expr2

   examples... 

      useCYMK = !(red || green || blue)
      ...apply De Morgan's Law...
      useCYMK = !red && !blue && !green

      isdefective = !(clear && zeroerrors && posted)
      ...apply De Morgan's Law...
      isdefective = !clear || !zeroerrors || !posted

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)


The while Statement

The while statement supports iteration (i.e. it is a repetition control statement). Iteration is the process of repeating an operation zero or more times. Repetition control statements are called loops.

The while statement repeatedly executes a simple statement or block (i.e. compound statement) until a boolean EXPR becomes false. Each loop iteration is called a cycle.

   while (EXPR)    // typically, you don't want a semicolon 
      statement;

   EXPR is the "conditional" test used to determine whether the loop 
   should continue for another cycle, and  statement  is to be repeated.
   If multiple  statements  need to be executed, then use a compound 
   statement (or block).

   EXPR is evaluated only at the beginning of a loop cycle.

   Pseudo-code for a while loop:
      
      top-of-loop
         evaluate the EXPR
         if  EXPR  is "true"     // true is a keyword
            execute statement
            go to top-of-loop
         else
            go to end-of-loop  (i.e. terminate the loop)
      end-of-loop
      statement
      ...

   //Example:  loop until  n  becomes greater than 9999
   Random r = new Random();
   int n = -1;
   while (n <= 9999)
      n = r.nextInt();

   //Example:  print out the numbers 1 through 5
   int i = 1;
   while (i <= 5) {
      System.out.print(i + "  ");
      i = i + 1;
   }
	System.out.println();
Sentinel Values

One way to terminate (end or stop) a loop is when a particular data value is entered (e.g. the user enters a -1 or an end-of-file (EOF) is encountered), a special value that is used to end the loop is called a sentinel value.

Sentinel values should be defined as manifest (symbolic) constants.

   static final int EXIT_VALUE = -1;
   int n = EXIT_VALUE + 1;

   while (n != EXIT_VALUE) {
      System.out.print("Enter a number: "); System.out.flush();
      n = SomeConsoleClass.getInt();
   }

Note: By convention, manifest constants are named using all upper-case letters, digits and underscores.

The break statement has the effect of immediately terminating the innermost enclosing loop. Example.

   while (true) {
      prompt user and read in a value
      if (value is invalid) 
         break;
      process the data value
   }
Infinite Loops

When you use a loop in a program, it is important to make sure that the condition used to control the loop will eventually become false, so that the loop can end. A loop that never finishes is called an infinite loop.

To stop an infinite loop, a special command sequence must be typed on the keyboard to interrupt the program and forcibly cause it to quit. This command differs from machine to machine.

Sometimes infinite loops are used by design; therefore, existenance of an infinite loop does not necessarily imply a program defect. For example, a web server program uses an infinite loop.

   //Example:  infinite loops that do nothing 
   while (true) 
      ;

   int i = 99;
   while (i != 0) ;  //null statement executed

{TopOfPage} {Oracle.com::API Specification | Tutorial} {GDT::Java Resources} {Eclipse IDE} {Udacity} {udemy} {CodingGround (online IDE)


Home Previous Next