L02 - Basic Programming Constructors
L02 - Basic Programming Constructors
• type name
• This is called Variable declaration, Which tell the
compiler the variables name and the type of data it
will hold.
Creating Variables (continued)
pageCount
loadFile
anyString
threeWordVariable
POP QUIZ
• Which of the following are valid variable
names?
1)$amount
2)6tally
3)my*Name
4)salary
5)_score
6)first Name
7)total#
Statements
• A statement is a command that causes
something to happen.
• All statements in Java are separated by
semicolons ;
• Example:
System.out.println(“Hello, World”);
• Example:
boolean monsterHungry = true;
boolean fileOpen = false;
Character Data Types
• Character is a data type that can be used to
store a single characters such as a letter,
number, punctuation mark, or other symbol.
• Example:
– char firstLetterOfName = 'e' ;
– char myQuestion = '?' ;
• Example
–5 + 6 // uses + operator
– 14 + 5 – 4 * (5 – 3) // uses +, -, *
operators
Operators
• There are 5 different groups of
operators:
oArithmetic operators
oAssignment operator
oIncrement/Decrement operators
oRelational operators
oConditional operators
Arithmetic Operators
• Java has 5 basic arithmetic operators
+ add
- subtract
* multiply
/ divide
% modulo (remainder)
• Order of operations (or precedence) when evaluating an
expression is the same as you learned in school (PEMDAS).
• Java Provides a Math class, which contains numerous
methods that are useful for performing complex
mathematical operations.
double result = Math.pow (4.0,2.0);
double result = Math.sqrt (9.0);
Integer Division
• When both operands of the division operator are
integer the result of operation will be an integer.
– E.g. double number;
number = 5 / 2; // number will be 2 not 2.5.
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = ++numOranges + numApples;
numFruit has value 16
numOranges has value 6
int numOranges = 5;
int numApples = 10;
int numFruit;
numFruit = numOranges++ + numApples;
numFruit has value 15
numOranges has value 6
Relational (Comparison) Operators
•Relational operators compare two values.
• Produces a boolean value (true or false)
depending on the relationship.
3) result = (x != x*y);
now result is assigned the value true because the product
of x and y (15) is not equal to x (3)
Conditional Operators
Symbol Name
&& AND
|| OR
! NOT
(x || y) evaluates to true
(true && x) evaluates to true
(x || y)
What happens if x is true?
• Similarly, Java will not evaluate the right-hand operator y
if the left-hand operator x is true, since the result is
already determined in this case to be true.
Conversion between Primitive Data Types
• Before a value can be stored in a variable, the values data
type must be compatible with the variables data type.
• Java automatically convert the lower ranked values
to higher ranked type , i.e. widening conversion.
➢ Double Higher rank
➢ Float
➢ Long
➢ Int
➢ short
Lower rank
➢ byte
– E.g. double x = 2.5;
Int y = 2;
x = y ; // Possible
y = x; // Error … this is Narrowing conversion
Cast Operator
• Java does not automatically convert any conversion that can
result in the loss of data or conversion to lower ranked.
• Cast operator lets you manually convert a value.
double num = 2.5;
int x = (int) num; // convert the value 2.5 to 2 & store it in x.
• Recall Integer Division
double x;
x = 5 / 2; // x = 2.0 integer division.
x = (double) 5 / 2; // x=2.5 cast one operand in to double.
x = (double) ( 5 / 2) ; // x = 2.0
• Mixed integer operations
e.g. short x, y=2, z=4;
x = y+z; // this will result an error.
x = (short) (y+z); //this will work because the arithmetic
operation using mixture of byte, short, & int will always be integer.
Control Structures
What are Control Structures?
• Control structures alter the flow of the
program, the sequence of statements
that are executed in a program.
if (expression) { yes
statement1;
} execute
statement
rest_of_program
execute
rest_of_program
If-Else Statement
if (expression) {
statement1;
}
else{
statement2;
}
next_statement;
if (grade == 'A')
System.out.println("You got an A.");
else if (grade == 'B')
System.out.println("You got a B.");
else if (grade == 'C')
System.out.println("You got a C.");
else
System.out.println("You got an F.");
Switch Statements
• The switch statement enables you to test several
cases generated by a given expression.
• For example:
switch (expression) {
case value1:
statement1;
case value2:
statement2;
default:
default_statement;
}
Every statement after the true case is executed
Do default action
Continue the
program
Break Statements in Switch Statements
• The break statement tells the computer to exit
the switch statement
• For example:
switch (expression) {
case value1:
statement1;
break;
case value2:
statement2;
break;
default:
default_statement;
break;
}
switch (expression){ expression y
case value1: equals Do value1 thing break
// Do value1 thing value1?
break;
case value2: n
// Do value2 thing
break;
expression y
... equals Do value2 thing break
default: value2?
// Do default action
break;
} n
// Continue the program
do default action
Continue the
break
program
• This is how it is accomplished with a switch:
switch (grade) {
case 'A':
System.out.println("You got an A.");
break;
case 'B':
System.out.println("You got a B.");
break;
case 'C':
System.out.println("You got a C.");
break;
default:
System.out.println("You got an F.");
}
int sum = 0;
int i = 1;
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
The do-while loop
do {
Statement;
} while (expression);
int sum = 0;
1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10 = 55
• Example :
int n = 0;
for(; n <= 10;) {
System.out.println(++n);
}
The for loop
Initialize count
The while loop
n n
Test condition Test condition
is true? is true?
y
y
Execute loop
statement Execute loop
statement(s)
Increment
Next statement count
New statement
The continue Statement
• The continue statement causes the program
to jump to the next iteration of the loop.
/*
* print out "5689"
*/
for(int m = 5; m < 10; m++) {
if(m == 7) {
continue;
}
System.out.print(m);
}
• Another continue example:
int sum = 0;
for(int i = 1; i <= 10; i++){
if(i % 3 == 0) {
continue;
}
sum += i;
}