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 – Console Input


Console input refers to keyboard. Most of the times we assign value s to the variables while declaring them. However, there are times when the program depends on the user provided inputs also. For example:

String your_name_example;

your_name_example  = Console.ReadLine();

In this example, first statement declares a string variable ` your_name_example ‘. Next statement, the Console.ReadLine command will read the value from console that is value which is entered by the user and store it in the variable ` your_name_example ‘.

This command works great if you assigning and entering the values into string variables. However, the input that the computer receives from using the command Console.ReadLine(); does not work with data types such as integers or doubles. This command will accept inputs in the text format only. If you try to input other types of variables using Console.ReadLine() command, an error message is generated.

Instead you can use the parse command as shown below:

int num_var_1;

num_var_1 = int.Parse(Console.ReadLine());

In the above example, the int.Parse command will convert the text input from the string output of  Console.ReadLine() into an integer variable num_var_1.

Similarly, the command double.parse is used to enter a double data type user defined value.

Not only this, the command `Console.WriteLine’ can be used to display messages and values stored in variables. For example,

Console.WriteLine(“Hello. This is an example of display message on screen”);

Continue reading JAVA – Console Input

JAVA – Console Output Commands


Just as the Console Input command, accepts the user defined input, the Console. Output command displays the output on the monitor. In the Java programming language, you can write programs that write text lines to the console, which is a DOS based command window.

Though it’s not as popular as a Graphical User Interface or a GUI, the Console.Output command is used sometimes by programmers. Also, the Console.Output command doesn’t work properly with applets, WebStart applications and normal GUI programs. A sample code that shows the dialog or the console output program for simple input/output command is shown below:

public class example_Console_Output

{

public static void main(String[] args) {

System.out.println(“Hello, This is the example of Console Output command. “);

}

}

The System.out.println command is used to display an entire line of output on the console. The message passed as an argument to the System.out.println command is printed on the console. The println is the short form of print line command and originates from the Pascal programming language.

Continue reading JAVA – Console Output Commands

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