Two Boolean checks in one if statement

VitzzViperzz

Well-known member
Joined
Jan 16, 2017
Messages
75
Location
United Kingdom
Programming Experience
1-3
Hello,

So I am making an application that checks to see if the person buying fuel will be paying in cash or buying premium fuel for discounts.

I have got as far as collecting all of the information, but I just cannot check to see if both booleans are equal to true.

Here is the code:

C#:
Console.WriteLine("Welcome to Mo's gas station.");
            Console.WriteLine("Will you be paying with cash?");
            bool payingCash = false;
            string userInput1 = Console.ReadLine().ToLower();
            if (userInput1 == "yes" || userInput1 == "y")
            {
                payingCash = true;
                Console.WriteLine("Awesome, you're paying in cash!");
            }
            else


            Console.WriteLine("Will you be buying premium gas?");
            bool premiumGas = false;
            string userInput2 = Console.ReadLine().ToLower();
            if (userInput2 == "yes" || userInput2 == "yes")
            {
                premiumGas = true;
                Console.WriteLine("That's great, you're buying premium gas!");
            }


            if (payingCash && premiumGas = true)
            {
                Console.WriteLine("You're entiteld to 10% discount!");
            }
            else
            {
                Console.WriteLine("Thank you for shopping us. No discounts have applied this time. ");
            }

I did try this:

C#:
if ((payingCash) && (premiumGas = true))

but it says that your local variables haven't been used and it assumes that the user has answered yes to both questions.

I did do some searching on the internet, but I really cannot find anything.

Any feedback welcome.

Thanks
 
SOLVED! I was determined to solve the problem and I did!

I just changed the last If statement slightly:

C#:
if (payingCash && premiumGas == true)

So we essentially check both statements at once.
 
Just note that there's no point comparing a Boolean value to another Boolean value to get a third Boolean value. Think about it. Under what circumstances would the result of this:
if (payingCash && premiumGas == true)

be different to the result of this:
if (payingCash && premiumGas)

The answer is that it never would. The second syntax is the more correct. If your Boolean variable is true then comparing it to true will produce true and if it's false then comparing it to true will produce false, so the result will always be the same as the original variable, so why not just use the original variable? Similarly, if you're testing for false then it's more correct to do this:
if (!myBool)

than this:
if (myBool == false)

The others are not wrong; just less correct.
 
Just note that there's no point comparing a Boolean value to another Boolean value to get a third Boolean value. Think about it. Under what circumstances would the result of this:
if (payingCash && premiumGas == true)

be different to the result of this:
if (payingCash && premiumGas)

The answer is that it never would. The second syntax is the more correct. If your Boolean variable is true then comparing it to true will produce true and if it's false then comparing it to true will produce false, so the result will always be the same as the original variable, so why not just use the original variable? Similarly, if you're testing for false then it's more correct to do this:
if (!myBool)

than this:
if (myBool == false)

The others are not wrong; just less correct.

Wow! You're actually correct. I didn't think that would also work.

Good tip mate,
I'll take that on board.
 
Back
Top Bottom