Java Identifiers and Reserved Words and control statements

Before talking about the topic we need to know the basic fundamentals of programming. All the methods, classes, statements and expressions present in a program is made up of the fundamental building blocks called Tokens. In other words, we can say that an expression is a set of tokens.

Tokens are meaningful to the compilers because it helps the compiler understand the code and execute tasks appropriately.

Tokens can further be classified as follows:

  1. Identifiers
  2. Keywords (in this case Reserved Words)
  3. Constants
  4. Literals
  5. Operators
  6. Separators
  7. Comments

This blog will cover the concepts of Keywords and Identifiers, and also control statements present in the Java Programming Language.

Keywords

Keywords are pre-defined or reserved words in a programming language that is usually stored in the library of the programming language.

Each keyword is meant to perform a specific function in a program. Since keywords are referred names for a compiler, they can’t be used as variable names because by doing so, we are trying to assign a new meaning to the keyword which is not allowed. They are always written in lowercase.

Java provides the following keywords :

Identifiers

Identifiers are also popularly (for beginners) known as variable names.

But again it would be inappropriate to call it in such a manner. Identifiers are used to name not just variables but also constant, function, class, array and much more.

These are user-defined names consisting of an arbitrarily long sequence of letters and digits with either a letter or the underscore(_) as a first character.

Identifier names must differ in spelling and case from any keywords. You cannot use keywords as identifiers, they are reserved for special use. Once declared, you can use the identifier in later program statements to refer to the associated value.

Now there are certain rules to be followed while naming an identifier:

  • Identifiers cannot be a keyword.
  • Identifiers are case-sensitive.
  • It can have a sequence of letters and digits. However, it must begin with a letter, $ or _. The first letter of an identifier cannot be a digit.
  • It’s a convention to start an identifier with a letter rather and $ or _.
  • Whitespaces are not allowed.
  • Similarly, you cannot use symbols such as @, #, and so on.

Some valid identifiers are:

  • score
  • level
  • highestScore
  • number1
  • convertToString

Here are some invalid identifiers:

  • class (Keyword)
  • float (Keyword)
  • 1number (number at the start)
  • highest Score (space in between words)
  • @pple (forbidden symbol)

Control Statements

A Java program follows a certain way in which it executes its statements and expressions.

This is called the flow of control or the control flow of a program. Java provides the programmers with certain statements which can alter the flow of control and direct the compiler to execute the code in a certain way. These statements are called control flow statements or just control statements.

There are three different types of control statements:

  1. Decision Making statements
  2. Loop statements
  3. Jump statements

Decision Making statements

Decision Making in programming is similar to decision making in real life.

In programming also we face some situations where we want a certain block of code to be executed when some condition is fulfilled.

They evaluate the Boolean expression and control the program flow depending upon the condition result.

Now we will see a few decision making statements .

If

If statement is the most simple decision making statement.

It is used to decide whether a certain statement or block of statements will be executed or not i.e if a certain condition is true then a block of statement is executed otherwise not.

Syntax:

if(condition) {
// Statements to execute if condition is true
}

Example:

