Statements

  • Statement = a command issued to the computer to "do something"

      • Statement is the unit of execution in a programming language

  • In Java:

      • A statement must be contained inside some method

  • All (procedural) programming languages have these 3 types of statements:

      • Assignment
      • Conditional
      • Loop

Assignment statement

  • Syntax:

      variable = expression ; 

    Examples:

      x = 4;     // Store the value 4 in variable x
    
      x = x + 1; // Read the value in variable x, add 1 to it,       
                 // then store result in variable x

  • Notes:

      • Variables on the right hand side of the = operator are read (= accessed)

      • The variable on the left hand side of the = operator is written (= updated)

Selection statements

  • If-statement

        if ( condition )          if ( condition )
           one-statement;         {
                                     statement1;
    				 statement2;
    				 ...
    			      }

  • If-else-statement

        if ( condition )          if ( condition )
           one-statement;         {
        else                         statement1;
           one-statement;		 statement2;
    				 ...
    			      }
    			      else
    			      {
    			         statement3;
    				 statement4;
    				 ...
    			      }

Multi-way selection statements

  • Switch-statement

            Switch ( integer-expression )
            {
               case intVal1: statement1-1;
                             statement1-2;
    			 ...
    			 break;
    
               case intVal2: statement2-1;
                             statement2-2;
    			 ...
    			 break;
    
    	   ...
    
               default:      statementD-1;  // Optional clause !!!      
                             statementD-2;
    			 ...
    			 break;
            }     
    

Loop statements

  • While-loop

      while ( condition )       while ( condition )
         one-statement;         {
    			       statement1;
    			       statement2;
    			       ...
    			    }
    

  • For-loop

      for ( init ; term-cond ; incr )
      {
          statement1;
          statement2;
          ...
      }
    

  • do-while-loop is rarely used...

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Trace result:

      Statements                    i     sum
     ===================       =================
    
    
    
    
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Initialization:

      Statements                    i     sum
     ===================       =================
       sum = 1                             1
       i   = 0                      0     
    
    
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 1:

      Statements                    i     sum
     ===================       =================
                                    0      1
       i <= 5?   T                     
       sum = sum + i++                     1 + 0 = 1
                                    1
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 2:

      Statements                    i     sum
     ===================       =================
                                    1      1
       i <= 5?   T                     
       sum = sum + i++                     1 + 1 = 2
                                    2
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 3:

      Statements                    i     sum
     ===================       =================
                                    2      2
       i <= 5?   T                     
       sum = sum + i++                     2 + 2 = 4
                                    3
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 4:

      Statements                    i     sum
     ===================       =================
                                    3      4
       i <= 5?   T                     
       sum = sum + i++                     4 + 3 = 7
                                    4
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 5:

      Statements                    i     sum
     ===================       =================
                                    4      7
       i <= 5?   T                     
       sum = sum + i++                     7 + 4 = 11
                                    5
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 6:

      Statements                    i     sum
     ===================       =================
                                    5      11
       i <= 5?   T                     
       sum = sum + i++                    11 + 5 = 16
                                    6
    

Loop tracing

  • Trace the execution of the following loop:

        int sum = 1;
    
        for ( int i = 0; i <= 5; sum = sum + i++ );      
    
        System.out.print(sum);

  • Iteration 7:

      Statements                    i     sum
     ===================       =================
                                    6      16
       i <= 5?   F                     
                                                       
       STOP