aspose file tools*
The moose likes Beginning Java and the fly likes please send me the answer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "please send me the answer" Watch "please send me the answer" New topic
Author

please send me the answer

praveen kumar tummala
Greenhorn

Joined: Mar 03, 2006
Posts: 8
class hai
{
public static void main(String[] args)
{
for(int i=1;i<3;i++)
{
System.out.println("Hello World!");
}
if(int i<5)
{
System.out.println("hello");
i++;
}
}
}


I want the answer with out intialising i globally or reinitialising would you do that
[ March 03, 2006: Message edited by: praveen kumar tummala ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If I understand your question, I think you want to declare int i within the scope of the main method. Then remove the initialization statement from the for "loop" (so that you don't have a separate int i within the loop), and change the "if" statement to a "while"...

Note: Please use Code Tags...

http://faq.javaranch.com/view?UseCodeTags
[ March 03, 2006: Message edited by: marc weber ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
praveen kumar tummala
Greenhorn

Joined: Mar 03, 2006
Posts: 8
int i = 1;


I dont want to initialise seperately,i would like to use "i" in for loop,by using that "i" variable in if, i want the answer,


thanks for ur response
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by praveen kumar tummala:
... I dont want to initialise seperately,i would like to use "i" in for loop,by using that "i" variable in if, i want the answer...

Then the answer is: You can't. A variable is limited to the scope in which it's declared. So if you declare int i in the "for" loop, then that variable's scope is limited to the "for" loop.
[ March 03, 2006: Message edited by: marc weber ]
praveen kumar tummala
Greenhorn

Joined: Mar 03, 2006
Posts: 8
thank you
 
jQuery in Action, 2nd edition
 
subject: please send me the answer
 
Similar Threads
confused
while() question
For Stament with continue
How to get a random number
Label (Continue)