public static void main(String args[]) {    int num = 10;    if (num > 15)
System.out.println(“10 is less than 15”);
// This statement will be executed as if considers one statement by default System.out.println(“I am Not in if”);}

If else

The if statement alone tells us that if a condition is true it will execute a block of statements and if the condition is false it won’t.

With the addition of else statement, we can execute some other statements if the condition is false.

Syntax:

if (condition) {
// Executes this block if condition is true
} else {
// Executes this block if condition is false
}

Example:

public static void main(String args[]) {    int num = 10;    if (num < 15)
System.out.println(“num is smaller than 15”);
else
System.out.println(“num is greater than 15”);
}

Nested-if

A nested if is an if statement that is the target of another if or else.

Nested if statements means an if statement inside an if statement.

Yes, java allows us to nest if statements within if statements. i.e, we can place an if statement inside another if statement.

Syntax:

if (condition1){
// Executes when condition1 is true
if (condition2){
// Executes when condition2 is true
}
}

Example:

public static void main(String args[]){    int num = 10;    if (num == 10) {        // First if statement
if (num < 15)
System.out.println(“num is smaller than 15”);
// Nested — if statement // Will only be executed if statement above it is true if (num < 12)
System.out.println(“num is smaller than 12 too”);
else
System.out.println(“num is greater than 15”);

}
}

If-else if

Here, a user can decide among multiple options.

The if statements are executed from the top down.

As soon as one of the conditions controlling the if is true, the statement associated with that if is executed, and the rest of the ladder is bypassed.

If none of the conditions is true, then the final else statement will be executed.

Syntax:

if (condition)
statement;
else if (condition)
statement;
else
statement;

Example:

public static void main(String args[]){

int num = 20;

if (num == 10)
System.out.println("num is 10");
else if (num == 15)
System.out.println("num is 15");
else if (num == 20)
System.out.println("num is 20");
else
System.out.println("num is not present");
}

Switch-case

The switch statement is a multiway branch statement.

It provides an easy way to dispatch execution to different parts of code based on the value of the expression.

Syntax:

switch (expression)
{
case value1:
statement1;
break;
case value2:
statement2;
break;
.
.
.
.
case valueN:
statementN;
break;
default:
statementDefault;
}

Example:

public static void main(String args[]) {
int num = 9;

switch (num) {
case 0:
System.out.println("num is zero.");
break;
case 1:
System.out.println("num is one.");
break;
case 2:
System.out.println("num is two.");
break;
default:
System.out.println("num is greater than 2.");
}

Loop Statements

In programming, sometimes we need to execute the block of code repeatedly while some condition evaluates to true.

However, loop statements are used to execute the set of instructions in a repeated order. The execution of the set of instructions depends upon a particular condition.

Let’s understand the loop statements one by one.

For loop

In java, for loop is similar to C and C ++. It enables us to initialize the loop variable, check the condition, and increment/decrement in a single line of code. The syntax to use the for loop is given below.

Syntax:

for(<initialization>, <condition>, <increment/decrement>){
// block of statements
}

Example:

public static void main(String[] args) {
int sum = 0;

for(int j = 1; j<=10; j++) {
sum = sum + j;
}

System.out.println("The sum of first 10 natural numbers is " + sum);
}

While loop

The while loop is also used to iterate over the number of statements multiple times. However, if we don’t know the number of iterations in advance, it is recommended to use a while loop.

Unlike for loop, the initialization and increment/decrement doesn’t take place inside the loop statement in while loop.

It is also known as the entry-controlled loop since the condition is checked at the start of the loop.

If the condition is true, then the loop body will be executed; otherwise, the statements after the loop will be executed.

Syntax:

while(<condition>) {
// loop statements
}

Example:

public static void main(String[] args) {
int counter = 1;

System.out.println("Printing the list of first 10 even numbers \n");
int num = 0;
while(counter < 10) {
System.out.println(num);
num = num + 2;
counter = counter + 1;
}
}

do-while loop

The do-while loop checks the condition at the end of the loop after executing the loop statements.

However, it is recommended to use the do-while loop if we don’t know the condition in advance, and we need the loop to execute at least once.

It is also known as the exit-controlled loop since the condition is not checked in advance.

Syntax:

do {    
//statements
} while (condition);

Example:

public static void main(String[] args) {

int counter = 1;

System.out.println("Printing the list of first 10 even numbers \n");

int num = 0;

do {
System.out.println(num);
num = num + 2;

counter = counter + 1;
} while(counter<=10);
}

Jump Statements

Jump statements are used to transfer the control of the program to the specific statements.

In other words, jump statements transfer the execution control to the other part of the program.

There are two types of jump statements in java, i.e., break and continue.

break statement

As the name suggests, the break statement is used to break the current flow of the program and transfer the control to the next statement outside the current flow. It is used to break the loop and switch statement.

However, it breaks only the inner loop in the case of the nested loop.

The break statement cannot be used independently in the java program, i.e., it can only be written inside the loop or switch statement.

continue statement

Unlike the break statement, the continue statement doesn’t break the loop, whereas, it skips the specific part of the loop and jumps to the next iteration of the loop immediately.

Conclusion

In this article, we first discussed about the tokens and then we moved on to identifiers and which are valid and invalid. Then we discussed control statements like conditional statements, looping statements, switch case statements and jump statements

These concepts are very basic and should be known to all kind of developers or coders who are in software engineering to know the computer science aspect of the language design

Thank you for staying with us so far. Hope you liked the article. Like and Comment down your feedback!!

Authors:

  • Aditya Sood
  • Anubhav Pabby
  • Aryama Dubey
  • Avinash Vijayvargiya
  • Ayush Prasad