if conditon

Pages: 12
Nov 13, 2016 at 8:00am
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.
Nov 13, 2016 at 8:03am
Comma operator, only the last part of the statement if(a,b==2,2) is chosen hence the condition becomes true.
Nov 13, 2016 at 8:07am
so neither a nor b are 2 so why it is executed
Nov 13, 2016 at 8:09am
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.
Nov 13, 2016 at 8:11am
i am not getting it how to make such a condition false
Nov 13, 2016 at 8:13am
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";
}
Nov 13, 2016 at 8:18am
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 Nov 13, 2016 at 8:27am
Nov 13, 2016 at 8:59am
i just wanna compare two dimensional array with nested loops i.e, i and j how to write it in if to compare.
Nov 13, 2016 at 8:59am
What are the details of your assignment?
Last edited on Nov 13, 2016 at 9:00am
Nov 13, 2016 at 9:03am
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 Nov 13, 2016 at 9:04am
Nov 13, 2016 at 9:04am
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?
Nov 13, 2016 at 9:08am
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
Nov 13, 2016 at 9:10am
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%?
Nov 13, 2016 at 9:13am
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
Nov 13, 2016 at 9:14am
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?
Nov 13, 2016 at 9:17am
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.
Nov 13, 2016 at 9:19am
what do you mean. i am just learning oop using c++. so i could make such types of things yet.
Nov 13, 2016 at 9:20am
How much progress are you making your ludo game? Between 0% and 100%?
Nov 13, 2016 at 9:21am
Both of the previous games were made a couple of days ago. so i am progressing i think
Nov 13, 2016 at 9:22am
Have you made 20% game progress? Is this game tough, or are you stuck somewhere?
Pages: 12