Core Java Notes - Java 25 Version
PDF 2 : Operators, Control Statements and Loops
1. Operators
-> Operator is a symbol used to perform some operation.
Examples:
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulus
Types of Operators:
1) Arithmetic Operators
2) Relational Operators
3) Logical Operators
4) Assignment Operators
5) Conditional Operators
6) new Operator
7) Dot Operator
2. Arithmetic Operators
Arithmetic operators are used to perform mathematical calculations.
Program:
class ArithmeticDemo {
void main() {
var a = 10;
var b = 5;
[Link]("Addition : " + (a + b));
[Link]("Subtraction : " + (a - b));
[Link]("Multiplication : " + (a * b));
[Link]("Division : " + (a / b));
[Link]("Modulus : " + (a % b));
}
}
Output:
Addition : 15
Subtraction : 5
Multiplication : 50
Division : 2
Modulus : 0
3. Increment and Decrement Operators
Increment operator increases value by 1.
Decrement operator decreases value by 1.
Post Increment:
a++
Pre Increment:
++a
Program:
class IncrementDemo {
void main() {
var a = 10;
[Link](a++);
[Link](a);
[Link](++a);
}
}
Output:
10
11
12
4. Relational Operators
Relational operators are used to compare values.
Operators:
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
== Equal
!= Not Equal
Program:
class RelationalDemo {
void main() {
var a = 10;
var b = 20;
[Link](a > b);
[Link](a < b);
[Link](a == b);
}
}
Output:
false
true
false
5. Logical Operators
Logical operators are used to combine conditions.
&& AND
|| OR
! NOT
Program:
class LogicalDemo {
void main() {
var a = true;
var b = false;
[Link](a && b);
[Link](a || b);
[Link](!a);
}
}
Output:
false
true
false
6. Assignment Operators
Assignment operators are used to assign values.
Operators:
=
+=
-=
*=
/=
Program:
class AssignmentDemo {
void main() {
var a = 10;
a += 5;
[Link](a);
}
}
Output:
15
7. Conditional Operator
Conditional operator is also called ternary operator.
Syntax:
condition ? true_statement : false_statement
Program:
class TernaryDemo {
void main() {
var marks = 70;
var result = marks >= 35 ? "Pass" : "Fail";
[Link](result);
}
}
Output:
Pass
8. if Statement
if statement is used to execute statements based on condition.
Program:
class IfDemo {
void main() {
var num = 10;
if(num > 0) {
[Link]("Positive Number");
}
}
}
Output:
Positive Number
9. if-else Statement
if-else statement executes one block among two blocks.
Program:
class IfElseDemo {
void main() {
var num = -10;
if(num > 0) {
[Link]("Positive");
} else {
[Link]("Negative");
}
}
}
Output:
Negative
10. Nested if
Writing one if inside another if is called nested if.
Program:
class NestedIfDemo {
void main() {
var age = 22;
var citizen = true;
if(age >= 18) {
if(citizen) {
[Link]("Eligible");
}
}
}
}
Output:
Eligible
11. switch Statement
switch statement is used to execute one case among multiple cases.
Program:
class SwitchDemo {
void main() {
var day = 2;
switch(day) {
case 1 -> [Link]("Monday");
case 2 -> [Link]("Tuesday");
case 3 -> [Link]("Wednesday");
default -> [Link]("Invalid");
}
}
}
Output:
Tuesday
12. while Loop
while loop executes until condition becomes false.
Program:
class WhileDemo {
void main() {
var i = 1;
while(i <= 5) {
[Link](i);
i++;
}
}
}
13. do-while Loop
do-while loop executes at least one time.
Program:
class DoWhileDemo {
void main() {
var i = 1;
do {
[Link](i);
i++;
} while(i <= 5);
}
}
14. for Loop
for loop is used when number of iterations are known.
Program:
class ForDemo {
void main() {
for(var i = 1; i <= 5; i++) {
[Link](i);
}
}
}
15. Nested for Loop
Writing one loop inside another loop is called nested loop.
Program:
class Pattern {
void main() {
for(var i = 1; i <= 5; i++) {
for(var j = 1; j <= 5; j++) {
[Link]("* ");
}
[Link]();
}
}
}
Output:
* * * * *
* * * * *
* * * * *
* * * * *
* * * * *
16. for-each Loop
for-each loop is used to traverse array elements.
Program:
class ForEachDemo {
void main() {
int[] nums = {10,20,30,40};
for(var n : nums) {
[Link](n);
}
}
}
17. break Statement
break statement is used to stop loop execution.
Program:
class BreakDemo {
void main() {
for(var i = 1; i <= 10; i++) {
if(i == 5) {
break;
}
[Link](i);
}
}
}
18. continue Statement
continue statement is used to skip current iteration.
Program:
class ContinueDemo {
void main() {
for(var i = 1; i <= 5; i++) {
if(i == 3) {
continue;
}
[Link](i);
}
}
}
19. return Statement
return statement is used to stop method execution.
Program:
class ReturnDemo {
void main() {
[Link]("Start");
return;
}
}
20. Assignment Programs
1) Write a program to check even or odd number
2) Write a program to check prime number
3) Write multiplication table program
4) Write star pattern programs
5) Write palindrome number program
6) Write reverse number program
7) Write factorial program
8) Write Fibonacci series program
End of PDF - 2