+ 3

Is it necessary to write “{}” in if statement, if here is only two lines in it?

//Example if(x<y){ y=88 y=9} //or if(x<y) y=88 y=9

9th Jan 2018, 1:58 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
10 Respostas
+ 6
Have you tried the comma operator? It works in some cases, like the one you have here. Helpful when you want to save some braced lines. Eg : #include <iostream> using namespace std; int main() { int x=1, y=2; if(x<y) y=88,y=9; cout<<y; }
9th Jan 2018, 6:01 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 6
in 1st code if( x<y ) { y = 88; y = 9; } if condition is true block of code executed and in 2nd code if( x<y ) y = 88; y = 9; if condition is true y = 88 executed otherwise rest of flow execution ie y = 9 executed !
9th Jan 2018, 2:06 PM
Amar Dahake
+ 3
@Kishuk Vasisht Thanks a lot!
10th Jan 2018, 4:02 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
+ 2
That’s what I needed
9th Jan 2018, 6:03 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
+ 2
But how much I can write in one line?
9th Jan 2018, 6:03 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
+ 2
@Sofia S. Tkachenko There isn't any limit to the use of the comma operator. It can be used simultaneously for 3, 4 or more lines. Its just that the operator fails when you use some type of functions like system(), exit(), etc..
10th Jan 2018, 3:59 PM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
Yes, you must if you're having multiple lines of code.
9th Jan 2018, 2:05 PM
James C
James C - avatar
+ 1
Thx
9th Jan 2018, 2:06 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
+ 1
I’ve already understood)
9th Jan 2018, 2:07 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar
+ 1
But thx
9th Jan 2018, 2:07 PM
Sofia S. Tkachenko
Sofia S. Tkachenko - avatar