imthegman55
imthegman55

Reputation: 80

Passing variables through if statements without new method

So well I'm just here wondering if there's a way to pass a variable through a statement. Something like this:

if (a < b) {
    double g = 1
} else if (a > b) {
    double g = 0
}
if (g = 1) {
    System.out.print("true");
} else {
    System.out.print("false");
}

Mainly saying, I want to set a variable if a statement is true or not, go to the next section of code and print out "true" or "false" and I pretty much am just wondering if this is possible without creating a new method (and of course if there is code for it). Thank you.

Upvotes: 1

Views: 4029

Answers (5)

BellaMidntye
BellaMidntye

Reputation: 21

  double g; double a = 4.0; double b = 3.0;
    if(a < b){
        g = 1.0;
    System.out.print("true");
    }
    else if (a > b){
        g = 0.0;
        System.out.print("false");
    }
    // Why not write your code like the above example. It seems like the    
    //same operations are executed but with less lines of code.

Upvotes: 0

user_loser
user_loser

Reputation: 871

The if condition if (g=1) does not work with java. This would work with C though.

You should code if (g==1) to test if g is in fact equal to the int value 1.

Upvotes: 1

Makoto
Makoto

Reputation: 106450

You've got three problems.

  • a and b aren't defined. Define them before entering the if statement.
  • Define g outside of the if statement (a simple double g; will suffice), then set the values as part of your conditional logic. You do have to give it a default value if you intend to keep the else if there, since Java would complain about that not being defined.
  • g=1 isn't going to work the way you think it should; you probably mean g == 1.


With else if

int a, b; // assumed instantiated with values
double g = -1; // required since Java can't guarantee that the else-if will be hit
if (a<b) {
    g = 1;
} else if (a>b) {
    g = 0;
}

With else

int a, b; // assumed instantiated with values
double g; // instantiation not required since Java can guarantee the else case
if (a<b) {
    g = 1;
} else {
    g = 0;
}

Upvotes: 0

Christian Tapia
Christian Tapia

Reputation: 34156

You are almost there. You have to declare g outside the if statements, so you can access to it whithin the whole function. Read more about scopes, if you declare a variable inside a block {}, it will be accessible just inside it, so when you declared it into the if-else if blocks, you couldn't access to the variable outside.

Also to compare a primitive type (in this case double) you have to use == operator, because = is used for assignment.

double g;
if (a<b) {
    g = 1;
}
else if (a>b) {
    g = 0;
}
// What happen if 'a = b'?

if (g == 1) {
    System.out.print("true");
}
else {
    System.out.print("false");
}

Note: What value will take g if a == b? You may want to take care about that case too.

Upvotes: 4

Martin Golpashin
Martin Golpashin

Reputation: 1062

double g;
if (a<b) {
   g=1
}
else if (a>b) {
   g=0
}
if (g==1) {
    System.out.print("true");
}
else {
     System.out.print("false");
}

also make sure that you always use == instead of = in your if-statement

Upvotes: 1

Related Questions