The Basic Operators in C++

Now we know what are variables and constants, Also we know about getting user input in C++. So now lets see how do we operate on these variables and constants. There are many operators in C++. In this post we will understand them one by one. So lets begin ..

Assignment Operator ( = )

The assignment operator assigns the value on its right hand side to the element on its left hand side. Remember that the assignment operation always takes place from right to left. In simple terms, right side is stored in the left side.

For example:

a=2;

Arithmetic Operators ( +, -, *, /, % )

Addition (+), Subtraction (-), Multiplication (*), Division (/), Modulo (%)

The first four operators are the common ones. Only the Modulo operator might be new for you guys. The Modulo operator simply returns the remainder.

For example:

 a = 10+2; // addition
b = 10 - 2; // subtraction
c = 10 * 2; // multiplication
d = 10 / 2; // division
e = 10 % 2; // modulo

/* output must be
a = 12
b = 8
c = 20
d = 5
e = 0 */ 

Compound Assignment ( +=, -=, *=, /= )

Expression Equivalent Expression
Variable Operator= Value Variable = Variable Operator Value
A += 2 A = A + 2
A -= 2 A = A – 2
A *= 2 A = A * 2
A /= 2 A = A / 2

Increment and Decrement Operator

Increment operator (i plus plus) is similar to i+=1 and i=i+1. While Decrement operator (i minus minus) is similar to (i-=1) and i=i-1.

Increment Decrement
i++
i--
i+=1 i-=1
i=i+1 i=i-1

But there are two types of increment and decrement operators. Namely pre and post. So in total there are 4 sets. Pre Increment(++i), Post Increment, Pre Decrement, Post Decrement.

If you are using these operators as a single statement then there is no problem. For example:

 i++; 

If you are about to ‘display’ or ‘assign’ these operators then pre and post comes into picture. For example :

i = 1;
j = ++i;
//(i is 2, j is 2)

In the above example, you can see i was equal to 1. Then we are storing ++i inside j. Where ++i means pre increment the value of i. So value of i is first increased by 1. Now i becomes 2. And this value is now stored in j. So final values are i is 2 and j is 2.

i = 1;
j = i++;
(i is 2, j is 1)

In the above example, you can see i was equal to 1. Then we are storing i++ inside j. Where i++ means post increment the value of i. So value of i is first stored inside j. So 1 is stored inside j. Now after storing, value of i is increased by 1. Now i becomes 2. So final values are i is 2 and j is 1.

In the similar way you can co-relate the pre and post decrement operators.

Relational Operators ( ==, !=, >, <, >=, <= )

The relational operators help us to relate the value on the right hand side with the value on the left hand side.

== Equal to
!= Not equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to

Note : (==) operator is not the same as (=) operator. Lets see with the help of an example.

a = 1; // this means that we are storing value 1 inside variable a
a == 1; // this means that we are checking whether a is equal to 1 ?

a==1 will either return true or false value. If the value of a is 1 then a==1 will return TRUE value. If the value of a is not 1 then a==1 will return FALSE value. So it is mostly used in conditional statements such as if (a==1) { do this; } 

  • Similarly, (!=) is opposite of (==) operator. Example, (a!=1) will check whether a is not equal to 1 ? 
  • (>) operator.  For example, (a>1) will check whether a is greater than 1 ?
  • (<) operator. For example, (a<1) will check whether a is less than 1 ?
  • (>=) operator. For example, (a>=1) will check whether a is greater than or equal to 1 ?
  • (<=) operator. For example, (a<=1) will check whether a is less than or equal to 1 ?

You can notice that in all the above examples we are asking a question to check the condition is true or false. So we can conclude that relational operators will always return either a true value or a false value. Also remember (==) is not the same as (=) operator.

Examples with numeric constants :

(5== 2) // False
(5 == 5) // True
(5 > 3) // True
(5 > 6) // False
(5 >= 5) // True

Lets see more examples with variables and constants, Consider a = 10.

(a == 5) // False
(a == 10) // True
(a > 5) // True
(a < 5) // False
(a > 10) // False
(a >= 10) // True 

Logical Operators (  !, &&, || )

The operator (!) acts as Logical NOT. It changes true to false and false to true. Lets understand this operator will the help of examples given below.

!true = false
!false = true
!(2==2) = !true = false
!(2!=2) = !false = true 

The operator (&&) acts as Logical AND. Output is true when both inputs are True.

A B A && B
TRUE TRUE TRUE
TRUE FALSE FALSE
FALSE TRUE FALSE
FALSE FALSE FALSE

The operator (||) acts as Logical OR. Output is false when both inputs are False.

A B A && B
TRUE TRUE TRUE
TRUE FALSE TRUE
FALSE TRUE TRUE
FALSE FALSE FALSE

Lets see some examples :

( (5==5) && ( 5 > 10 ) ) // This gives (True && False). Hence total result False.
( (5==5) || ( 5 > 10 ) ) // This gives (True || False). Hence total result True.
( (5>2) && (5>3) ) // This gives (True && True). Hence total result True.
( (5>6) || (5>7) ) // This gives (False && False). Hence total result False.

Difference between normal AND (&) and short circuit AND (&&)

First understand that AND operation returns TRUE value when both conditions are TRUE. Normal AND (&) checks both the conditions even if the first condition turns out to be FALSE. But short-circuit AND (&&) is smart. If it finds the first condition to be FALSE then it does not bother to check the next condition. It rounds up the final answer to be FALSE.

