Loading…
1. Variables and Operators in Java
A variable is used to store data, and operators perform operations on these variables.
Example: Arithmetic Operators
Explanation:
+
(Addition),-
(Subtraction),*
(Multiplication),/
(Division),%
(Modulo) are used to perform arithmetic calculations.
2. Control Flow Statements in Java
Control flow statements decide the execution path of the program based on conditions.
Example: If-Else Statement
Explanation:
if
checks a condition; iftrue
, it executes the block inside{ }
.- If
false
, it moves toelse
block.
Example: Switch Case
Explanation:
- The
switch
statement is used when we have multiple possible values for a variable. break
ensures the program exits after finding the correct case.
Example: Loops (for, while, do-while)
For Loop:
Explanation:
for
loop repeats execution fromi = 1
toi = 5
.
While Loop:
Explanation:
while
loop runs until the condition (i <= 5
) becomesfalse
.
Do-While Loop:
Explanation:
- The
do-while
loop ensures the code runs at least once, even if the condition isfalse
.
Summary:
✅ Variables store values.
✅ Operators perform calculations.
✅ If-else decides execution based on a condition.
✅ Switch handles multiple choices.
✅ Loops repeat tasks multiple times.
These basics are the foundation of Java programming! 🚀
Comments
Post a Comment