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
Courtesy : Javatpoint.

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 :

Courtesy: Javatpoint

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 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.

Courtesy : Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

Courtesy: Geeksforgeeks

Syntax:

Example:

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.

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.

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