Q:

How to use an element belonging to an if-else cycle outside the cycle?

I need to use the variable of an if-else loop as an argument of a method or lets say somewhere out of the if-else loop but in the same class.
     public static String myvar;

                if(x > 5) {
                    myvar = 0 ;
                    Double  y = Double.parseDouble(myvar);
                    System.out.println("The value is: "+ y); }
                else
                {

                  Double  y= Double.parseDouble(myvar);
                  System.out.println("The value is: "+ y);
                }

So whenever i use the variable y somewhere out of the loop, it can not be resolved to a variable..How to use this variable out of the if-else loop?

A:

Just declare variable outside if-else, right before.
Double  y = 0;
if ()
 y =X;
else
 y =Y;

A:

You have to declare and initialize the y variable out of the if-else loop.
Also your statement myvar = 0; will not compile as myvar is a String.
String myvar = "0";
Double y = 0.0;

if(x > 5) {
  y = Double.parseDouble(myvar);
  System.out.println("The value is: "+ y);
} else {
  y= Double.parseDouble(myvar);
  System.out.println("The value is: "+ y);
}

