2.3 Modify variable with operations

The concept

One of the most important functionality of programming is to do calculations. And that is why computers are called computers. In order to do calculations, we need operators.

Here is the sample code:


Integer i = 10;
i *= 2; //Means i = i * 2, i is 20 now
i++; //Means i = i + 1, i is 21 now
Boolean b = i > 15; // b is true
Boolean b2 = i <= 20; //b2 is false
Boolean b3 = b && b2; // && means And, b3 is false
Boolean b4 = b || b2; // || means Or, b4 is true
String str = 'Hello ' + 'World'; //str is Hello World

Pretty easy to guess the meaning, isn’t it?

A bit explanation

Sometimes people from non-technical background may find it hard to understand statements like i = i + 1. Please note, this is not a mathematical equation. It is a statement in programming. This statement simply means get the value of i, add it by 1, then store the new value back to i.

i *= 2 is the abbreviation for i = i * 2. Similarly we can write i += 20 and i /= 2.

i++ is the abbreviation for i = i + 1; Similarly we can write i–, ++i and –1. But there is no i** or i // .

&& and || are logical operators, means And and Or.

+ in String operations means joining two strings together. Please note only + works for string. There is no -, * or / for String operation.

Exercise

Look at your current time, say it is 19:40. Calculate how many minutes has elapsed since the beginning of the day. Tell if it is greater than 1000 or not. And try to even more operations.

Next Post

2.4 Variables only store data

Subscribe to Sfdcinpractice

Subscribe to get the latest blogs and tutorials of sfdcinpractice. No spam, no trash, only the awesome posts from sfdcinpractice. 

Leave a Reply

Your email address will not be published / Required fields are marked *