JAVA – Condition (Decision-making) Statements


Unlike the sequential statements, the Control flow statements enable execution of parts of a code only, based on a particular condition. These control flow statements enable decision making on the program. The common decision-making statements that we use in different Java programs are:

  • if statement
  • if-else statement
  • if-else-if statement
  • Nested if statement
  • Switch

We will look at each of these statements in detail.

JAVA – Control Statements


As you’ve learned in the sequential statements topic, the computer while running a java program, starts executing from the topmost line of the code. It reads the first line of the code, and continues doing so till the last line. Control statements help you run a program in a manner you define. These statements are used to change the computer’s control from reading and executing the statements in a given sequence to the way you define.

Control statements always base the program execution on a set of conditions. Let’s take an example of a programmer designing a program for a financial institute. The data for such programs is confidential and cannot be accessed by all the users. You would provide such an access to the user only if they provide the correct username and password. “Only” if both the passwords and the username are correct, should the program code allow them to see the records. The “else” part of code will run for people who don’t have valid password and enter incorrect information.

As you can see, the control statements help you check a certain condition. You can also put one control statement into another. Such statements are then known as nested control statements. In the example given above, the financial institute software provides 3 chances o each user. If the user enters incorrect information in all the three chances, their records are locked for a particular period. A sample algorithm that would help you design such a program is shown below:

Continue reading JAVA – Control Statements

JAVA Expressions


Let’s first look at expressions. An expression is a program construct defined according to the syntax of the programming language, which uses variables, operators, and methods. Let’s now look at some examples of expressions:

int category_var = 0;

cat_Array[0]=200;

Now you can use these variables in the following expression:

System.out.println(“An element 1 at index 0: ” + cat_Array[0]);

int result_operator = 22 + 40; // result is now 62

The data type of resultant value returned by an expression depends on the data types of the operand used in the expression.  In the example shown above, the data type of the expression category_var = 0 return an int. This is because the assignment operator returns the value of int data type. Apart from int, the expressions can also return data of other types also such as Boolean or String.

Not only this, you can also construct compound expressions in the Java programming language. These compound expressions can be constructed by combining a number of smaller expressions. The only condition being the data type of the left side of the expression should match the data type of the right type.

The following code snippet displays the following compound expression:

3 * 4 * 5

In this example, apart from the product obtained from this expression, it is also important to note the order in which this expression is evaluated by the compiler. The outcome or the product of the expression always remains same irrespective of the order in which they appear in the expression.

Continue reading JAVA Expressions

Sequential Statements in JAVA


A program is defined as a group of statements executed in a given order to achieve a predetermined result.  Therefore, a program executes the statements in a sequential manner, either from top to bottom or vice versa. These are executed in the manner that they appear in the program code, called the sequential execution or sequential control flow.

The sequential statements can also be used in Processes, Procedures and Functions. These are further divided into:

  • Expressions
  • Console Input statements
  • Console Output statements

JAVA – Different type of Statements


A statement is a command given by the programmer to the computer program. Statements are used by different computer programming languages, such as Java, C, and C++. The syntax for declaring and using statements varies in each language. A simple example of a statement can be:

println “Hello ! I hope you are enjoying your stay here”

The statement uses a set of words “Hello! I hope you are enjoying your stay here?” and other details, such as what will be printed on screen.  The command println tells the computer to show the output on the monitor and not on the printer. The computer understands this command in the following two ways:

The programmer can give the command to the computer in the running mode, as the program is being executed.

They can also store a statement or a set of statements in a text file and execute it as and when required. This will then save the task of executing the statements again and again. For example, the command we just saw can be saved in a file Hello.txt and give this file to the computer.

As stated above, you can also save a set of multiple statements in a text file, which will then be executed in the top to bottom order. This means that the statement on the top will be executed first, followed by all other statements in that order.

Continue reading JAVA – Different type of Statements