do
/while
LoopHomework 5 is due this Sunday at 11:59 PM.
You will find the assignment here.
If you have a problem or a question, make a post on the Class Discussion Area.
Description | Operators |
---|---|
Unary operators | ++ -- |
Multiplicative operators | * / % |
Additive operators | + - |
Relational operators | < > <= >= |
Equality operators | == != |
Method | What it does |
---|---|
Math.abs() | Absolute value |
Math.max() | The greater of two numbers |
Math.min() | The lesses of two numbers |
Math.sqrt() | Square root |
Math.cbrt() | Cube root |
Math.random() | A random number between 0.0 and 1.0 |
Math.log() | Natural logarithm |
Math.log10() | Base 10 logarithm |
Math.cos() | Cosine |
Math.sin() | Sine |
Math.tan() | Tangent |
Constant | What it is |
---|---|
Math.PI | π - The ratio of the circumference of a circle to its diameter |
Math.E | e - The base of the natural logarithms |
(DATA_TYPE) EXPRESSION
double
but I needed an int
I would write
(int) num
&&
(and) operator returns true if both its
operands are true
5 > 4 && 4 > 2 : true
&&
returns false
5 > 4 && 4 < 2 : false
5 < 4 && 4 < 2 : false
||
(or) operator returns true if either of its operands are true
5 > 4 || 4 < 2 : true
5 < 4 || 4 < 2: false
!
(not) operator takes only one operand and reverses its value
! 5 > 4: false
! 5 < 4: true
&&
(and)
p | q | p && q |
---|---|---|
true | true | true |
true | false | false |
false | true | false |
false | false | false |
||
(or)
p | q | p || q |
---|---|---|
true | true | true |
true | false | true |
false | true | true |
false | false | false |
!
(not)
p | ! p |
---|---|
true | false |
false | true |
a > b && c > d
&&
to return truea > b || c > dand the first expression is true, the result of the whole expression will be true
a + b > c and c * d < athere are three different kind of operators
! | Logical NOT |
* / % | Multiplication, division and remainder |
+ - | Addition and subtraction |
> < >= <= | Relational Operators |
== != | Equality Operators |
&& | Logical AND |
|| | Logical OR |
for
loopfor
loop is a counted loopwhile
loop has the following format
while (BOOLEAN_EXPRESSION){
STATEMENT;
STATEMENT;
...
}
java
interpreter comes to a while
loop it evaluates the BOOLEAN_EXPRESSION
public class DiceGame {
public static void main (String[] args){
int die_1 = rand_int(7, 1);
int die_2 = rand_int(7, 1);
int total = die_1 + die_2;
System.out.println(die_1 + " " + die_2 + " -> " + total);
while (total != 7){
die_1 = rand_int(7, 1);
die_2 = rand_int(7, 1);
total = die_1 + die_2;
System.out.println(die_1 + " " + die_2 + " -> " + total);
}
System.out.println("Game over");
}
public static int rand_int(int max, int min){
double value = Math.random() * (max - min) + min;
return (int) value;
}
}
$ java DiceGame 5 1 -> 6 4 5 -> 9 2 3 -> 5 5 5 -> 10 3 5 -> 8 5 2 -> 7 Game over
do
/while
Loopint die_1 = rand_int(7, 1); int die_2 = rand_int(7, 1); int total = die_1 + die_2; ... while (total != 7){ die_1 = rand_int(7, 1); die_2 = rand_int(7, 1); total = die_1 + die_2;
do
/while
loop
do {
STATEMENT;
STATEMENT;
...
}
while (BOOLEAN_EXPRESSION);
do
/while
and the while
loop is when the boolean expression is
evaluated
while
loop the code block is only executed
if the boolean expression is true
do
/while
loop the loop
body is always executed at least once
do
/while
loop
public class DiceGame2 { public static void main (String[] args){ int die_1, die_2, total; do { die_1 = rand_int(7, 1); die_2 = rand_int(7, 1); total = die_1 + die_2; System.out.println(die_1 + " " + die_2 + " -> " + total); } while (total != 7); System.out.println("Game over"); } public static int rand_int(int max, int min){ double value = Math.random() * (max - min) + min; return (int) value; } }
$ java DiceGame2 3 3 -> 6 2 2 -> 4 2 5 -> 7 Game over