Few noob c++ questions

1.
1
2
3
4
5
the question is when the "yes" will be printed
int department ;
cin>>department ;
if (department == 1 || 2)
cout<<"yes"<<endl ;

How I understand them, the "yes" will be printed if the value is 1 or 2, the answer is always but I don't know the logic, I know || = or

2.
1
2
3
4
5
The question is which of the following is equivalent
a*=--b+c ;
1)a=a*--b+c ;
2)a=a*(--b+c) ;
answer 1) and 2) are equal

I understand if it is 2) but how it is the same as 1) ? doesn't 1 mean (a*--b)+c

3.
1
2
3
4
5
6
7
8
The question, what is the output :
int a= 7 ; // line a
if (a=0)   //line b
{if (a==22)//line c
cout<<a;}
else
cout<<a;
cout<<88 ; //line g 

the answer is 088 : I don't understand the logic, if a = 0 // line b means if a is false right? but a is 7, not zero, but let say it is, how can a==22 //line c ?
I understand it like this, a is not false because 7 is true, then it will print out cout<<88 // line g, but why the answer is 088 not 88 ?

4.
1
2
3
4
int b, c ; int a=2 ;
b=++a+a++ //line b;
c=a++ ;
cout<<a<<b<<c ;

Output is 564, I understand why a = 5, and c = 4 but how's b = 6?
//line b ,b= 3 + 2 isn't it? because a++ is post increment or because ++a(=3) + a (must use new value = 3?) but if the expression b=++a + ++a replaced to //line b the answer is 8. how's that :s
5.
1
2
3
4
int a = 6, b= 5 ;
cout<<(a|b)<<" " ;
cout<<(a&b)<<" "<<endl;
Output 7 and 4

what | and & means ? I dont know how they operate

I admit it I am a newbie and sorry for the noob questions but I need answers to understand this things, tomorrow will have exam and these are my revisions
1. This doesn't do what you think
if(department==1 || 2) means if department == 1 IR if 2 == 2

You need something like
if(department == 1 || department == 2)

2. In this situation, the parentheses are irrelevant.

3. = is an assignment operator, not a comparison. Essentially, this is assigning a as 0. This will always be true. == is the comparison, this is why you're not outputting the first option. Does it output 088?

4. Think about precedence. Does ++ precede +?

5. | is the Boolean or, and & is the Boolean and.
http://www.cplusplus.com/doc/boolean/
6 in binary is 110 and 5 in binary is 101. Should be able to think about this a bit and figure it out now.
Any more questions just ask
1. This doesn't do what you think
if(department==1 || 2) means if department == 1 IR if 2 == 2

This explanation is not accurate. It means:

if( (department == 1) || 2)
due to order of operations. If we enter "3" we get:

if ( (3 == 1) || 2)

which translates to:

if ( false || 2 )

which becomes:

if (true)

which is true. Therefore the if statement is always true regardless of what you enter.

**************
The reason I am correcting you is that imagine if the if block instead read:

if(department == 1 || 0)

According to you, this would translate to:
if( (department == 1) || (0 == 0) )

which would always be true. But test if(department == 1 || 0) - it's not always true. The correct explanation is mine above.

edit: edited because I rushed this and did something wrong. Thanks Peter87!
Last edited on
@ReedTompkins, ah yea thank you. I had a misunderstanding of what was going on there. I never see the type of example in any code, so I wasn't too familiar with what was going on.
ReedTompkins wrote:
if( (department == 1) || 2)
due to order of operations. If we enter "3" we get:

if ( (3 == 1) || 2)

which translates to:

if ( 0 || 2 )

which becomes:

if (2)


