Showing posts with label &&. Show all posts
Showing posts with label &&. Show all posts

Leap Year in C++

Let's use our knowledge of logical operators to learn how to detect whether a year is leap or not.

C++ tutorial algorithm

Leap years

Typically, the year has 365 days.
Actually, it's a broken value, it's 365 days and 6h or 365.25 days ... but it was going to be weird a day under 24 hours, it was going to be confusing.

So to make up for those broken parts of the day, time and time we have a year with 366 days, which is February 29th.

And they occur every 4 years, minus the multiples of 100 that aren't multiples of 400.
That is, it is also leap year if it's multiple of 400.

Calm down, take a deep breath, read again ... it's a little confusing even at first.
Take a look at the history of leap years, calendars, etc., to relax a little:
https://en.wikipedia.org/wiki/Leap_year

Basically, every 4 years we have leap years:
...1996, 2000, 2004, 2008, 2012, 2016, 2020, 2024, 2028, 2032, 2036 ...

Every 100 years we don't leap year ...:
100, 200, 300 ... are not leap years

... except if they are multiples of 400:
400, 800, 1200, 1600 ... are leap years

Like this:
...
400 - is leap
500 - not leap
600 - not leap
700 - not leap
800 - is leap
...
1200 - is leap
1300 - not leap
1400 - not leap
1500 - not leap
1600 - is leap
1700 - not leap
1800 - not leap
1900 - not leap
2000 - is leap
2100 - not leap
2200 - not leap
2300 - not leap
2400 - is leap
...

Leap year code in C++

Ok, let's put this into practice by creating C ++ code.
A difficult problem is nothing more than several easy problems.

So let's break this algorithm into a smaller, simpler one.
First, let's look at the multiple years of 400. If it's a multiple of 400, it's gone, it's just success, it's leap, and it's over:

And how do you verify that? Simple:

  • if (year % 400 == 0)


Ready. Just say it's leap.
And if not? Well, it falls on the ELSE.

Now comes the 4 year part ... every 100 years ... that stuff weird.
Well, that's how we need to check two things:

  • If is multiple of 4 (occurs every 4 years, from the year 0: 4, 8, 12, ... 1996, 2000)
  • If not multiple of 100


Doing this in C ++:

  • Multiple of 4: (year% 4 == 0)
  • Not multiple of 100: (year% 100! = 0)

Putting the two together, we have the next IF (inside the else), using the logical AND&& operator:

  • (year % 4 == 0)  && (year% 100 != 0)

So our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int year;

    cout <<"Year: ";
    cin >> year;


    if(year % 400 == 0)
        cout << "It's leap year" << endl;
    else
        if( (year % 4 == 0) && (year % 100 != 0) )
            cout << "It's leap year" << endl;
        else
            cout << "Not a leap year" << endl;


    return 0;
} 

Better Leap Year Algorithm in C++

Programmer likes to write little.
The simpler and more direct the code, the better.

Note in the previous code that we have two conditions for the year to be leap:
  1. (year % 400 == 0)
  2. ((year % 4 == 0) && (year % 100 != 0))

If one thing or another happens, it is leap.
Or ... huumm ... or remember what? Logical Operator ||

That is, we can join the two conditions and have only one IF conditional test:
  • (year% 400 == 0) || ( (year% 4 == 0) && (year% 100! = 0) )

Our code looks like this:
#include <iostream>
using namespace std;

int main()
{
    int year;

    cout <<"Year: ";
    cin >> year;


    if( (year % 400 == 0) || ( (year % 4 == 0) && (year % 100 != 0) ) )
        cout << "It's leap year" << endl;
    else
        cout << "Not a leap year" << endl;


    return 0;
}
Beautiful, huh?

C++ Logical Operators: AND (&&), OR (||) and NOT (!)

In this tutorial of our C++ course, we will learn three more operators, called logical operators, that will allow us to do more complex and interesting conditional tests. They are:

  • && - AND 
  • || - OR
  • ! - NOT

Logical Operator AND: &&

The && operator serves to merge two logical expressions into one, like this:

  • expression1 && expression2

The result of this expression is true only if expression1 and expression2 are true.
If either one of them is false, the general expression becomes false as well.

The truth table of the && operator is:

Logical operators in C++

For example, for you to donate blood, there must be two conditions:
  • Be in legal age
  • Not have diseases

That is, for pain you need to be adult AND ALSO not have diseases:
  • adulthood && have no diseases

If any of these conditions are not true, you cannot donate blood.
Let's redo the program that says whether you must vote or not.

To be mandatory, it must satisfy two conditions:
  1. 21 years or older
  2. Under 65

That is, you must be 21 years old AND (AND) must be under 65 years old.
Our C++ code looks like this:

#include <iostream>
using namespace std;

int main()
{
    int age;

    cout <<"Your age: ";
    cin >> age;


    if( age>=21 && age<65 )
        cout << "Required to vote.";
    else
        cout << "Optional vote";


    return 0;
}

See that it was much more cleaner code, because we made two conditional tests at once within the IF.
If the person is under 21, the first expression will be false and the entire test will be false, going to ELSE.
If it is 65 or older, the second expression is already false, leaving the IF result false as well, going to ELSE.
In both cases, voting is optional.


Logical Operator OR: ||

The logical operator || (OR) joins two expressions like this:

  • expression1 || expression2

It returns true if any of the expressions are true.
That is, it only returns false if both are false. Check out this operator's truth table:

Truth table in C++

For example, if on a highway the maximum speed is 80 km/h, walking above and below half (40 km/h ) will give you a traffic ticket. Lets test:

  • speed> 80 || speed <40


That is: if the speed is greater than 80 km/h OR is below 40 km/h, the driver will be fined. If any of the expressions are true, the entire test is true.

Here's a program that takes the speed of a car and tells you whether it will generate a ticket or not:
#include <iostream>
using namespace std;

int main()
{
    int speed;

    cout <<"Speed: ";
    cin >> speed;


    if( speed>80 || speed<40 )
        cout << "You'll be fined.";
    else
        cout << "That's ok.";


    return 0;
}
The IF detects if you are going to get a fine, that is, not to get it needs to fall into ELSE, and only falls into ELSE if both expressions are false, so you are neither above 80 nor below 40, so you are between 40 and 80, which is the correct one. Did you get it? OR one thing OR another.

Logical Operator NOT: !

Finally, we have the negation operator.
It turns what is false into true, and what is true into false.

See his truth table:
Truth Tablet NOT Operator
That is:

  • !true is the same as false
  • !false is the same as true


Redoing the first code:
#include <iostream>
using namespace std;

int main()
{
    int age;

    cout <<"Your age: ";
    cin >> age;


    if( !(age>=21 && age<65) )
        cout << "Optional vote.";
    else
        cout << "Required to vote";


    return 0;
}
Redoing the second code:
#include <iostream>
using namespace std;

int main()
{
    int speed;

    cout <<"Speed: ";
    cin >> speed;


    if( !(speed>80 || speed<40) )
        cout << "Thats ok.";
    else
        cout << "You'll be fined.";


    return 0;
}
It may seem more complicated or the same thing, in fact it is the same thing, but often it will be more logical and it will make more sense to use the NOT operator in some conditional statements in C++. We will see this a few times over the our C ++ course.

The precedence of operators between them is from top to bottom:
!
&&
||

Logical Operators Exercise

In the last tutorial on nested IF and ELSEs, we posed a question about converting a numeric note to A, B, C ... but there is a bug there.

What if the user types less than 0 or more than 10?

It is a mistake. Arrange the program using the logical operator || if you type notes below 0 or above 10.