Saturday, April 16, 2011

Java : Do-While Loops

The while loop in Java allows a loop block to be executed an arbitrary number of times until a condition is met.

In java the “while loop” can be used in two different ways. The ways are “while-do” and “do-while”. According to your algorithm you can select and use one of them.

The “while-do” form, before executing the code block, allows the condition to be tested. It means that it is possible for the code block never to be executed. Firstly check the condition and then execute the code block.

Generally “while-do” syntax is:

while (condition) {

code_block;

}

In this syntax, “while-do” loop, it is important to know that, the keyword “do” is not used.

“do-while” loop does not check the condition until the loop code block is executed. It means that in a “do-while” loop the code block will be run at least once.

Generally “do-while” syntax is:

do {

code_block;

} while(condition)

The condition can be any expression that evaluates a Boolean value.

Now, by using “while-do” we write a code block which will output the number 1-10.

int i = 1

while (i < 11) {

System.out.println(i);

i = i + 1;

}

Output : 1 2 3 4 5 6 7 8 9 10

Now , by using “do-while” we write a code block which will output the numbers 1-10.

int i = 1

do {

System.out.println(i);

i = i + 1;

} while (i < 11)

Output : 1 2 3 4 5 6 7 8 9 10

Friday, April 15, 2011

Java : IF Statements

The if statement is a fundamental programming construct. “If” allows a program to execute different blocks of code depending on the result of a test. In this article I will describe the variations of the “if” statement in Java Programming Language.

The general form of a Java if statement is:

if (satement) {

code_block_execute;

} else {

Code_block_non_execute;

}

Where statement is anything that evaluates as a boolean value. If the statement is true then the Java code in the “code_block_execute” block is executed. Otherwise, the code in “code_block_non_execute” is executed. The else statement is optional, so the simplest example of the if statement is:

if (x == 1) {

System.out.println(“As you see, x is one”);

}

A block of code is either a single line of code or several lines of code contained in curly braces. Note that in this example, enclosing the code in curly braces is not required, although some programmers prefer to use them anyway since it makes it clearer what code is exectued.

Multiple else if conditions can be chained together:

In this code given below we will decide if the score Exellent, Good etc. Take score from the user and return a logical value for entered scrore.

import java.util.Scanner;

public class IfElseIfExample

{

public static void main(String args[])

{

System.out.println("Enter your score : ");

Scanner sc = new Scanner(System.in);

int score = sc.nextInt();

if ( score >= 90 )

{

System.out.println("Excellent");

}

else if ( score >= 80 )

{

System.out.println("Good");

}

else if ( score >= 70 )

{

System.out.println("Satisfied");

}

else if ( score >= 60 )

{

System.out.println("Not too bad");

}

else

{

System.out.println("Need to improve");

}

}

}

Java "Hello World"

This is my first attempt to write something about java and java technologies. In my blog I will try to give information about java programming language.

In this small text (on the picture), we will say Hello to the World via java programing language.

Hello World is a classic sample to start when we learn a new programming language. Below is the Java version of Hello World program, it simple enough to start.




The code contains one class called Main, a main(String[] args) method which is the execution entry point of every Java application and a single line of code that write a Hello World string to the console.

The Main class must be saved in a file named Main.java, the class file name is case sensitive. In the example we also define a package name for the class. In this case theHelloWorld.java must be placed in a helloworld directory.

Then build and run it. . .