Decision Making in C++ (if , if..else, Nested if, if-else-if )

Hello,

I want to write a program that receives two numbers X and Y from the input. "without decision making" (decisions statements) determine how many of the following statements are true?

The question is vague. could you please help me?

Thanks
1
2
3
4
5

x>=0
x>y
x !=0
x!=y


Is this a good idea?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
using namespace std;

int main()
{

	int x, y;

	bool first = ( x >= 0);
	bool second = (x>= y);


	return 0;
}
Last edited on
 
cout<<(x>=0)+(x>y)+(x!=0)+(x!=y)<<" are true";
Thank you.
this may seem like a 'cute' problem but avoiding unnecessary conditions sometimes has a notable effect on performance. c++'s ability to use conditions as integers is very powerful because it can assist with that.
Hello Shervan360,

Well lets take a look at what you have:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

using namespace std;

int main()
{
    int x, y;  // <--- Uninitialized variables. They contain garbage at this point. Could be (-858993460).

    bool first = (x >= 0);  // <--- Or, i.e., "first = (-858993460 >= 0)". This would produce false.
    bool second = (x >= y);  // <--- Or, i.e., "second = (-858993460 >= -858993460)". This would also produce false.


    return 0;
}

The (-858993460) is what I usually get on my computer. You may get something different. Either way it is still garbage.

Give this a try:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>

using namespace std;

int main()
{
	int x{ 5 }, y{ 10 };

	bool first = ( x >= 0);
	bool second = (x>= y);

    std::cout << std::boolalpha << "first = " << first << '\n'<< "second = " << second << '\n';

	return 0;
}


Andy
Topic archived. No new replies allowed.