If Else statement understanding

Hi I would like to know why the following code will give an output of 78.65. I know that the condition has 1 = sign instead of 2, but why doesnt it do the rest of the other statements? Thanks in advance!

#include <iostream>

using namespace std;

int main()
{
char choice = 'A';
double num = 2.6, gain = 5.5, result;
if (choice = 'A')
result = num * gain * gain;
else
if (choice = 'B')
result = num/3*gain;
else
result = 0;
cout << result << endl;
return 0;
}
= is for assigning values. Use == for comparison.
@peter87 yes I understand that but why does it still do the first if statement?
 
if (choice = 'A')

First it will assign so that choice get the value 'A'. To do the same you could have written the following instead.

1
2
choice = 'A';
if (choice)

You don't have a boolean value (true or false) in the if condition so what it will do is that it will treat zero (for char that is the null character '\0') as false and everything else as true. 'A' is not zero so it will be treated as true.

1
2
choice = 'A';
if (choice != '\0')

1
2
choice = 'A';
if ('A' != '\0')

1
2
choice = 'A';
if (true)
@peter87 Thanks for clearing my doubts. However, I still do not understand "You dont have a boolean value in the if condition". Does this mean the condition if (choice) does not produce a boolean value?

Also, what do you mean by "treat zero (for char that is the null character '\0'?" Thanks in advance
Normally you put a boolean expression inside the parentheses of if statements.

A boolean expression is something that gives you true or false.
Example: x < 5 is a boolean expression. It returns true if x is less than 5, otherwise it returns false.

If the boolean expression of an if statement is true it will execute the statement that follows, otherwise it will execute the statement in the else-part (if there is one).

1
2
3
4
if (condition)
	runIfConditionIsTrue();
else
	runIfConditionIsFalse();


When you use an integer instead of a boolean expression it will handle an integer value of zero the same way as the boolean value false. All integer values other than zero is handled the same way as true.

1
2
3
4
if (integer)
	runIfIntegerIsNotZero();
else
	runIfIntegerIsZero();


In your code you are using a char, and a char is just an integer type, so it will handle it the same way as described above. Because choice is set to 'A' it is never zero so it will never execute what's in the else-part. It will only execute the result=num*gain*gain part of the if statement.

Note when I say zero here I'm talking about the integer value zero and not the character zero. The character '0' is represented by some integer value other than zero. To set choice equal to zero you could use the integer literal 0.

 
choice = 0;

But 0 has type int. When working with characters we often use '\0' which has type char.

 
choice = '\0';

The '\0' character is usually called a null character. Its main use is to mark the end of strings.
Last edited on
@peter87 thanks a lot for your help!
@peter87

I decided to try these 2 codes out
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    char x;
    x = 48;
    if (x)
        cout << "hi";
    else
        cout << "bye";
    return 0;
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

using namespace std;

int main()
{
    char x;
    x = '40';
    if (x)
        cout << "hi";
    else
        cout << "bye";
    return 0;
}

Why do they do the IF statement even if they both have a value of 0?
Last edited on
If you want to check for a particular ASCII character then you must specify which character you want.
 
    if (x == '0')


@Chervil

I was just testing out something. @Peter87 mentioned that if (0) and if ('\0') will do the ELSE statement if there is one. However, in the 2 cases I have shown, it does the IF statement instead. Even though, in both cases, x has a value of 0.
@peter87 I dont understand what you mean.

1
2
3
4
5
6
7
int main()
{
    char x;
    x = 48;
    cout << x;
    return 0;
}


This will give me a value of 0. Same for the 2nd case.
closed account (48T7M4Gy)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>

using namespace std;

int main()
{
   char choice = 'A';
   double num = 2.6, gain = 5.5, result = 0;
   
   if (choice == 'B')
      result = num * gain * gain;
   else if (choice == 'A')
      result = num/3*gain;
   else
      result = 0;
      
   cout << result << endl;
   
   return 0;
}
However, in the 2 cases I have shown, it does the IF statement instead. Even though, in both cases, x has a value of 0.

But it doesn't have a value of 0. The example code you posted clearly stated
x = 48;
48 is not zero, it is 48.

I see you edited the code to add
x = '40';
Unfortunately that is not valid code. x is a single character.
'40' is an attempt to squeeze a string of two characters into one. The compiler should reject it, or at least issue a warning, which you should resolve.
48 is not zero. The ASCII code for the character '0' is 48 and that is why it prints '0'. If you cast the char to an int before printing you will see that it really has the value 48.

 
cout << int(x);
Chervil wrote:
'40' is an attempt to squeeze a string of two characters into one. The compiler should reject it, or at least issue a warning, which you should resolve.

Using '40' was probably a mistake here but the code is valid. It's called a multicharacter literal. The type is int and the value is implementation defined. In C++14 this feature is conditionally-supported which means compilers don't have to support it.
Thanks Peter87 for the clarification.
Topic archived. No new replies allowed.