0% found this document useful (0 votes)
8 views7 pages

If Else Loop Notes

If else loop notes Java

Uploaded by

vd707096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views7 pages

If Else Loop Notes

If else loop notes Java

Uploaded by

vd707096
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Java if and else Statements

1. if Statement

The if statement is used to test a condition. If the condition is true, the block of code inside the
if runs.

Syntax:

if (condition) {
// code to execute if condition is true
}

Example:

int age = 20;


if (age >= 18) {
[Link]("You are eligible to vote.");
}

2. if-else Statement

If the condition in the if statement is false, the code in the else block runs.

Syntax:

if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}

Example:
int age = 16;
if (age >= 18) {
[Link]("You can vote.");
} else {
[Link]("You cannot vote.");
}
3. if-else if-else Ladder

This is used when you want to test multiple conditions. As soon as one condition is true, the
corresponding block executes and the rest are skipped.

Syntax:

if (condition1) {
// code for condition1
} else if (condition2) {
// code for condition2
} else {
// code if none of the conditions are true
}

Example:

int marks = 72;


if (marks >= 90) {
[Link]("Grade A");
} else if (marks >= 75) {
[Link]("Grade B");
} else if (marks >= 60) {
[Link]("Grade C");
} else {
[Link]("Grade D");
}

4. Nested if Statements

An if statement inside another if block is called a nested if.

Syntax:

if (condition1) {
if (condition2) {
// code to execute if both conditions are true
}
}

int age = 19;


boolean hasID = true;
if (age >= 18) {
if (hasID) {
[Link]("Entry allowed.");
} else {
[Link]("ID is required.");
}
}

Important Notes

●​ The condition inside an if must return a boolean value (true or false).​

●​ Use {} braces to clearly define the code blocks, even for one-line statements.​

●​ You can combine conditions using logical operators:​

○​ && (AND)​

○​ || (OR)​

○​ ! (NOT)

Practice Questions
1.​ Write a Java program to check whether a number is positive, negative, or zero.​

2.​ Write a program that checks if a number is even or odd.​

3.​ Take age as input and print if the person is a child (age < 13), teenager (13–19), adult
(20–59), or senior (60 and above).​

4.​ Write a program to find the largest of two numbers.​

5.​ Write a program to find the largest of three numbers using if-else if-else.​

6.​ Check whether a character entered is a vowel or consonant.​

7.​ Check whether a year is a leap year or not.​

8.​ Write a program that checks if a student passes or fails based on marks entered (pass
mark is 40).​
9.​ Write a program to input a number and check if it is divisible by both 5 and 11.​

10.​Write a program to determine whether a given number is positive and divisible by 3.

Java Loops Notes


In Java, loops are used to execute a block of code repeatedly as long as a condition is true.

1. while Loop

The while loop checks the condition before executing the loop body. If the condition is false
initially, the loop body may not execute at all.

Syntax:

while (condition) {
// code to be executed
}

Example:

int i = 1;
while (i <= 5) {
[Link](i);
i++;
}

2. do-while Loop

The do-while loop executes the loop body at least once, even if the condition is false.

Syntax:

do {
// code to be executed
} while (condition);

int i = 1;
do {
[Link](i);
i++;
} while (i <= 5);
3. for Loop

The for loop is commonly used when the number of iterations is known.

Syntax:

for (initialization; condition; update) {


// code to be executed
}

Example:

for (int i = 1; i <= 5; i++) {


[Link](i);
}

4. break Statement

The break statement is used to exit a loop early.

Example:

for (int i = 1; i <= 10; i++) {


if (i == 5) {
break;
}
[Link](i);
}

5. continue Statement

The continue statement skips the current iteration and continues with the next one.

Example:

for (int i = 1; i <= 5; i++) {


if (i == 3) {
continue;
}
[Link](i);
}

Practice Questions on Loops


1.​ Print numbers from 1 to 10 using a for loop.​

2.​ Print even numbers from 1 to 20 using a while loop.​

3.​ Print the multiplication table of a number (e.g., 7) using a for loop.​

4.​ Find the sum of the first 10 natural numbers using a loop.​

5.​ Write a program to calculate the factorial of a number using a for loop.​

6.​ Print the reverse of a given number using a while loop.​

7.​ Write a program to count the digits in a number using a loop.​

8.​ Write a program to check if a number is a palindrome.​

9.​ Print Fibonacci series up to n terms using a loop.​

10.​Write a program to print a pattern like this using loops:​

*
**
***
****
*****
Signs and usage:
{ } : Block of code
( ) : Function
[ ] : Array
; : Line terminate

You might also like