Using = and ==

Something I thought was very interesting while reading about the basics of booleans was the different uses of = and ==.

I wrote this program to provide some insight into what I have experienced.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// if statement

#include <iostream>

using namespace std;

int frenchfries = 45;
int main()
{
    if (true)
        cout << "This is always displayed.\n\n";
    
    if (false)
        cout << "This is never displayed. \n\n";
        
    
    if (frenchfries = 50)  
        cout << "You have 50 french fries.\n\n";
    if (frenchfries == 500)
        cout << "Wow, how did you get so many french fries?\n\n";

    
    
// using (frenchfries = 50) on line 17 instead of (frenchfries == 50)

    cout << "frenchfries is: " << frenchfries;


        cin.get();
        return 0;
        }


I thought it was interesting that even though I had written "(frenchfries = 50)" to be interpreted as a condition, C++ also redefined the number of french fries as well as still considering its boolean value.

To test this theory I wrote cout << "frenchfries is: " << frenchfries; at the end of the program and am glad to know that this is indeed correct.

Program result:
This is always displayed

You have 50 french fries.

frenchfries is:50



Score.
Last edited on
Nope. The = operator is used only for assignment, and it returns a reference to the variable. For example:
(var = 50) = 40;
This first assigns 50 to var, returns a reference to var, and then assigns 40 to that reference (var).
Here's another example:
var1 = var2 = 3;
This first assigns 3 to var2, which returns a reference to var2 and assigns the value of var2 to var1.

In C++ any non-zero value is true, so ofc 50 is true.
Last edited on
not a clue where 40 came in .... too much beer my brains hurtz
It was an example, 40 came from my head randomly ;)
I thought it was interesting that even though I had written "(frenchfries = 50)" to be interpreted as a condition


Here are some more C++ conditions:

1
2
3
4
5
6
7
if ( int x = 0)

if (int x = 5)

if (double f)

if ( rand())


Every statement can be interpreted as a condition; the condition is considered false if the statement evaluates to zero, and true otherwise.

Moschops, I thought you could not create variables inside expressions like that? At least it never worked for me :\
Sure you can, its the same as...
 
for(int i = 0;...)


Albeit it may not make sense.
Last edited on
Yes, unfortunatly C++ allows assignments within the if clause. Like stated before everything that's not 0 is interpreted as true else false. The assignment or what ever is executed.

This if (frenchfries = 50) is really this
1
2
frenchfries = 50;
if(frenchfries != 0)


Becaus of this vulnerability i use the constant value first, like: if (50 == frenchfries)
Awesome. Thank you for the replies, every bit helps.

I tried typing in just "2" for a condition and you are right it still displayed the message. Very cool to know about numbers being true.

I also tried entering in "-2" and it also worked. I am glad you all know what you are doing!

Best of luck to all! And may the french fries be with you.
Topic archived. No new replies allowed.