Win a copy of Penetration Testing Basics this week in the Security forum!

# Loops question

Rob M Jones
Greenhorn
Posts: 2
Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!

By the way the output is --,<

amro talaat
Greenhorn
Posts: 9
Rob M Jones wrote:Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!

By the way the output is --,<

try if else ???

Carey Brown
Bartender
Posts: 1734
22
Rob M Jones wrote:Hi everyone, I am currently studying java and came across this question on one of the practice exams. I have stepped through each iteration of the loops but am stumped as to why x is never increased to 4 when i is set to 1. Can anyone shed some light on this?
Thanks!

By the way the output is --,<

Try

Sheriff
Posts: 14691
16
Check this FAQ.

Ankit Dan
Ranch Hand
Posts: 47
why x is never increased to 4 when i is set to 1.

you can see that you have assigned the value of post increment x to x i.e x=x++
so x is assigned a value before it is incremented
you can try x = ++x
or simply x++ or ++x

hope it helps

Ankit

Campbell Ritchie
Sheriff
Posts: 50640
83
The idea of that question may be to find out whether you can see
i = i++;
and realise there is a trap for the unwary!