The Java programming language

(in slow motion)

In the last class...



These situations are unpredictable

Thanks for helping out!

Variables in Java have types

Primitive types: int, float, double, char, boolean
Object types: String

Declaring variables

To use variables in Java we need to declare them

TYPE variable_name;

TYPE variable_name = INITIAL_VALUE;

Declaring variables

The int type

Operations with numerical variables

Binary: variable_1 OPERATOR variable_2

Unary:OPERATORvariable_1, variable_1OPERATOR

Operations with variables

Assigning values to variables


Operations with variables


Operations with variables

We can also do some operations with Strings

Important thing to remember

These statements must end with a ; in Java

Logical Expressions

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

Logical Expressions

The simplest logical expressions


Logical Expressions

The next simplest logical expressions, Comparisons

The value of a comparison is either true or false

Logical Expressions

Comparisons



Logical Expressions

We can build complex logical expressions using boolean operators

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

Conditional statements

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

Conditional Execution

if ( the apple is rotten )
then it goes in the garbage bin;

Alternative Execution

if ( the apple is not rotten )
then it goes in one of the shelves;

else
then it goes in the garbage bin;

Chained conditional statements

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;

Nested conditional statements

Visualizing conditional statements

A simple conditional statement

Visualizing conditional statements

Chained conditional statements

How do we repeat executions?

A possible visualization of the repeated execution?

In Java, we can do this with a loop

The while loop

while ( logical_expression )
do something;

if ( logical_expression )
do something;

What is the difference?

The while loop

while ( logical_expression )
do something;

The while loop


while ( there are apples left in the box )
do something;

The while loop


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;

Visualizing the while loop

There are other kinds of loops!

Before going into for loops, let's move on from our apples example

Let's leave that for next class

More on Variables

Imagine we want to write a program that uses a calendar...



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

Declaring variables for September


Declaring variables January


Declaring variables for the rest of the months


Resources

/