JAVA – Loop Statement

The Loop statements are used to execute a particular statements a pre-defined number of times. This statement allows a particular piece of code to be repeatedly executed. The Loop statement is enclosed in a LOOP and the END LOOP keywords, and is classified as an iteration statement. The Java script, like many other programming languages, supports the use of Loops.

Many different types of loop statements are available, such as:

  • While Loop
  • For Loop
  • Do-while Loop

The For Loop is a commonly used loop statement. This loop is associated with a loop counter or a loop variable, which allows the body of the For loop to know the exact sequence of each iteration. The body of the For loop is the code that is being executed repeatedly. These loops are generally used when the programmer knows the number of iterations before executing the program. Not only this, the keyword For, which constitutes the name of the For loop originates from the English language word For.

When the Java script executor finds a For loop in a program, it first executes or runs the initialization statements, initializing all the variables, and then it executes and checks for the given conditions. The interpreter enters the ‘loop’ once the condition evaluates to True. The condition is executed repeatedly till the condition evaluates to false. This case also makes the end of the loop.

When running a loop in Java, you must consider the following points:

  • When the Java interpreter evaluates a For loop, it initializes all the variables.
  • The condition is evaluated only after the initialization statements are executed.
  • The update statements are executed and the conditions are checked after every iteration.

The program code given below executes a For loop to prints the digits from 11 to 20.

var message_variable = “”;

for (var Counter_variable = 11; Counter_variable <= 20; Counter_variable ++)

{

message_variable = message_variable + Counter_variable + “\n”;

}

Continue reading JAVA – Loop Statement

How to do Iteration Using JAVA

Iteration in computing terminology means the repetition of a process or a method within a computer program. It defines the style of programming used in all the advanced programming languages.

Let’s look at a pseducode or a short code for the iteration.

var Num_var_2, Num_var_3=0// initialize the variable c before iteration or reputation

For Num_var_3=1 to 5 // loop the variable Num_var_3 5 times

Num_var_2:= Num_var_2 + Num_var_3 // increment Num_var_2 with current value of Num_var_3

print Num_var_2 // the number 10 is printed

In this pseudocode the value of the variable Num_var_3 keeps on changing over time. The variable Num_var_3 takes the values from 1 to 6. Note that, this changing value, also known as a mutable state, is the primary characteristic of iteration.

Iterator is another important term associated with the iteration is an object that wraps iteration.

In computer science, an iterator is an object that enables you to go through all the parts or elements contained in another object in a particular sequence. The iterator is generally used in a container or a list.

Java Control Statement – The Switch Statement


The Switch statement unlike the If-else statement allows any number of or multiple execution paths. The Switch statement is supported by the int, char, byte, and short data type.  Not only this, some other basic data types that wrap the data types mentioned above also support these statements.

The program given below Demo_of_switch_program, shows the use of the Switch statement. In this program a variable int named day_number is declared whose value represents a day of the week. This program will display the output as the current day; decision is made using the value of the day_number it receives, using the switch statement.

class Demo_of_switch_program {

public static void main(String[] args) {

int day_number = 5;

switch (day_number) {

case 1:  System.out.println(“Today is Monday.”); break;

case 2:  System.out.println(“Today is Tuesday.”); break;

case 3:  System.out.println(“Today is Wednesday.”); break;

case 4:  System.out.println(“Today is Thursday.”); break;

case 5:  System.out.println(“Today is Friday.”); break;

case 6:  System.out.println(“Today is Saturday”); break;

case 7:  System.out.println(“Today is Sunday.”); break;

default: System.out.println(“There is no day by that number.”);break;

}

}

}

As you can see in the example given above, the body of a switch statement, which has code showing current day based on the value specified in beginning, is called the switch block. The statements that form the part of a switch block can have cases or default labels depending on values of switch variable and decision requirements. The switch statement evaluates the given expression and executes the code contained in the case statement.

Continue reading Java Control Statement – The Switch Statement

Java Control Statement – Nested IF-Else Statements


When an IF statement contains another IF statement in it as one of its branches, these IF statements are said to be nested. Theoretically, you can place as many If-statements as you want in one statement to form the nested IF statements. However, complex nested if statements can make the program execution complex, leading to erroneous results.  Instead use of the keyword Evaluate can be a better choice when testing more than 2 variables simultaneously.

Even, when using nested if statements, always remember to use proper indentation and end them with explicit scope terminators.

The pseudo code given below shows the syntax for the nested IF statement:

IF condition-a

IF condition-b

statement-1

ELSE

statement-2

END-IF

statement-3

ELSE

statement-4

Continue reading Java Control Statement – Nested IF-Else Statements