First, there is some difference between 0 and false, namely the type. 3 == 1 gives false and not 0. If we ignore that, you still have an error. 0 || 2 does not generate 2. It generates true (which is 1).
Ack! I intentionally glossed over the difference between 0 and false (didn't want to make things too complicated), but you are absolutely correct about 0 || 2. I was rushing and did bitwise OR.

Please listen to Peter87. I've edited my post to avoid spreading misinformation.
Last edited on
Actually, I want to correct something.

if( (department == 1) || 2)

This doesn't translate to 2 == 2.

Since it is an or, it splits the two up

department == 1

Now it tests to see if this is true. Now it tests the next expression

2

Since 0 is false and everything else is true, this is automatically true. It doesn't test itself against itself, it tests if it is not 0 (false). I wanted to clear that up because I don't want people confused for the more complex stuff.

One person started into it when they were talking about bits. All data is stored in bits. When all those bits ar turned off (all zeros), then the data is considered false. Other then that, all data is considered true because it is not false.

There are 3 very important bit operators to know, ^, | and &. Two of these were covered in this topic, the third I don't think was mentioned yet but I stopped reading for now and will continue reading once I am done typing (bad logic there but I am not computer and that is where I make my mistakes).

These bits oparators may seem unimportant because you rarely use them, but the are because behind the scenes you need them for everything you do. The way to check if an integer is false is simple.

if(1 = 2)

what this does is that it check the bits in 1 and see if they match the bits in 2. Now both numbers have only 1 bit active but the 1 bit is active in the first position depending on what system your computer uses to store bits (left to right or right to left). The 2 bit uses the second bit so when they compare with an XOR function they will result in a 1. XOR is different from OR because if both bits are active, then OR returns a true but XOR only returns true if one of them is active and not the other. If XOR of 2 numbers returns 0, they are identical. If it returns anything else (true) they are different numbers.

I use bit operators in alot of things I do because managing bits becomes easy with practice and is more efficient in regards to speed and memory size. When you can cut memory size and increase speed at the same time, it is always a win win. I used bits in card programs (determine the poker rank for a poker hand) or for multiple true or false situations (bool uses a 8 bit integer (__int8 or char) which is a waste of 7 bits. I could throw 8 true false in one char, saving myself the ram and processor. This is VERY handy in writing servers since speed and efficiency are critical when you have thousands of people logging in at a time.

I think I got way off course here though.
ResidentBiscuit (707) Mar 10, 2012 at 1:04am
1. This doesn't do what you think
if(department==1 || 2) means if department == 1 IR if 2 == 2

You need something like
if(department == 1 || department == 2)

2. In this situation, the parentheses are irrelevant.

3. = is an assignment operator, not a comparison. Essentially, this is assigning a as 0. This will always be true. == is the comparison, this is why you're not outputting the first option. Does it output 088?

4. Think about precedence. Does ++ precede +?

5. | is the Boolean or, and & is the Boolean and.
http://www.cplusplus.com/doc/boolean/
6 in binary is 110 and 5 in binary is 101. Should be able to think about this a bit and figure it out now.
Any more questions just ask

1. solved
2.I still don't understand, which parentheses are you referring too? they are telling that
1)a=a*--b+c ;
2)a=a*(--b+c) ;
are equal. okay let say it is 1, it multiply first before added to +c, while in 2) (--b+c) then multiply by a
3.yah it prints out 088 instead of 88, you can try copy to your compiler, isn't it supposed to be 88
4.I didnt get your meaning :s sorry I am too newbie maybe -___- , so you are telling we must + before ++ ? so b=++a +a++ , should be 5? (according) I think it is 6 if, b= ++a(=3) + a++ (and by this time a stored is 3) but if b=++a + ++a why the output is 8? ( shouldnt be 7?) b=++a(=3) + ++a (where a is 4 this time?)
5.Solved
Last edited on
2.I still don't understand, which parentheses are you referring too? they are telling that
1)a=a*--b+c ;
2)a=a*(--b+c) ;
are equal.


1 and 2 are definitely not equivalent.

3.yah it prints out 088 instead of 88, you can try copy to your compiler, isn't it supposed to be 88
1
2
3
4
5
6
7
8
9
10
The question, what is the output :
int a= 7 ; // line a
if (a=0)   //line b
{
       if (a==22)//line c
              cout<<a;
}
else
       cout<<a;
cout<<88 ; //line g  


Reformatted, it should be obvious why it prints out 088, and why proper indentation is a good thing.

if ( a=0 ) sets a to 0. Because 0 evaluates to false, it skips to the else branch and prints out a, which we just set to 0. Then it prints out 88.

For question 4, line 2 results in undefined behavior. Either operand could be evaluated first. Because it's undefined behavior, b could be anything and the compiler would still be conforming.




Question 4
I don't think question 4 is undefined behavior.

See this link for operator precidence:

http://en.cppreference.com/w/cpp/language/operator_precedence

First we do the suffix++
The ++ prefix and the addition have the same precedence so they are read right to left. Since the addition is on the right, we do that first:

1
2
3
4
5
6
7
8
	b = ++ a + a ++ ; // Original expression
	b = ++ a +(2 ++); // Do the suffix ++ first
	b = ++ a + 2    ; // returns 2, then increments a
	b = ++(a + 2)	; // now do addition
	b = ++(3 + 2)   ; // Remember a has been incremented, it is now 3
	b = ++(  5  )   ; // Do the addition
	b =(++   5  )   ; // Do the increment and then return a;
	b =      6      ; // a is now 4 
Last edited on
1
2
First we do the suffix++
The ++ prefix and the addition have the same precedence so they are read right to left


There is no reason to do the postfix++ operator first. Both prefix and postfix ++ operators have higher precedence than addition. The unary+ is not addition. Might want to take another gander at the link you supplied.

b = (++a) + (a++) ;

is how the code parses. The compiler is not constrained to evaluate the parenthesized expressions in a particular order. Note that pre and postfix precedence only come into play (vs each other) when applied to the same expression (++a++). Of course, that would also be undefined behavior.
Last edited on
Topic archived. No new replies allowed.