Professional Documents
Culture Documents
OBJECT ORIENTED
PROGRAMMING
Data Types short 2 bytes Stores whole numbers from -32,768 to 32,767
Non-Primitive Primitive
Non-primitive types starts with an uppercase A primitive type starts with a lowercase letter.
letter.
Non-primitive types have all the same size. The size of a primitive type depends on the
data type.
Variables are containers for storing data values.
www.sli.do
Click - Slido
Code to Join :
#18659
To create a variable, you must specify the type and
assign it a value.
Syntax:
(Creating) Example:
Variables
String name = "John";
• Arithmetic operators
• Assignment operators
• Comparison operators
• Logical operators
• Bitwise operators
Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations.
== Equal to x == y
!= Not equal x != y
&& Logical and Returns true if both statements are x < 5 && x < 10
true
& AND - Sets each bit to 1 if both 5&1 0101 & 0001 1
bits are 1 0001
<< Zero-fill left shift - Shift left by 9 << 1 1001 << 1 0010 2
Bitwise operators are pushing zeroes in from the right
used to perform and letting the leftmost bits fall
binary logic with the off
bits of an integer or
>> Signed right shift - Shift right by 9 >> 1 1001 >> 1 1100 12
long integer. pushing copies of the leftmost
bit in from the left and letting
the rightmost bits fall off
>>> Zero-fill right shift - Shift right by 9 >>> 1 1001 >>> 0100 4
pushing zeroes in from the left 1
and letting the rightmost bits fall
off
Java supports the usual logical conditions from
mathematics:
Less than: a < b
Equal to a == b
Syntax :
if (condition) {
// block of code to be executed if the condition is true
}
Example :
if (20 > 18)
{
System.out.println("20 is greater than 18");
}
If
Syntax :
if (condition)
Else - {
Statement
// block of code to be executed if the
condition is true
}
else
{
// block of code to be executed if the
condition is false
}
If - Else
Example :
If - Else -
int time = 20;
Statement
if (time < 18)
{
System.out.println("Good day.");}
else
{
System.out.println("Good evening.");
}
Syntax :
if (condition1)
{
Else - If // block of code to be executed if condition1 is
Statement true
}
else if (condition2)
{
// block of code to be executed if the
condition1 is false and condition2 is true
}
else
{
// block of code to be executed if the
condition1 is false and condition2 is false}
Else - If
Example :
int time = 22;
if (time < 10)
Else - If {
Statement System.out.println("Good morning.");
}
else if (time < 20)
{
System.out.println("Good day.");
}
else
{
System.out.println("Good evening.");}
Syntax :
if(condition)
{ //code to be executed
Nested if
if(condition)
statement {
//code to be executed
}
}
Nested if
Example :
int age=20;
int weight=80;
if(age>=18)
{
Nested if if(weight>50)
{
System.out.println("You are eligible to donate blood");
}
}
Switch
Switch statement is used when you have multiple possibilities for the if statement.
Syntax : Example :
switch(expression) int day = 4;
{ case x:
switch (day)
// code block
break;
{ case 1:
case y: System.out.println("Monday");
// code block break;
break;
case 2:
default:
System.out.println("Tuesday");
// code block}
break;}
Switch
While Loop
The while loop loops through a block of code as long as
a specified condition is true:
Syntax : Example :
while (condition) int i = 0;
{ while (i < 5)
// code block to be executed {
} System.out.println(i);
i++;
}
While Loop
Do - While Loop
Loop will execute the code block once,
before checking if the condition is true, then it will
repeat the loop as long as the condition is true.
Syntax : Example :
do int i = 0;
{ do
// code block to be executed {
} System.out.println(i);
while (condition); i++;
}
while (i < 5);
Do - While
Loop
For Loop
When you know exactly how many times you want to loop through a
block of code, use the for loop instead of a while loop:
Syntax : Example :
Introduction The Java for loop is a The Java while loop is The Java do while loop is a
control flow statement a control flow control flow statement that
that iterates a part statement that executes a part of the
of the programs executes a part of programs at least once and
multiple times. the programs the further execution
repeatedly on the depends upon the given
basis of given boolean condition.
boolean condition.
When to use If the number of iteration If the number of If the number of iteration
is fixed, it is iteration is not fixed, is not fixed and you must
recommended to use for it is recommended to have to execute the loop at
loop. use while loop. least once, it is
recommended to use the
do-while loop.
Break
The break statement can also be used to jump out / exit of a loop.
Example :
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
break;
}
System.out.println(i);
}
Break
Continue
The continue statement breaks one iteration (in the loop), if a specified
condition occurs, and continues with the next iteration in the loop.
Example :
for (int i = 0; i < 10; i++)
{
if (i == 4)
{
continue;
}
System.out.println(i);
}
THANK YOU