Skip to content

Latest commit

 

History

History
168 lines (113 loc) · 2.91 KB

File metadata and controls

168 lines (113 loc) · 2.91 KB

Valid Syntax

1. Empty

for ( int i = 0 ; i < 10 ; i++ ) { }

2. Complete for loop

for ( int i = 0 ; i < 10 ; i++ ) {
  int x , y ; // identifier list
  a=3 ; // variable declaration
  b++ ; // update
  System.out.println ( "hello" ) ; // print
  c+=3 ; // expression

  for ( int j = 0 ; j < 3 ; j++ ) { // nested for loop
    counter+=1 ;
  }
}

for ( int i = 0 ; i < 10 ; i++ ) { int x , y ; a=3 ; b++ ; System.out.println ( "hello" ) ; c+=3 ; for ( int j = 0 ; j < 3 ; j++ ) { counter+=1 ; } }


3. Complete nested for loop

for ( int i = 0 ; i < 10 ; i++ ) {
  for ( int j = 0 ; j < 3 ; j++ ) { // nested for loop 1
    counter+=1 ;
  }
  for ( int x = 0 ; x < 3 ; x++ ) { // nested for loop 2
    a++ ;
  }
}

for ( int i = 0 ; i < 10 ; i++ ) { for ( int j = 0 ; j < 3 ; j++ ) { counter+=1 ; } for ( int x = 0 ; x < 3 ; x++ ) { a++ ; } }


4. Expression as update (and w/o space)

for ( i=0 ; i<length ; i+=1 ) { counter++ ; }





Invalid Syntax

1. For loop unequal () {}

a. Separators are not balanced

for ( ( int i = 0 ; i < 3 ; i++ ) { }

b. Separators are balanced but in the wrong order

for ( int i = 0 ; i < 3 ; i++ ) } {

c. Sequence () {} is not followed

for { int i = 0 ; i < 3 ; i++ } ( )

2. Incorrect Control Statements

a. Empty control statement

for ( ) { System.out.println ( "hello" ) ; }

b. Less than 3 correct control statements

for ( variable ; i < 3 ; i++ ) { counter += 3 ; }

c. More than 3 correct control statements

for ( int i=0 ; i < 3 ; i++ ; i++ ) { counter += 3 ; }

3. Incorrect Statements

a. Variable Declaration

for ( int i = 0 ; i < 3 ; i++ ) { int a == 10 ; }

b. Print

for ( int i=0 ; i < 3 ; i++ ) { System.out.println ( 1abc ) ; }

c. Expression

for ( int i = 0 ; i < 3 ; i++ ) { i += int ; }

COMBINED: for ( int i = 0 ; i < 3 ; i++ ) { int a == 10 ; System.out.println ( 1abc ) ; i += int ; }


4. Statement not found

a. Statement entered is not one of the defined statements

for ( int i=0 ; i < 3 ; i++ ) { abc }

b. When the statement does not end with a semicolon

for ( int i=0 ; i < 3 ; i++ ) { i++ }





Errors the program cannot recognize

1. Same variable declaration

for ( int i = 0 ; i < 5 ; i++ ) {
  a = 3 ;
  int a = 4 ; // declares an existing var
}

for ( int i = 0 ; i < 5 ; i++ ) { a = 3 ; int a = 4 ; }


2. Variable declared twice locally in nested for

for ( int i = 0 ; i < 5 ; i++ ) {
  for ( int i = 0 ; i < var ; i++ ) { // also uses int i
    System.out.println ( “hello” ) ;
  }
  counter++ ;
}

for ( int i = 0 ; i < 5 ; i++ ) { for ( int i = 0 ; i < var ; i++ ) { System.out.println ( “hello” ) ; } counter++ ; }