+ 2

if...else Statements

Can we put two conditions in if statement.

15th Sep 2018, 2:17 AM
kp kalia
kp kalia - avatar
6 Respuestas
+ 5
yes, if(x == 3 && y == 7){}
15th Sep 2018, 2:20 AM
J.G.
J.G. - avatar
+ 2
You can you have as many condition as you like not only one or two, also you have groups of conditions inside parenthesis as well like in the example bellow. you can use a single AND"&" and OR "|" or double AND"&&" and OR "||" operators. The difference is when you use a single simple the compiler will check all the conditions inside your if statement if you use the double ones (short cuts) it will stops once one of the conditions are meet which is more efficient and some times help you to avoid exception. int a = 1; int c = 10; if( (a != c && c < 20 && a > 2 ) || (c == 10) || (a==1 && c > 3) { //some nice code goes here. }
27th Jan 2020, 2:45 AM
Arturo Santos Pardo
Arturo Santos Pardo - avatar
+ 1
I will try Nested if Statements chapter. Hope to serve purpose.
15th Sep 2018, 2:20 AM
kp kalia
kp kalia - avatar
+ 1
&& - AND (True if both values are true) || - OR (True if either of the values are true) != - logical not (understandable ) You can put as many conditions as you want in if statement.
15th Sep 2018, 3:53 AM
onekpsc
+ 1
Absolutely! You just need to use conjunctions like and &&, or || . And conditions like equals ==, not equals !=, greater than >, less than < etc.
26th Sep 2018, 1:36 PM
Darren Moody
Darren Moody - avatar
0
It is common for languages (Java and Python are among them) to evaluate the first argument of a logical AND and finish evaluation of the statement if the first argument is false . This is because: ... When Java evaluates the expression d = b && c;, it first checks whether b is true more details you go to this websitehttps://www.programiz.com/java-programming/if-else-statement
15th Sep 2018, 10:32 AM
deepak sharma
deepak sharma - avatar