CS 1073 Introductory Programming
|
Examples:
int i = 21; int j = 4; int k = i/j; // k is the quotient of 21 divided by 4 or 5 int m = i%j; // m is the remainder of 21 divided by 4 or 1
Note: These operators are usually used with ints.
Note: These operators are used either with ints, or with doubles. If you combine an int and a double, the int will first be converted to a double before doing the comparison.
Note: The or operator gives what is called "inclusive or", meaning "either the first or the second or both".
Note: This operator assumes the same type on the left side and right side. If you put an int on the right and a double on the left, that is permitted, because any int can be converted to a double having the same value. You are not permitted to put a double on the right and an int on the left unless you use a "cast" (the (int) below.
Examples:
int i = 3; double x; x = i; // OK x = 7.8; i = x; // not permitted i = (int) x; // OK, i is 7 now
Examples:
int x;
double rate;
char ch;
String name;
Examples:
x = 47;
rate = 92.33;
ch = 'A';
name = "Silver surfer";
Examples:
int x = 47;
double rate = 92.33;
char ch = 'A';
String name = "Silver surfer";
if ( ...) single statement;
if ( ...) {
statement;
statement;
...
}
if ( ...) {
statements;
}
else if ( ...) {
statements;
}
else if ( ...) {
statements;
}
else {
statements;
}
while ( ...)
single statement;
while ( ...) {
statement;
statement;
...
}