Conditional Expressions and Statements

By Herbert J. Bernstein

© Copyright 1999 Herbert J. Bernstein

The basic structure of a conditional statement is:

if ( expression ) statement

Or

if ( expression ) statement_1 else statement_2

The expression is evaluated. If the resulting value is non-zero, the statement after the closing parenthesis is executed. If there is an else and if the expression evaluated to zero, the statement after the else is executed.

If if statements are nested, as in

if (expression_1) if ( expression_2 ) statement_1 else statement_2

then the else clause is associated with the closest prior if with which no else has been associated. However, a much better practice is to use explicitly nesting

if (expression_1) { if ( expression_2) statement_1 else statement_2 }

A conditional expression is of the form

expression_1 ? expression_2 : expression_3

which evaluates to the value of expression_2 if the value of expression_1 is non-zero and to the value of expression_3 is the value of expression_1 is zero.

In order to allow control over the order of evaluation of components of an expression for use in a conditional, the operators && (logical and) and || (logical or) work left to right through their operands, with execution for further operands skipped once the value of the expression has been determined.

The statement

switch ( expression ) {

case const_1: statement_1

break;

case const_2: statement_2

break;

case const_3: statement_3

break;

default: statement_default

}

has the same meaning as

if ( expression == const_1 ) {

statement_1

} else {

if ( expression == const_2 ) {

statement_2

} else {

if ( expression == const_3 ) {

statement_3

} else {

} else {

statement_default

}

}

}

}

 

To help understand the uses of conditional statements, let us consider a simple program which accepts strings of digits and which reports the equivalent integer numeric value:

 


import CSLib.*;

public class TestAtoi {

public static int myatoi(String xstring){

  int ipos = 0;        /* position within string */
  int idig;            /* current numeric digit  */
  int xvalue = 0;      /* tentative return value */
  char c;              /* next character from string */

  while (ipos < xstring.length()) {                               
    c = xstring.charAt(ipos++);
    switch (c) {
      case ('0'): idig = 0; break;
      case ('1'): idig = 1; break;
      case ('2'): idig = 2; break;
      case ('3'): idig = 3; break;
      case ('4'): idig = 4; break;
      case ('5'): idig = 5; break;
      case ('6'): idig = 6; break;
      case ('7'): idig = 7; break;
      case ('8'): idig = 8; break;
      case ('9'): idig = 9; break;
      default: idig = -1;
    }
    if (idig == -1) return xvalue;
    xvalue = xvalue*10 + idig;
  }
  return xvalue;
}

public static void main( String[] args ) {
  InputBox in;
  OutputBox out;
  String mystring;

  in = new InputBox ();
  out = new OutputBox();
  in.setPrompt ("Please type an integer: ");


  int num = 1;
  while (num != 0) {
    mystring = in.readString ();
    num = myatoi(mystring);
    out.println("You typed "+num);
    if (num == 0) out.println("Goodbye");
  }
  System.exit(0);
} 
}

 

This program takes a very naïve approach to the characters in the input string, making no assumptions about the collation sequence of digits and not making use of library routines to help in what it is doing. A real program would use library routines, but this simple application helps us to see the pwer of the switch statement in simplifying the presentation of complex conditionals.

The first version of this program lacks some useful features. It does not handle signs, and it only handles integers. Let us add handling of signs first. We add a case for the two possible sign symbols, '-' or '+', and test if we previously had a sign, making use of a conditional expression to distinguish the two signs, and adding a conditional expression on some of the returns to allow for three cases: no sign given, '-' given or '+' given.

 

import CSLib.*;

