Incremental, Decremental & Assignment Operator

So far you have seen how we do additional and subtraction by using 2 variable to preform additional and subtraction and assign it to a their variable.

Incremental, decremental and assignment operator are the similar to additional and subtraction. Incremental operator will be used if you want to add 1 to the variable. For example count++. Decremental operator will be used if you want to subtract 1 to the variable. For example: count– . Assignment operator will be used if you want to add value that are more than 1 or subtract value more than 1. For example: count += 5 or count -=5.

In incremental and decremental there are 2 types of it. One is pre and one is post. The code below will show and explain about this 2 types:

increop
Code for Incremental, Decremental & Assignment  Operator  with comments.

Test yourself

Create a simple calculator to take 2 integer variables from the user and perform additional on the 2 integer. Lastly, print out the equation with the answer.

simpleCalculator
One way of writing the program

NOTE: There can be many ways to write a program and give the same result.

Maths Operator

In programming it is essential to have the basic knowledge of Mathematics like addition, subtraction, multiplication and so. In short know about the maths operator. This are the following Math operator that you should know:

  • + → addition operator
  • → subtraction operator
  • * → multiplication operator
  • / → division operator.
    If you use division operator on 2 integer variable you will get another integer variable regardless of the reminder. Usually integer variables will ignore the remainder. However, if you use 2 double or float variables you will get another double or float variable with the reminder as the data type accepts reminder.
  • % → modulo operator.
    Modulo operator is the same as division operator but it will only give the reminder of the 2 variables. For example 8 % 3 = 2, 7 % 5 = 2
mathoperator
The code for Math operator

User Inputs

Sometimes we create a program for user to use it. Therefore, we need the program to read the user input. In Java, we use Scanner object to scan for user input and store it in a variable. To use the Scanner object, you need to import scanner function from utility (util) library first.

  1. How to import the Scanner function from utility library?

    import java.util.Scanner;

    Add the above line of code in between the package and public class.

  2. How to use the Scanner to read the user input and store it in a variable?

    Scanner scan = new Scanner(System.in);

    System.in – Reading the user input from keyboard.
    Scanner scan = New Scanner(); – the system input will be assigned and stored to a Scanner object named scan.

  3. Make sure that the user provide the necessary input before the program continues to run. Therefore, we need the nextLine() to make sure the program pause till the input is given.

    System.out.println(scan.nextLine());

    nextLine() – is used for String inputs. If the input is integer, use nextInt().

userInput
User Input codes

Variables

What are variables and why do we need variables in programming? 

Variables are like boxes to store items. In Java, you need to know the sizes of the boxes so that the item can be stored properly. In another words, you need variables to store your data (item) and need to know the datatype(size) of the variables(box).

We need this variable to perform some operations and it would be better to change the value of the variable and then print them out rather then hard-coding in the println method. When we hard-code the data in the println, there might be some possibilities that the data might change and we might need to check the whole document to see if we got mention the data anywhere.

So now the question will be, how to write and declare a variable in Java? 
Answer:

datatype variableName = data;

String name = ‘Bob’;
int age = 23;
boolean checker = True;
double price = 23.33;

The actual code to play with the variables:

vaiables
The actual codes of variables with comments

Hello World

public class HelloWorld {

public static void main(String[] args){
System.out.println(“Hello world”);
}
}

public class HelloWorld – is a class file that you will use to start programming your java program. Within the ‘{}’ of HelloWorld class is where all the relevant codes of that class file/program will be in.

public static void main(String[] args) – we need this method as a default method because Java needs to know where to start the execution the codes. Without this the Java will not know. This line is also known as a method header named ‘main’ and the code inside the ‘{}’ is known as method body.

System.out.println(“Hello world”); – the system outputs by printing the “Hello world” message. Print is a built-in method and helps to print out the statement inside the ‘()’.

HelloWorld
First program codes with comments

Welcome to Magic Codez

I know what you are thinking. Why create a blogging site for coding since there are many coding websites out there? True, but all are only basic level.

My intention is to share my experience in coding like the issues I face when coding. I am not professional in coding. I know the basic level of coding but I face a lot of difficulties when I want to go to advance level. Therefore, I am hoping that by using this blogging platform I can build the bridge from basic level to advance level of coding.

Hope that everyone of you who are seeing this blog would support me.

Thanks,

Magic Coder 🙂

Continue reading “Welcome to Magic Codez”