开发者

About if statement in Java lang

i want to ask,can i insert an if sta开发者_开发技巧tement into an if statement i have a code like this:

       if(some codes && some codes || (if(some codes) && some codes))
    //then something else..

is it possible.i tried it but it gives an error.maybe its a syntax error maybe this is not possible if you answer i will learn :)


That's a syntax error.

If you have multiple conditions that you want to test, e.g. a, b, c and d, then combining them with logical operators in a single if statement should be enough, e.g.:

if (a && b || (c && d))
{
    ...
}


Depending on what you mean by

some codes and then something else

you might do fine with an if... else if

if(some codes && some codes )
  //  ...
else if((some codes) && some codes)
 // ...

If "some codes" means you're using the body of the if to execute statements I'd suggest refactoring to make the code more readable.


If you want a conditional value, you can use the ?: notation in boolean context as follows

if(cond1 && cond2 || cond3 ? cond4 : cond5){...}

meaning if cond3==true evaluates to if(cond1&&cond2||cond4), else if(cond1&&cond2||cond5)

example

int a=1;
int b=2;
int c=3;
int d=4;
if(a!=b && b!=c || a+b==c ? a+c==d : b+c==d){
    System.out.println("yup");
}else{
    System.out.println("nope");
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