public class TestAtoi2 {

public static int myatoi(String xstring){

  int ipos = 0;        /* position within string */
  int isign = 0;       /* flag for sign:
                            0 not found, -1 negative, 1 positive */
  int digfound = 0;    /* flag for digits found  */
  int idig = 0;        /* current numeric digit  */
  int xvalue = 0;      /* tentative return value */
  char c;     /* next character from string */

  while (c = xstring[ipos++]) {
    switch (c) {
      case ('0'): idig = 0; digfound = 1; break;
      case ('1'): idig = 1; digfound = 1; break;
      case ('2'): idig = 2; digfound = 1; break;
      case ('3'): idig = 3; digfound = 1; break;
      case ('4'): idig = 4; digfound = 1; break;
      case ('5'): idig = 5; digfound = 1; break;
      case ('6'): idig = 6; digfound = 1; break;
      case ('7'): idig = 7; digfound = 1; break;
      case ('8'): idig = 8; digfound = 1; break;
      case ('9'): idig = 9; digfound = 1; break;
      case ('-'):
      case ('+'):
        if ((isign == 0) && (digfound==0)) {
          isign = (c == '-')?(-1):1;
        } else {
          return ((isign!=0)?isign:1)*xvalue;
        }
        break;
      default: idig = -1;
    }
    if (idig == -1) return ((isign!=0)?isign:1)*xvalue;
    if (digfound!=0) {
      xvalue = xvalue*10 + idig;
    }
  }
  return ((isign!=0)?isign:1)*xvalue;
}

public static void main( String[] args ) {
  InputBox in;
  OutputBox out;   
  String mystring; 
      
  in = new InputBox ();
  out = new OutputBox ();
  in.setPrompt ("Please type an integer: ");
 
  int num = 1;
  while (num != 0) {
    mystring = in.readString ();
    num = myatoi(mystring);
  }
  System.exit(0);
}
}

 

The last version of this program will accept a decimal point. We now process numbers as double instead of as int. Just as a second sign or a sign after digits terminate the number, so will multiple decimal points.

 

import CSLib.*;

public class TestAtod { 

public static double myatod(String xstring){

double myatod(const char xstring[]){
  int ipos = 0;        /* position within string */
  int isign = 0;       /* flag for sign:
                            0 not found, -1 negative, 1 positive */
  int digfound = 0;    /* flag for digits found  */
  int idig;            /* current numeric digit  */
  int decpos = 0;      /* position after the decimal point */
  double xvalue = 0.;  /* tentative return value */
  double xpow10 = 1.;  /* next (negative) power of ten */
  char c;              /* next character from string */

  while (c = xstring[ipos++]) {
    switch (c) {
      case ('0'): idig = 0; digfound = 1; break;
      case ('1'): idig = 1; digfound = 1; break;
      case ('2'): idig = 2; digfound = 1; break;
      case ('3'): idig = 3; digfound = 1; break;
      case ('4'): idig = 4; digfound = 1; break;
      case ('5'): idig = 5; digfound = 1; break;
      case ('6'): idig = 6; digfound = 1; break;
      case ('7'): idig = 7; digfound = 1; break;
      case ('8'): idig = 8; digfound = 1; break;
      case ('9'): idig = 9; digfound = 1; break;
      case ('.'):
        if (decpos==0) {
          idig = 0;
          digfound = -1;
          decpos++;
          xpow10 /= 10.;
        } else {
          return ((isign!=0)?isign:1)*xvalue;
        }
        break;
      case ('-'):
      case ('+'):
        if ((isign==0) && (digfound==0)) {
          isign = (c == '-')?(-1):1;
        } else {
          return ((isign==0)?isign:1)*xvalue;
        }
        break;
      default: idig = -1;
    }
    if (idig == -1) return ((isign==0)?isign:1)*xvalue;
    if (digfound > 0 ) {
      if (decpos==0) { 
      xvalue = xvalue*10. + (double)idig;
      } else {
        xvalue = xvalue + ((double)idig)*xpow10;
        decpos++;
        xpow10 /= 10.;
      }
    }
  }
  return ((isign!=0)?isign:1)*xvalue;
}

public static void main( String[] args ) {
  InputBox in;
  OutputBox out;
  String mystring;
         
  in = new InputBox ();
  out = new OutputBox ();
  in.setPrompt ("Please type an integer: ");
    
  double num = 1;
  while (num != 0.) { 
    mystring = in.readString ();
    num = myatod(mystring);
    out.println("You typed "+num);
    if (num == 0.) out.println("Goodbye");
  }
  System.exit(0);
}
}