aspose file tools*
The moose likes Beginning Java and the fly likes if and while 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 "if and while" Watch "if and while" New topic
Author

if and while

Sridhar Padala
Greenhorn

Joined: Aug 03, 2007
Posts: 20
Hi

The below is code working fine

public class Outer {

public static void main(String [] args) {
float x;
if (false) {
x = 3.0f;
System.out.println(x);
} } }

But when I use the same code with while, its giving compile time error

public class Outer {

public static void main(String [] args) {
float x;
while(false) {
x = 3.0f;
System.out.println(x);
} } }

For boolean value=true both are working fine. Please advice
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
Moving to JiG(B)

Eric
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
In general, code that can't possibly execute causes a compile-time error in Java. That's what you're seeing in your "while" example.

However, "if (false)" is a special case. Java allows this, even though the code inside the "if" cannot ever run. See this part of the Java Language Specification and search for "unreachable" for why.
[ August 16, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
camilo lopes
Ranch Hand

Joined: Aug 08, 2007
Posts: 202
any variable that you to declare inside of a method is local, then before uses-there must inicializar with a value. The code does not compel, why it is not insurance that instruction if or while executes, and you after this instruction it used a local variable that does not have value. It is not guaranteed that x = 3.0f.


Brazil - Sun Certified Java Programmer - SCJP 5
http://www.camilolopes.com/ About Java - Update every Week.
Guide SCJP - tips that you need know http://blog.camilolopes.com.br/livrosrevistaspalestras/
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by Peter Chase:
In general, code that can't possibly execute causes a compile-time error in Java. That's what you're seeing in your "while" example.

However, "if (false)" is a special case. Java allows this, even though the code inside the "if" cannot ever run.


Thats a good point. Thanks Peter.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Originally posted by camilo lopes:
any variable that you to declare inside of a method is local, then...


I think that the content of the "if" and of the "while" was not important to this discussion.

The question was why does "if (false)" compile and "while (false)" doesn't. The part of the JLS to which I referred in my previous answer explains that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: if and while
 
Similar Threads
Hope you can help me
Doubt
Primitive Data Value Equality
Floating Confusion
Math.round question