Write the output of the following program part:
int c= 10;
for(int j = 1; j <= 5; j++, c++)
{
if (c++> 2 && ++c <15)
System.out.println(c * 2);
}
Please explain how the output will come??
Answers
Required Answer:-
Given C∅de:
int c=10;
for(int j=1;j<=5;j++,c++) {
if(c++>2&&++c<15)
System.out.println(c*2);
}
To Find:
- Output (Java)
Solution:
Initially,
→ c = 10
→ j = 1
When j = 1:
→ j<=5 is true.
→ Loop will execute.
c++ > 2 && ++c < 15
= 10 > 2 && ++c < 15
= true && ++c < 15 (c becomes 11, post-increment)
= true && 12 < 15 (c becomes 12, pre-increment)
= true && true
= true
As the condition is true,
→ If block will execute.
→ c * 2 = 12 * 2 = 24
Output: 24
★ c and j are incremented.
→ c = 13, j = 2
When j = 2:
→ j<=5 is true.
→ Loop will execute.
→ c++ > 2 && ++c < 15
= 13 > 2 && ++c < 15 (c = 13, post-increment)
= true && ++c < 15 (c becomes 14)
= true && 15 < 15 (c becomes 15, pre-increment)
= true && false
= false
From now, condition is always false. So, no output will be shown when j = 3,4,5
Final Output: 24
Output:
- 24
•••♪