Krishiv Patel Beginner Journal
Krishiv Patel Beginner Journal
If statements let us make decisions in our code. They check if something is true, and if it is, they do
if (x == 3) {
System.out.println("X is 3");
Loops help us run the same code multiple times without having to write it again and again. Some
types of loops are do, while, and for loops. Here's a simple example of a do-while loop:
do {
System.out.println("Hello");
} while (x == 2);
In this loop, the message "Hello" will print at least once, even if x is not 2.
3. Constants:
Constants are values that don't change while the program runs. We use the word 'final' to create a
4. Loop Examples:
- Do-While Loop:
do {
// Code here
} while (condition);
- While Loop:
int x = 3;
while (x < 2) {
x += 2;
System.out.println(x);
- For Loop:
A do-while loop always runs the code inside it at least once before checking if the condition is true.
A while loop checks the condition first, so it might not run the code at all if the condition is false from
the start.