(in slow motion)
int
, float
, double
, char
, boolean
String
To use variables in Java we need to declare them
TYPE
variable_name;
TYPE
variable_name = INITIAL_VALUE;
int
typeOPERATOR
variable_2 =
: Assignment+
, -
: Addition , Subtraction *
, /
: Multiplication , Division %
: Remainder of division OPERATOR
variable_1, variable_1OPERATOR
++
, --
: Increase variable value by 1, decrease variable value by 1 Assigning values to variables
Statements for which we can answer yes or no
Determine the truth value of a statement: true
or false
A variable that can hold truth values is called a boolean
variable
The simplest logical expressions
true
valuefalse
valueboolean
variable
The next simplest logical expressions, Comparisons
>
b , a <
b: Greater than, Less than >=
b , a <=
b: Greater or equal, Less than or equal ==
b , a!=
b: Equal, Different .equals
(b) , !
(a.equals
(b)): Equal, Different The value of a comparison is either true
or false
Comparisons
boolean
operators&&
: AND ||
: OR !
: NOT A tasty apple : is NOT rotten AND it is a McIntosh
A tasty apple : it is a McIntosh OR weights more than 0.3 lb
We can use the truth values of logical expressions (or operations on them) to conditionally execute code
Example: A computer program for putting apples in the appropriate shelf space
if
( the apple is rotten )
then
it goes in the garbage bin;
if
( the apple is not rotten )
then
it goes in one of the shelves;
else
then
it goes in the garbage bin;
if
( the apple is not rotten AND it is big )
then
it goes in one of the top shelves;
else if
( the apple is not rotten AND it is small )
then
it goes in one of the bottom shelves;
else
then
it goes in the garbage bin;
In Java, we can do this with a loop
while
( logical_expression )
do something;
if
( logical_expression )
do something;
What is the difference?
while
( logical_expression )
do something;
false
false
, the program never stops
An infinite loop
while
( there are apples left in the box )
do something;
while
( there are apples left in the box )
if
( the apple is not rotten )
then
it goes in one of the shelves;
else
then
it goes in the garbage bin;
while
loopfor each
loopfor
loop Before going into for
loops, let's move on from our apples example
Let's leave that for next class
A friend wants to come visit Montreal
Your friend wants to come when the weather is nice
We will use variables to manipulate information about the weather
/