0

Can we put assignment operator in if condition?

Can we put assignment operator in if condition?

29th Jun 2018, 3:00 AM
zeeshan
zeeshan - avatar
14 Antworten
+ 8
Yes you can put assignment operator (=) inside if conditional statement(C/C++) and its boolean type will be always evaluated to true since it will generate side effect to variables inside in it. And if you use equality relational operator (==) then its boolean type will be evaluated to true or false depending upon comparisons.The == is thus used as an average for comparing to boolean type of if for either true or false and followed by if conditional actions. I will demonstrate 3 different examples in 3 different languages(C,C++,Java) .Each language will contain 1 different examples.So here's it follows:- 👉 Example 1:- x=y in C Language main() { int x=6,y=7; if (x = y) printf("Value of x is %d",x); } 💻 Output :- Value of x is 7 💡 [Brief Working:- You can see that inside if statement, first x is assigned with value of y (i.e) and = is left associative and so x=7 and so if statement returns the value of x its boolean will be always true and so it prints the ouput for x=7.] Continues...
29th Jun 2018, 10:21 AM
D-Key
D-Key - avatar
+ 8
📄 FINAL CONCLUSION:- The assignment operator (=) will work for C and C++ Language inside if but it wont work for Java language directly as already mentioned for java in previous answer.But will work indirectly as shown in example 3 ((x=y)==y). SO YOUR FINAL ANSWER IS YOU CAN USE ASSIGNMENT OPERATOR INSIDE IF IN C AND C++ LANGUAGE BUT YOU CAN'T USE IT IN JAVA LANGUAGE DIRECTLY BUT INDIRECTLY ITS POSSIBLE AS SHOWN IN EXAMPLE 3 ((x=y)==y).
29th Jun 2018, 11:37 AM
D-Key
D-Key - avatar
+ 7
📜 [Note that for x=y if y=0 then x=0 due to (=) side effects and the value would be evaluated to false here because evaluation of 0 in if ,returns boolean of False.] 👉 Example 2:- x=y==y in C++ Language using namespace std; int main() { int x=6,y=7; if(x=y==y) cout<<"The value of x is "<< x; } 💻 Output:- The value of x is 1 💡 [Brief Working:- Here inside if we have two operators, one is assignment operator (=) and other one is relational operator (==).So according to precedence of operators relational operator(==) has highest priority compared to assignment operator (=).So first y==y is evaluated and its boolean is true so y=1 and then the assignment operator = evaluates x=y which gives x=1.And if boolean to 1 is true and prints output for x=1 ].
29th Jun 2018, 10:42 AM
D-Key
D-Key - avatar
+ 7
Continuation... 👉 Example 3:- (x=y)==y in Java Language class IfStatement { public static void main(String args[]) { int x=6,y=7; if((x=y)==y) System.out.println("The value of x is " + x); } } 💻 Output:- The value of x is 7 💡[Brief Working :- Here inside if we have two operators, one is assignment operator (=) and other one is relational operator (==).But (x=y) is enclosed in bracket so bracket has highest precedence in multi- operators, therefore (x=y) is evaluated first and x=7 and then its compared with == operator to y value which is also 7.So if boolean type to true and hence you have print output for x=7.] 🔔 VERY VERY IMPORTANT NOTE:- Java Language will return error for if(x=y) and if (x=y==y) because java uses reference which is almost like pointers in C/C++ but reference doesnot allow unsafe conversions or they are type safe.So when you execute for if(x=y) or if (x=y==y),one would incompatible error.But it works in C/C++ language.]
29th Jun 2018, 11:24 AM
D-Key
D-Key - avatar
+ 6
Assignment operators can't be used inside java language directly though indirectly is possible as shown in example 3.The working of java in call references prohibits strictly. For example of declaration in java, if we declare , short x = 6; short y= 7; short z= x+y; // wrong because result z is going to be int and not short. And in C or C++ boolean is taken as interger therefore in the given example 2 (x=y==y) of the above in my answer you get the value of x=1 which is actually the return by true boolean of if for (y==y) and hence the value of x becomes 1.So booleans are taken as integers as well as bool true or false in C or C++. But due to type safety in Java it does not allow unsafe conversions and boolean are not taken as integers. SO FOR THE JAVA YOU CANT TAKE ASSIGNMENT OPERATOR DIRECTLY LIKE IN C OR C++ YOU CAN. BUT IN EXAMPLE 3 ((x=y)==y) FOR JAVA IN MY ABOVE ANSWER I HAVE TAKEN ASSGINMENT OPERATOR (=) INSIDE BRACKETS SO INDIRECTLY ITS POSSIBLE IN JAVA.
29th Jun 2018, 12:13 PM
D-Key
D-Key - avatar
+ 5
AK-47 Thank you!! I tried to explain the working in the knowledge to simple. hope it helps.
29th Jun 2018, 12:25 PM
D-Key
D-Key - avatar
+ 4
In C++17, you can do this: if(int x=5;x<6) { /*Do stuff*/ }
29th Jun 2018, 6:57 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 3
zeeshan show your example. if conditions should have a boolean arguement inside them. just adding say "if( x= y){}" would not work idealy.
29th Jun 2018, 3:08 AM
Manual
Manual - avatar
+ 3
Nikhil Dhama * [...] but an assignment statement will always give True [...] " According to the standard (n4292) section 5.18 page 131, " The assignment operator (=) and the compound assignment operators all group right-to-left. All require a modifiable lvalue as their left operand and return an lvalue reffering to left operand [...] " ~example~ int a = 0; int b = 10; // b is assigned to a first, then a's value (10) participates in the evaluation if ( a = b ) { // going to execute } On the other hand, if a = 10; b = 0; // then, a's value (0) is going through the process if ( a = b ) { // not going to execute }
29th Jun 2018, 7:14 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar
+ 1
although yes you can put it. but an assignment statement will always give True, so there isn't any importance of using if in that case. your if block will always execute, and why would someone use if-statement if it is always executing. as Manual mentioned , it wouldn't work idealy. if condition should have a boolean argument.
29th Jun 2018, 3:36 AM
Nikhil Dhama
Nikhil Dhama - avatar
+ 1
In Java you can, and I'm sure other languages can too. For example: String line; if ( (line = reader.nextLine() ) != null ) { // do stuff, line is now initialized } assuming you're reading from a file and have a reader object. This statement assigns the variable and then checks if it is null. The reason this works is that an assignment operation actually returns a value, namely the value assigned. So you can write things like return x = y + 1; simultaneously assigning x and returning it. https://code.sololearn.com/c4qW0VBpyFmF/?ref=app
29th Jun 2018, 6:13 AM
Dan Walker
Dan Walker - avatar
0
AK-47 ya my bad, i forgot to mention, it checks for the value of the lest operand of the assignment operator, if it's value is zer0 then it won't execute, otherwise will execute for each and every vaule.
29th Jun 2018, 10:47 AM
Nikhil Dhama
Nikhil Dhama - avatar
0
D-Key Good effort. I think your thoroughly explained comments would make the OP totally satisfied.
29th Jun 2018, 11:48 AM
To Seek Glory in Battle is Glorious
To Seek Glory in Battle is Glorious - avatar