0% found this document useful (0 votes)
7 views2 pages

Java Loops and Conditionals

The document explains Java loops and conditional evaluations, detailing a for loop that outputs odd numbers from 1 to 9 and a final value of 110. It also describes an if statement that modifies variables based on the value of n, and a do-while loop that prints negative numbers from -8 to -1. The final outputs for each loop and condition are summarized at the end.

Uploaded by

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

Java Loops and Conditionals

The document explains Java loops and conditional evaluations, detailing a for loop that outputs odd numbers from 1 to 9 and a final value of 110. It also describes an if statement that modifies variables based on the value of n, and a do-while loop that prints negative numbers from -8 to -1. The final outputs for each loop and condition are summarized at the end.

Uploaded by

guramritsinghop
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

Java Loop and Conditional Evaluations

1. Loop with for and System.out.println


Step Statement Value of y Output
1 Initialization y=1
2 Check y <= 10 true
3 Print y + " " 1 1
4 Increment y += 2 y=3
... Loop repeats: 3, 5, 7, 3579
9
5 y = 11 → condition
false, exit loop
6 After loop: y = 11 → 11*10 110 (on new line)
System.out.println("
\n" + y*10)

Final Output (for loop)


Line Output
1 13579
2 110

2. Conditional with if(n > 0)


Case Value of n Condition n x becomes y becomes Output
>0
1 1 true 1+1=2 1-1=0 x = 2, y = 0
2 0 false 1 1 x = 1, y = 1

3. Loop with do-while


Step Value of a Condition a < 0 Printed Value Next a
1 -8 true -8 -7
2 -7 true -7 -6
3 -6 true -6 -5
4 -5 true -5 -4
5 -4 true -4 -3
6 -3 true -3 -2
7 -2 true -2 -1
8 -1 true -1 0
9 0 false loop ends —
Final Output (do-while loop)
Line Output
1 to 8 -8 to -1 (each on new line)

You might also like