if conditon

Pages: 12
Write your question here.

1
2
3
4
5
6
7
8
#include<iostream>
using namespace std;
int main()
{
int a=1,b=1;
if(a,b==2,2)
cout<<"True";
}

why if statement is executing even it is incorrect.
Comma operator, only the last part of the statement if(a,b==2,2) is chosen hence the condition becomes true.
so neither a nor b are 2 so why it is executed
Comma operator, only the last part of the statement if(a,b==2,2) is chosen hence the condition becomes true.

Don't make me repeat this. The 2 part will be used to evaluate the result of the if-statement and it is always true.
i am not getting it how to make such a condition false
Why not try :
1
2
3
4
5
6
7
8
9
10
#include<iostream>
using namespace std;
int main()
{
int a=1,b=1;
if(b==2)
cout<<"True";
else
cout<<"False";
}
There are three expressions:
(a)
(b==2)
(2)
Each of those is evaluated (but none of them cause any action) in sequence.
Finally the last expression (following the last comma) is used as the value of the entire expression.
Zero corresponds with false, any non-zero value is true.

See Comma operator near the bottom of this tutorial page:
http://www.cplusplus.com/doc/tutorial/operators/

Last edited on
i just wanna compare two dimensional array with nested loops i.e, i and j how to write it in if to compare.
What are the details of your assignment?
Last edited on
i am making a game called ludo. i dont know you are fimiliar with it or not. so i want the desired block on desired location. 2 dimentional array represents x and y axis of block and loops i represent x axis and j represent y axis.
Last edited on
i am making a game called ludo. i dont know you are fimiliar with it or not. so i want the desired block on desired location. 2 dimentional array x and y axis of block and loops i represent x axis and y represent y axis.

Have you finished the game yet? Is it a 2D game?
it is a simple game without graphics. just google ludo and you would see the shape of the board and where blocks should be placed
it is a simple game without graphics. just google ludo and you would see the shape of the board and where blocks should be placed

How much progress are you making? Between 0% and 100%?
i just started making different programs in cpp. it is my 3rd game by the way. 1st one was like pacman second was jumping from obstacles. just as the dino game on google which occurs when your net connection is interupted. i wrote it bcoz to let you know i just started simple coding this so you could figure out how could be this programs written.but it is tough for me
i just started making different programs in cpp. it is my 3rd game by the way. 1st one was like pacman second was jumping from obstacles. just as the dino game on google which occurs when your net connection is interupted. i wrote it bcoz to let you know i just started simple coding this so you could figure out how could be this programs written. But it is tough for me

In other words, you are making about less than 10% progress?
If you want to compare two different variables to two values in the same if-statement, then write each condition separately (a == 2) and (b==2).

Lastly, combine those two expressions with a logical AND operator &&. In other situations you may also need logical OR || or logical NOT !.

I'm assuming this is what you wanted?
 
    if ((a==2) && (b==2))

Some of those inner parentheses ( ) are not needed, but added for legibility.
what do you mean. i am just learning oop using c++. so i could make such types of things yet.
How much progress are you making your ludo game? Between 0% and 100%?
Both of the previous games were made a couple of days ago. so i am progressing i think
Have you made 20% game progress? Is this game tough, or are you stuck somewhere?
Pages: 12