1 % 0 and boolean values

This was our exam question. I said it will be 4th but IDEs say it's 2nd. How can
second "if" be true?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main ()
{
    int a = 0, b = 1, c = 2;
    
    if ((c % b) + a)
        cout << "1st";
    else
        if ((a % b) + c)
            cout << "2nd";
        else
            if ((b % a) + b)
            {
                cout << "3rd";
            }
            else
                cout << "4th";
    
    return 0;
}
(a%b)+c=(0%1)+2=0+2=2, giving a bool value of true. Hence what your IDEs give you.

I wouldn’t even like to guess what is supposed to happen if you try 1%0. I hope it would throw an exception.
Given 1 % 0: division by zero is not trapped or handled by C++. Unless you use an OS-specific method to catch it you will get a program crash. (Best to always check that your divisor is non-zero before trying.)
Last edited on
Note that it checks the conditions in order and stops as soon as it finds one that is true.

The first condition:
 
(c % b) + a
=>
 
(2 % 1) + 0
=>
 
0 + 0
=>
 
0
(zero is treated as false)

The second condition:
 
(a % b) + c
=>
 
(0 % 1) + 2
=>
 
0 + 2
=>
 
2
(non-zero values are treated as true)
Last edited on
The funny thing is nobody told me about non zero values are being true in boolean.
Could that "fact" have been somewhere in the course material?

https://en.cppreference.com/w/cpp/language/implicit_conversion writes:
Integral conversions
A prvalue of an integer type or of an unscoped enumeration type can be converted to any other integer type. If the conversion is listed under integral promotions, it is a promotion and not a conversion.

* If the source type is bool, the value false is converted to zero and the value true is converted to the value one of the destination type (note that if the destination type is int, this is an integer promotion, not an integer conversion).


Boolean conversions
A prvalue of integral, floating-point, unscoped enumeration, pointer, and pointer-to-member types can be converted to a prvalue of type bool.

The value zero (for integral, floating-point, and unscoped enumeration) and the null pointer and the null pointer-to-member values become false. All other values become true.

(Since C++11:) In the context of a direct-initialization, a bool object may be initialized from a prvalue of type std::nullptr_t, including nullptr. The resulting value is false. However, this is not considered to be an implicit conversion.

In other words:
1
2
3
4
bool no  =  0; // no == false
bool yes = 42; // yes == true
int  one = false; // one == 0
int  two = true;  // two == 1 
n % m where m > 0 and n > = 0 gives a result in the range 0 to m - 1.

Hence if m is 1 then the result will always be 0.
Last edited on
I didn't hear about that "fact" while having a udemy course of Frank Mitropoulos and while having school classes by former C++ programmer working for school.

Thanks everyone, I think I understand the statement. Thanks for your effort.
Topic archived. No new replies allowed.