Seminar 3
Division by Zero in Java
Generally, will give no error and the result Infinity
.
If you divide 0.0 by 0.0 then you will get the result NaN
.
Code Blocks
Indentation is ignored by the compiler. You should use {}
to properly define your blocks.
switch
statements
This can we used as a case select statement.
switch (x) {
case 0:
case 1: // code to run
break; // this will have it stop after the block.
default: //default code to run
break;
}
The break
reserved word ceases execution of the switch
block. The default
case is not required.
Inherits from C/C++
int a = 0;
int b = 0;
int c = 12 > 10 ? ++a : b++; // the ? can be used in place of an if
If the condition before ?
is true then the first condition is executed. If false then the condition after :
is executed.
To increment numbers ++
can be used. There can be two cases:
a++
- This evaluates to the old value of
a
but incrementsa
.
- This evaluates to the old value of
++a
- This evaluates to the increment of
a
and incrementsa
.
- This evaluates to the increment of
A Random Walk Example
Generating a Random Value of 1/-1
Math.random()
gives a decimal number in the range $0\leq x<1$.
You can use Math.floor(x)
to concatenate to integer.
Math.floor(Math.random())
will give 0 or 1 randomly.
Assessment 1
This assessment is based around the Caesar Cipher.