Difference between normal OR (|) and short circuit OR (||)

First understand that OR operation returns FALSE value when both conditions are FALSE. Normal OR (|) checks both the conditions even if the first conditions turns out to be TRUE. But short-circuit OR (||) is smart. If it find the first condition to be TRUE then it does not bother to check the next condition. It rounds up the final answer to be TRUE.

In the next tutorial we will see the conditional operator.

How to protect your Facebook Account

Facebook has become a part of everyone’s life. Almost every individual is there on facebook today. But very few people understand that the most important part of facebook account is security. In this post I will share some tips on how to protect your facebook account from getting hacked. This post will help you to increase the over all security of your facebook account. And i hope you enjoy this post. So lets begin ..

 

 

#1. Enable HTTPS on your Profile

First let me tell you the difference between HTTPS and HTTP. Normally you visit your facebook account or any other website on the HTTP (Hyper Text Transfer Protocol). While HTTPS means Hyper Text Transfer Protocol Secure.

HTTP URL’s begin with “http://” and uses the port 80 by default while HTTPS URL’s begin with “https://” and uses the port 443 by default.

HTTP is very much insecure and is prone to eaves dropping (in simple terms prone to man-in-the-middle). So if you are transferring a sensitive information using HTTP from local computer to remote computer or vice versa then your information is prone to leak. But HTTPS is designed in such a manner that it can withstand all such man-in-the-middle attacks.

Now to enable HTTPS on facebook, just follow these steps :

  1. Go to Facebook Account Settings.
  2. Click on Security Tab.
  3. Select Secure Browsing.
  4. Tick the “Browse Facebook on a secure connection (https) when possible‘ option.
  5. click save changes.

#2 Third Party Apps Permissions

Most facebook users use facebook apps. But have you noticed a thing that you have to grant permission to the apps before you can use them. But very few are aware of the fact that these permissions endure even after we stop using that particular app. And this can be really a huge risk for your facebook account. These permitted third party apps can access all your personal information and use it the way they want. They can’t be trusted. Even the fact that you have to Disable HTTPS for using a Facebook App is enough to make you realize how dangerous these apps can be. So I recommend you to remove the granted permissions from the Apps which you no longer use.

#3 Mobile Notifications

There is a feature on facebook which allows you to receive text notifications whenever your facebook account is accessed from a unknown device. For eg. When you log in from your computer, facebook will ask you to enter a name for your device. Just enter the name and log in. Then facebook will send a text notification to your mobile stating that X device has logged in to your account at xyz time. The next time you log in from the same computer, facebook wont send a text notification. Because facebook as already added your device as the ‘known’ device. But if some other device tries to log into your account, facebook will immediately send a text notification to your mobile stating that a unknown device has logged into your account at xyz time. 

To enable facebook log in notifications just follow these steps :

  1. Go to Facebook Account Settings.
  2. Click on Security Tab.
  3. Select Login Notifications.
  4. Tick Text Message/Push Notification.
  5. Click save changes.

#4 Email Address

I recommend you guys to have two separate email addresses, one which can be used with facebook while other for your secure accounts. Like the one which you use with your Paypal account or for any other sensitive information should not be used with facebook. Because just in case if your facebook account gets compromised, then even the email address which you use with facebook possess a high risk of getting hacked.

#5 Security Question

You can set a Facebook account security question to increase the over all security of your facebook account. The answer to this security question will just remain with you. Just in case if a hacker tried to gain access to your account, he will have to answer this security question. So i recommend you to never to share the answer to your security question with anyone – i mean absolutely no-one but you yourself must know the answer.

If you want to set up a security question just click here.

Note that, Once you set up a security question and the answer then after you cant change it. I don’t know if they (the facebook) allow us to change our security question in future or not. But presently we cannot change our security question once we set it up. So we have got only one chance to set it right.

So that’s why i told you not to share your security answer with anyone. Because you cant change your security question later. So keep it safe with you. Do not share it.

#6 Change Password Occasionally

Changing your facebook password occasionally helps you to strengthen the health of your password. This will incredibly minimize the chances of getting your account hacked. Trust me. Keep changing passwords regularly. In this way you can be sure that you are secure.

And if you loved this article. Please do subscribe to the blog feed. Book mark Codefap.com. And give us a visit when you are free. Thanks. Take care. 🙂

Why to click safely remove hardware icon on USB drives

When you connect a USB drive to a Computer, it gains the freedom to read/write data on the USB drive. The data which is being written on your USB drive from your Computer is not directly stored on the USB drive. But caching process occurs over here. During caching the data is not directly written on your USB drive. Instead it is stored on a temporary memory (RAM).

And if you are about to yank the USB drive directly without clicking the safely remove option. Then you may end up with a corrupted file on your USB drive. This is because the file was not completely transferred, instead was being cached to your computers memory. This is why users are recommended to click ‘Safely remove hardware icon’ on their windows computer.

However, Windows automatically disables the caching process on USB drives when read / write operations are in idle state. So you don’t have to click the safely remove option if you are not writing or reading anything on the USB drive.

The Safely remove option is there for extra security. To make sure that you wont lose any valuable data. Hence preventing you from destroying your own data.

Yanking out the USB drive directly has rarely caused any serious issues. But there are cases when the USB drive becomes corrupt and unusable. So its always recommended to click safely remove option. Its there for your own good.