Explain variable declaration and output in this example - c++

I'm confused at this example:
int x = 5;
if (x==5) cout << x; // output 5
if (x==6) cout << x;
if (x=6) cout << x; // output 6
x = 0;
if (x=0) cout << x;
x = 5;
if (x-5) cout << x;
if (x-6) cout << x; // output 5
I understand first if (x==5), but why does it output 6 at if (x=6) when x = 5, and why won't it output 0 in if(x=0)

if (x=6)
means not comparison, but assignment. You assign 6 to x and the return value of the expression is 6, which is not 0 so it gains true.
similar with if (x=0) The expression x=0 gains 0 so it means if(0)

The thing about computers is that they're extraordinarily literal. A missing semicolon, or an added character, can completely change a program's function. So you need to be just as careful as a computer when working wth programs.
As #juanchopanza alluded to, there is a difference between == and = - and you already know what it is.

Related

Learning Nested Loops C++ confused with example

Hello taking an online class on nested loops and this was provided as the example but I don't really know what is going on.
The following code example shows nesting for loops to output a chess or checkerboard representation using the characters X and O. Why do we need x and y variables to execute a certain amount of times. And what does alternate = !alternate; mean? About the x and y wouldn't it just do it 8 times total because its greater than the amount of times y supplies? what is the difference in purpose for the two for statements? Thank you.
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 4; y++)
{
if (alternate)
{
cout << "X ";
cout << "O ";
}
else
{
cout << "O ";
cout << "X ";
}
}
alternate = !alternate;
cout << endl;
}
The variable x used for the number of lines you want to print X-O pairs. Variable y used to specify the number of X-O pairs in 1 line. So for printing 8 lines of X-O pairs and in each line, 4 pairs of X-O, you should do just like that.
The operator '!' used for getting the opposite of a value (it's logical NOT) (for example, 1 to 0 or false to true). so alternate = !alternate; means that after every line of X-O pairs, it changes from true to false or vise versa.
So lines' first character (X or O) will change according to 'alternate' variable.

Why is this loop running invisible code? and not working in order

int ASCI1 = 1;
for (int i = 1; i < 8; i++)
{
cout << ASCI1 << endl << ASCI1++;
}
I get output :
2
13
24
35
46
57
68
7 (and no end line here for some reason).
The idea was to get
1
2
3
4
etc.
Completely stumped, please help!
The << operator is not defined as a sequence point in C++, so it's possible for ASCI1 and ASCI1++ to be evaluated in any order.
If you try to perform reads and writes on an object with unsequenced evaluation, then you have undefined behavior, hence the garbage output you are seeing.
Finally, you don't see a newline at the end 1) because you got lucky, and 2) because even if your loop executed as you had planned, you're still performing a write without following it up with another endl
The code is not invisible. You are outputting two numbers per each loop execution, one on first line, then a newline, then another on the next line.
Just do
int ASCI1 = 1;
for (int i = 1; i <= 4; i++){
cout << ASCI1++ << endl;
}
Not clear why you created that mess, just use:
int ASCI1 = 1;
for (int i = 0; i < 4; i++) {
cout << ASCI1++ << endl;
}
Note you should either start with 0 (as shown) or change condition to <= otherwise you will loop 3 times instead of 4.
Let's consider the very first iteration of for loop (when ASCI1 is 1):
cout << ASCI1 << endl << ASCI1++;
This statement is transformed as (hypothetically):
cout.print(ASCI1).printNewLine().print(ASCI1++);
As others commented, order of evaluation is not defined. In your case, ++ operation is being performed first, but the last print statement is to be passed old value of variable (i.e. 1), and not incremented value. The just incremented value is passed to first print. Hence it becomes:
cout.print(2).printNewLine().print(1);
The value of ASCI1 would be 1 after this statement. It will give output as:
2\n
1
And the next iteration would print 3 and 2 the same way I explained above
2
13
2\n
You've been lucky that you used just one ++ statement in one statement.
What if first print is passed 1 and second is passed 2, or 1 and 1. It is up to compiler, and the compilation phase. This is Undefined Behaviour (UB)

What is the difference between these two programs?

What are the differences between these two programs? For the first program I got an output of 9 and for the second program I got an output of 10.
#include <iostream>
using namespace std; // So we can see cout and endl
int main()
{
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
cout << x << endl;
x++; // Update x so the condition can be met eventually
}
cin.get();
}
#include <iostream>
using namespace std; // So we can see cout and endl
int main()
{
int x = 0; // Don't forget to declare variables
while ( x < 10 ) { // While x is less than 10
x++; // Update x so the condition can be met eventually
cout << x << endl;
}
cin.get();
}
In the first code block you are outputting x then adding to it so it will output 0-9. In the second code block you are adding 1 to x before you output it so it will give you outputs 1-10. It's based on where you put the x++ in relation to the output statement
Output of first is 0 1 2 3 4 5 6 7 8 9. Output of second is 1 2 3 4 5 6 7 8 9 10.
In the first example, you write the number out and then increase the variable, whereas in the second one you increase the value at first.
This is due to the order (obviously). In the first while loop, when x is 9, it prints it, increases it and doesn't pass the condition and does not enter the loop again.
In the 2nd case, when x is 9, it increases it to 10 and then prints it, and then leaves the loop. It's just common sense. The second loop therefore skips the number 0, and prints till 10.
First loop : 0,1,2,3,4,5,6,7,8,9
Second loop : 1,2,3,4,5,6,7,8,9,10
The first snippet prints the value of the variable and then increments it. This means the value of the variable after the last incrementation will not be printed.
The second snippet increments the variable and then prints it. This means that it will not print 0. The value it was initialized as.

Why the following two programs yield different outputs? (C++, bit operation, VS 2012)

Program 1:
int x = 4 ^ (4>>32);
cout << x << endl;
Output is 4
Program 2:
int x = 4;
int y = x ^ (x>>32);
cout << y << endl;
Output is 0
Both code-snippets induce undefined behavior if int has 32 bit or less. [expr.shift]/1:
The behavior is undefined if the right operand is negative, or greater
than or equal to the length in bits of the promoted left operand.
Hence an implementation is not in any way obliged to provide consistent results.

Proveit.com C++ test

Has anyone taken the C++ test on proveit.com ? I've done it a few times, and there are always questions that I get wrong, but I can't help but assume it's the site and not me. The only example I can think of off the top of my head (its been a while) is
What is the value of x after the operation?
int x = 5;
++x;
And it gives a few answers, one of which being 6. I don't see how it could be anything but. Just to be sure I would compile the code and still get the same answer, but the test would tell me I'm wrong. Wondering if anyone has any experience with this test/site.
Perhaps the test read what will this output:
int x = 5;
std::cout << x++;
And the question was:
What will be the output?
Because when you do this x will change value but will display 5 from cout because x is incremented after the original value of x is displayed. Otherwise, it should equal 6 in your case. (You said it had been a while...)
Strictly speaking this is implementation dependent, because there might an overflow here. But for all practical purpose, yes, the value of x should be 6 when the code is done execution.
You probably have an answer by now, but hopefully this will help someone else. Depending on how the question is stated you may have got the wrong answer:
int x = 5;
cout << x++ << endl;
cout << x << endl;
would display 5 and 6 because x is incremented AFTER it has been outputted. However,
int x = 5;
cout << ++x << endl;
cout << x << endl;
would display 6 and 6 because x is incremented BEFORE it has been outputted.