Confused about logical statements in C++

  • C/C++
  • Thread starter torquerotates
  • Start date
  • Tags
    C++ Confused
In summary, The conversation discusses the logic behind the isLeapYear function in C++, which checks if a given year is a leap year. The confusion arises from the use of binary logical statements and the order of precedence and binding in C++. The function is equivalent to (y%4==0 && y % 100 !=0 || y% 400==0) and uses short circuiting to optimize its execution. It is important to note that the order of execution is not guaranteed in C++, which can lead to unintended side effects.
  • #1
torquerotates
207
0
Maybe this should belong in the logic section. But since this is C++, ill put it here.

bool isLeapYear( int y)
{
return y%4==0 && y % 100 !=0 || y% 400==0 ;
}



They said it returns when y is divisible by 4 but not by 100 unless it is also divisible by 400.

This is a bit confusing since I thought that statements are binary. that is its either A and(BorC) or (AorB)andC. I mean we get statements like (A and B and B) b/c [A and (B and C)]=[A and B and C]. Same thing for the or operator. But I can't see how (A and B or C) results from a binary logical statement involving two sets.

Do they mean (y%4==0 && y % 100 !=0 || y% 400==0) is the same as

(y%4==0) && (y % 100 !=0 || y% 400==0)?

It would make a lot more sense b/c
(y%4==0) && (y % 100 !=0 || y% 400==0) is the same as

(y%4==0 && y % 100 !=0) ||(y%4==0 && y% 400==0)

which is the same as the statement:y is divisible by 4 but not by 100 unless it is also divisible by 400.
 
Technology news on Phys.org
  • #2
It works. Let's look at 100 as an example and 800.

100:

First, C++ checks if y % 4 == 0. 100 % 4 == 0, so this is true.

Then, it checks if y % 100 != 0. This is false.

true AND false is false.

Then, it checks if y % 400 == 0. 100 % 400 == 100 != 0, so this is false.

false OR false is false, so the function returns false.

800:

First, C++ checks if y % 4 == 0. 800 % 4 == 0, so this is true.

Then, it checks if y % 100 != 0. This is false.

true AND false is false.

Then, it checks if y % 400 == 0. 800 % 400 = 0 == 0, so this is true.

false OR true is true, so the function returns true.
 
Last edited:
  • #3
torquerotates said:
Do they mean (y%4==0 && y % 100 !=0 || y% 400==0) is the same as
(y%4==0) && (y % 100 !=0 || y% 400==0)?
No! The initial expression is the same as

(((y % 4 == 0) && (y % 100 != 0)) || (y% 400 == 0))

A fully parenthesized version of this expression is

((((y % 4) == 0) && ((y % 100) != 0)) || ((y% 400) == 0))


The expression (y%4==0 && y % 100 !=0 || y% 400==0) has no parentheses. There is a lot of ambiguity in that expression. One has to look to the precedence table to resolve this ambiguity. Modulus takes precedence over every other operation in this expression, so the first step in resolving the ambiguity is to group the modulus operations:

((y % 4) == 0 && (y % 100) != 0 || (y% 400) == 0)

Equality and inequality come next in precedence, resolving the expression to

(((y % 4) == 0) && ((y % 100) != 0) || ((y% 400) == 0))

Here we have something of the form A && B || C. Logical and takes precedence over logical or, perhaps by way of extension that multiplication takes precedence over addition (2*3+4 is 10, not 14). With this rule, A && B || C is the same as (A && B) || C, or

((((y % 4) == 0) && ((y % 100) != 0)) || ((y% 400) == 0))


That fully-parenthesized version leaves no doubt as to the meaning. My personal rule: Don't rely on the precedence table except where it's patently obvious. 2*3 + 4 is patently obvious. It's 10, no parentheses are needed. Beyond that, it's not obvious. I tend to use lots of parentheses to remove all doubt. On the grounds that arithmetic operators taking precedence over the comparison operators is fairly obvious, I might write the above as

(((y % 4 == 0) && (y % 100 != 0)) || (y% 400 == 0))


Note that even though your interpretation is incorrect, it still works here. The reason is that 100 and 400 are multiples of 4.
 
  • #4
Be aware that C and C++ will "short circuit" boolean expressions.

in the case of

if (A && B) {...}

A is checked first, and B will never be evaluated if A is false, which can have side effects if B is a function that updates a state.

Also be aware the order of execution is not guaranteed under C or C++. Only the order of precedence, and order of binding is guaranteed.

a+b+c is parsed as a+(b+c) because of right to left binding on the + operator, but the compiler can compute B and C in any order it wants to. Again this can have side effects where B and C are [STRIKE]not simple numbers[/STRIKE] interdependent.
 
Last edited:
  • #5


I can understand your confusion with logical statements in C++. The statement provided is a conditional statement that checks if a given year is a leap year. The statement can be broken down into two parts: (y%4==0 && y % 100 !=0) and (y% 400==0). The first part, (y%4==0 && y % 100 !=0), checks if the year is divisible by 4 but not by 100. If this condition is true, the statement returns true. However, if the first part is false, the second part, (y%400==0), checks if the year is also divisible by 400. If this condition is true, the statement also returns true.

To answer your question, yes, (y%4==0 && y % 100 !=0 || y% 400==0) is the same as (y%4==0) && (y % 100 !=0 || y% 400==0). The logical operators in C++ follow a specific order of operations, similar to mathematical operations, where "and" (&&) is evaluated before "or" (||). So, in this case, the statement is evaluated as (y%4==0 && (y % 100 !=0 || y% 400==0)), which is the same as the original statement provided.

I hope this explanation helps clarify your confusion about logical statements in C++. Keep in mind that logical statements can sometimes be complex and require careful evaluation, but with practice, you will become more comfortable with them.
 

1. What are logical statements in C++?

Logical statements in C++ are expressions that evaluate to either true or false. They are used to make decisions in a program and control the flow of execution.

2. What are the logical operators in C++?

The logical operators in C++ are && (and), || (or), and ! (not). They are used to combine multiple logical statements and create more complex conditions.

3. How do I write a simple logical statement in C++?

A simple logical statement in C++ consists of two operands and a logical operator. For example, x > 10 is a logical statement that checks if the value of x is greater than 10.

4. What is the difference between && and || in logical statements?

The && (and) operator evaluates to true only if both operands are true. The || (or) operator evaluates to true if at least one of the operands is true. In other words, && requires both conditions to be true, while || only requires one.

5. How do I use logical statements in control structures like if-else statements?

In control structures like if-else statements, logical statements are used to determine which branch of code to execute. The code inside the if statement will only be executed if the logical statement evaluates to true. Otherwise, the code inside the else statement (if present) will be executed.

Similar threads

  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
Replies
11
Views
1K
  • Programming and Computer Science
Replies
17
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Set Theory, Logic, Probability, Statistics
Replies
2
Views
1K
  • Precalculus Mathematics Homework Help
Replies
3
Views
1K
  • Calculus and Beyond Homework Help
Replies
12
Views
2K
Replies
4
Views
1K
  • Introductory Physics Homework Help
Replies
6
Views
640
Back
Top