Some Programming doubts

Dark Star

ex-Mod
Please do help me out with these questions

1. How do we get the o/p as 2 when the initialization is 3

Code:
int main()

 {	int x = 3; 

		if( x == 2 );

 		 x = 0; 

		if( x == 3 )

		 x++; 

		else x += 2;

		cout<< x;

}

2. What does caret '^' represents in C++ ?

11 ^ 5 shows the output as 14 !?

3. Can we do assignment under conditional statements as in if else ? if(x=(a-b) ?

More to come.

Please do help.
 
Dark Star said:
Please do help me out with these questions

1. How do we get the o/p as 2 when the initialization is 3

Code:
int main()

 {	int x = 3; 

		if( x == 2 );

 		 x = 0; 

		if( x == 3 )

		 x++; 

		else x += 2;

		cout<< x;

}

Didn't get what you are actually asking :(

But the line
Code:
if( x == 2 );
is wrong, you should not put the ; after if statements, in that case if won't work as you want it to work. It'll execute as one statement. Means next statement will be always executed and not be treated as condition for if statement.

2. What does caret '^' represents in C++ ?

It's power of operator. 2^2 = 4, 2^3=8 like this.

11 ^ 5 shows the output as 14 !?

3. Can we do assignment under conditional statements as in if else ? if(x=(a-b) ?

Depends on your college, I suggest not to do that. Teacher finds way to deduct marks.

More to come.

Please do help.
 
{ int x = 3; ----------- x gets initialized to 3
if( x == 2 ); ------------- checks x == 2, but terminates right away as you've put ';', maybe a mistake?
x = 0; ------------ now x gets assigned 0, as its an independent statement, not under above 'if' statement as the previous statement is terminated with ';'
if( x == 3 ) ------- checks if x == 3, its not, its 0 now, so goes to 'else'
x++;
else x += 2; ------ increments x by 2, so 0+2 = 2

cout<< x; --------- outputs 2

}

My replies in bold. :) The ';' should be eliminated from first if and it should be fine.
 
Dark Star said:
2. What does caret '^' represents in C++ ?

Bitwise xor Operator.

11 ^ 5 shows the output as 14 !?

To understand this, you will have to first convert these two decimal numbers into binary number system because XOR is a binary operation like AND and OR.

links:

Addition and Subtraction

XOR -- from Wolfram MathWorld

Dark Star said:
3. Can we do assignment under conditional statements as in if else ? if(x=(a-b) ?

I tried it & it works. I tried it with & without if, result was same. apparently using conditional does not make a difference because of semicolon as it terminates right away. Without semicolon, it would been a syntax error.

Code:
#include<iostream>

using namespace std;

int main() {

        int a = 3,b=1, x=0;

        if(x=(a-b));

        cout << x;

}

Code:
gaurish@gaurish-desktop:/tmp$ ./a.out 

2

so it does not make sense to use if statement because its really not doing anything, only thing happening there is x = a-b. Makes sense?

krishnandu said:
^^Sure?? Then what's the power of?? ** ??
ever heard about Pointers? :p
 
Back
Top