Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within OCPJP
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Ron McLeod
Jeanne Boyarsky
Sheriffs:
Paul Clapham
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
Programmer Certification (OCPJP)
Expression in for loop
Vamshi Vrukodar
Greenhorn
Posts: 6
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
in the following code,
int j=0;
for(int i=0;j>3;i++)
j=j+1;
System.out.println(j);
the output is 0 where I expected it to be 3.
Please can you explain the above behaviour.
Regards
Vamshi
Vamshi Vrukodar
Greenhorn
Posts: 6
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I am sorry. excuse me for this post. I misunderstood the question.
Thank you
Vamshi
Jose Botella
Ranch Hand
Posts: 2120
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Welcome to the Ranch Vamshi.
You can delete this post if you want via the editing icon (the one with the pencil on it)
SCJP2. Please Indent your code using UBB Code
Jack Lau
Ranch Hand
Posts: 168
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
If you replace j>3 to i>3, the output is still 0. That means when the condition not match, the statement j=j+1; in the for loop will not be executed.
int j=0; for(int i=0;i>3;i++) { j=j+1; } System.out.println(j); // 0
Jack
Alton Hernandez
Ranch Hand
Posts: 443
posted 22 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
What your code is saying is that 'while j>3 then increment j by 1'. Of course this will not happen because j started out as 0.
You need to change your code from
for(int i=0;j > 3;i++)
to
for(int i=0;j < 3;i++)
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Flow control
Please Explain
Int array to 3D array
increment operator
why does 'break' traverse one more time
More...