public class ForLoop{ public static void main(String args[]){ for(int i=50;i<80;i++){ if(i%2==0){ System.out.println(i); } else{ break; } } } }
Welcome to the Java Programming Forums
The professional, friendly Java community. 21,500 members and growing!
The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
public class ForLoop{ public static void main(String args[]){ for(int i=50;i<80;i++){ if(i%2==0){ System.out.println(i); } else{ break; } } } }
Last edited by pbrockway2; June 25th, 2013 at 11:31 PM. Reason: code tags added
Hi nehaverma, wecome to the forums!
When you post code, use the code tags. Put [code] at the start of a section of code and [/code] at the end. That way the forum software will attempt to format your code properly for a web page.
You could clean up the indents a bit. Blocks of code are indented. And the closing } should go on a line of ots own.
public class ForLoop{ public static void main(String args[]){ for(int i=50;i<80;i++){ if(i%2==0){ System.out.println(i); } else{ break; } } } }
Part of the formatting may be due to using tabs. It's better, I think, to use spaces. They are more reliable.
Now the problem... You should tell us what it is
Does the code compile? If it doesn't compile and you can't understand the compiler'message, post the whole message.
If it compiles but does not do at runtime what you expect or intend, describe what you intended and what the program actually did. If there was an exception, again post the whole stack trace (the long message that an exception produces on the console.)
I cannot see any point to this code. For the first value of 50 it is even and enters the if branch and prints out the value. Then the value becomes 51 which is odd and enters the else branch and exits the loop and the program ends. Perhaps you can provide more details of what you are trying to achieve.
Improving the world one idiot at a time!
My guess is, he is trying to calculate how many even numbers there are between 50 and 80, but the thing is, you need to use different type of the variable i, because int can only be a full number, etc. 1,2,3,16...So when you modulus the number it will always be 0. You can try using "double" or "float".
Hope this helps,
G