Professional Documents
Culture Documents
002 - Basic Language Elements
002 - Basic Language Elements
Shakir Hussain
Identifiers
y A name in a program is called an
Identifier.
y Used to denote classes, methods,
variables and labels.
3
4
Literals
y Denotes a constant value, the value that a
literal represents remains unchanged in
the program.
y Example :
5
Primitive Types and Variables
6
Initialisation
y If no value is assigned to local variable prior to
use, then the compiler will give an error
y Java sets primitive variables to zero or false in
the case of a boolean variable[non-local
variable]
y All object references are initially set to null
y An array of anything is an object
◦ Set to null on declaration
7
Declarations
8
Assignment
y All Java assignments are right associative
9
Basic Mathematical Operators
10
Statements & Blocks
11
Flow of Control
y Java executes one statement after the other in
the order they are written
y Many Java statements are flow control
statements:
12
If – The Conditional Statement
y The if statement evaluates an expression and if that
evaluation is true then the specified action is taken
y Or, alternatively:
13
Relational Operators
== Equal (careful)
!= Not equal
>= Greater than or equal
<= Less than or equal
> Greater than
< Less than
14
If… else
y The if … else statement evaluates an expression and
performs one action if that evaluation is true or a
different action if it is false.
15
Nested if … else
16
else if
y Useful for choosing between alternatives:
17
A Warning…
WRONG! CORRECT!
18
The Statement
19
The loop
y Loop n times
y Nested for:
20
loops
21
loops
22
Break
23
Continue
y Can only be used with while, do or for.
y The continue statement causes the innermost loop to
start the next iteration immediately
24
25