LOOPING STATEMENTS
Java Programming
LOOPING STATEMENTS
Type Category Java Loops
Entry Controlled Condition checked before body for, while
Exit Controlled Body executes at least once do-while
WEATHER REPORT PROBLEM
Weather station in Chennai has recorded rainfall of first ten days in December 2024 using a rain gauge. Develop an algorithm and write a Java program to
count and print the number of days in which rainfall was low, medium and high.
• Light rain – Rate of rain varies between 0 to 2.5 millimeters
• Moderate rain – Rate of rain varies between 2.6 mm to 7.6 mm
• Heavy rain – Rate of rain is beyond 7.6 millimeters
FOR STATEMENT
for ( initialization ; condition ; increment/decrement )
Initialization Executed once before the loop begins
Condition Statement executes until condition becomes false
Increment/Decrement Executed at the end of each iteration
for ( initialization ; condition ; increment ) {
statement;
statement;
}
FOR STATEMENT (Display sum of first n natural numbers)
import [Link]; Output:
public class SumNatural { Enter the value of n: 5
public static void main(String[] args) { 15
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
int sum = 0;
for (int i = 1; i <= n; i++)
sum = sum + i;
[Link](sum);
}
FOR STATEMENT EXAMPLES
for (int i = 1; i <= n; i += 2) Output (n=5):
[Link](i);
1
for (int i = 5; i >= 1; i--) Output (n=5):
[Link](i);
5
Multiple variables using comma operator:
for (int i=0, j=10; i<j; i++, j--)
WHILE STATEMENT
while is a keyword
while ( condition ) { If the condition is true, the statements are executed.
Then the condition is evaluated again.
statement;
statement;
}
WHILE STATEMENT EXAMPLE (Display sum of first n natural numbers)
import [Link]; Output:
public class SumWhile { Enter the value of n: 5
public static void main(String[] args) { 15
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
int i = 1, sum = 0;
while (i <= n) {
sum = sum + i;
i++;
[Link](sum);
}
DO-WHILE STATEMENT
do is a keyword
do { Irrespective of condition, the statements are executed
first. Then the condition is evaluated.
statement;
statement; Note the semicolon at the end of while().
} while ( condition );
DO-WHILE STATEMENT EXAMPLE (Display sum of first n natural numbers)
import [Link]; Output:
public class SumDoWhile { Enter the value of n: 5
public static void main(String[] args) { sum: 15
Scanner sc = new Scanner([Link]);
[Link]("Enter the value of n: ");
int n = [Link]();
int i = 1, sum = 0;
do {
sum = sum + i;
i++;
} while (i <= n);
[Link]("sum: " + sum);
}
WHILE VS DO...WHILE STATEMENT
Basis for while
Declaration for(init; cond; iter){ } while(cond){ }
Format Init, condition, iteration at top Only init & condition at top
Use When number of iterations is known When number of iterations is not known
Condition omitted Infinite loop Compilation error in some forms
WEATHER REPORT PROBLEM – SOLUTION
import [Link]; Sample Output:
public class WeatherReport {
3.4 2.2 5.6 7.8
public static void main(String[] args) {
4.5 2.1 5.7 8.4
Scanner sc = new Scanner([Link]);
2.5 6.5
int lrCount=0, mrCount=0, hrCount=0, count=0;
double rain; Light rainfall days: 3
while (count < 10) {
Moderate rainfall days: 5
rain = [Link]();
Heavy rainfall days: 2
if (rain <= 2.5) lrCount++;
else if (rain <= 7.6) mrCount++;
else hrCount++;
count++;
[Link]("Light rainfall days: " + lrCount);
[Link]("Moderate rainfall days: " + mrCount);
[Link]("Heavy rainfall days: " + hrCount);
}
GCD OF TWO NUMBERS – ALGORITHM
Step 1: Read the numbers from the user.
Step 2: Let dividend = number1 and divisor = number2.
Step 3: Repeat steps 4–6 while remainder ≠ 0.
Step 4: remainder = number1 modulus number2.
Step 5: dividend = divisor.
Step 6: divisor = remainder.
Step 7: GCD = divisor.
Step 8: Print GCD.
GCD OF TWO NUMBERS – SOURCE CODE
import [Link]; Output:
public class GCD { 735
public static void main(String[] args) { 252
Scanner sc = new Scanner([Link]);
GCD: 21
int num1 = [Link](), num2 = [Link]();
int remainder;
while (num2 != 0) {
remainder = num1 % num2;
num1 = num2;
num2 = remainder;
[Link]("GCD: " + num1);
}
MCQ
What will be the output?
public class MCQ1 {
public static void main(String[] args) {
double k = 0.5;
for (k = 0.5; k < 3; k++)
[Link]("I love Java");
a) Error
b) I love Java – will be printed 6 times
c) I love Java – will be printed 3 times ← Ans: C
d) I love Java – will be printed 5 times
MCQ
What will be the output?
public class MCQ2 {
public static void main(String[] args) {
int i = 0;
for (;;) {
if (i == 10) break;
[Link](++i + " ");
a) Syntax error
b) 0 1 2 3 4 5 6 7 8 9 10
c) 1 2 3 4 5 6 7 8 9 10 ← Ans: C
d) 0 1 2 3 4 5 6 7 8 9
MCQ
What is the output of the below Java program?
public class MCQ3 {
public static void main(String[] args) {
short k = 1, j = 1;
while (k <= 4 || j <= 3) {
k = (short)(k + 2);
j += 1;
[Link](k + "," + j);
a) 5,4
b) 7,4 ← Ans: B
c) 5,6
d) 6,4