Professional Documents
Culture Documents
CSE 2001Y Lab Sheet 2 - Problems
CSE 2001Y Lab Sheet 2 - Problems
CSE 2001Y Lab Sheet 2 - Problems
Faculty of Engineering
System.out.println(sausages);
System.out.println(sausages++);
System.out.println(sausages--);
}
}
The increment operator ++ can be put after a variable (a++). When this is
done, it is a postfix operator. Same applies for the decrement operator --
(which gives a--).
A postfix expression (a++ or a--) produces a pure value that is the value
of the variable before it is incremented or decremented.
A prefix expression (++a or --a) produces a pure value that is the value
of the variable after it has been incremented or decremented.
In summary:
postfix expression → a++ or a--
++a means increment the value before using it.
Same for a--
1
prefix expression → ++a or --a
a++ means increment the value after using it.
Same for a—
Example 1:
//Increment.java
// Prefix increment and postfix increment operators.
c = 5; // assign 5 to c
System.out.println( c ); // prints 5
System.out.println( c++ ); // prints 5 then postincrements
System.out.println( c ); // prints 6
} // end main
Example 2:
int i = 3, j = 3; //short for int i = 3; int j = 4;//
System.out.println( "i++ produces " + i++);
System.out.println( "++j produces " + ++j);
The above code fragment produces the following output:
i++ produces 3
++j produces 4
Example 3:
int sum = 0;
int counter = 10;
2
sum = ++counter ;
2. Write a piece of code that provides output based on the age of an infant.
If the age is 1, then the output should be “One year old”, if age is 2, we
obtain “Two years old” and if age is 3, we get “Three years old”.
Otherwise, we should get the message “Not a baby!”. The age should be
input via the keyboard using Scanner.
4. The code below makes use of “nested ifs”, rewrite it using “if else-if”.
Discuss any drawbacks associated with the “if else-if” solution.
import java.util.*;
int mark;
mark = myscanner.nextInt();
if (mark >= 85)
3
{
System.out.println("High Distinction");
if (mark >= 95)
System.out.println("Consider applying for a
scholarship");
}
myscanner.close();
}
The Java compiler may find two types of errors: Syntax Errors and
Semantic Errors.
Syntax Errors
If you make a typing mistake while entering or editing your program, the
compiler may print out a syntax error. This happens when something you
typed doesn't conform to the rules of the Java language. For example, the
language specifies that each statement in Java must be terminated by a
semicolon. If in Lesson 2 we forgot the semicolon on the statement that prints
"Hello World!", we might see the following error message when compiling:
This error states that the problem was found on line 13 and that a ';' was
expected but not found.
An error message saying it couldn't resolve a symbol usually means that you
misspelled or misremembered a variable name, method name, or a keyword.
Semantic Errors
Even though your program may be syntactically correct, the compiler may
discover a semantic error (i.e., an error in usage). One example would be if
your program tried to use a variable that has never had an initial value set.
For example, if you had the following code snippet:
Cascading Errors
Cascading errors are not a distinct category of errors like syntax and semantic
errors. Nevertheless, this is a situation worthy of some discussion. Sometimes
one error in your program will result in an ambiguity that the compiler cannot
resolve. This may result in several other error messages on the same line or
later lines that may not represent true errors in the code.
For example, in the ASimpleSquare example file there is a for loop. If the
keyword for is accidentally mistyped as in: