/*
If
statement Example
This Java
Example shows how to use if statement in Java program.
*/
public class SimpleIfStatementExample {
public static void main(String[] args) {
/*
* If statement is used to execute an action if particular condition is
true.
* Syntax of if statement is,
*
* if(<condition>)
* <statement>
*
* while <condition> is a boolean expression, and statement is a
valied java
* statement which will be executed if <condition> is true. To use
multiple
* statements, enclose them in a block.
*
*/
boolean blnStatus = true;
if(blnStatus)
System.out.println("Status is true");
}
}
/*
Output would be
Status is true
*/
/*
If Else
statement Example
This Java
Example shows how to use if else statement in Java program.
*/
public class SimpleIfElseStatementExample {
public static void main(String[] args) {
/*
* If else statement is used to execute either of two conditions based
* upon certain condition.
* Syntax of if else statement is,
*
* if(<condition>)
* statement1
* else
* statement2
*
* where <condition> is a boolean expression.
* If <condition> is true, statement1 will be executed, else
statement2 will
* be executed.
*/
int i = 0;
if(i == 0)
System.out.println("i is 0");
else
System.out.println("i
is not 0");
}
}
/*
Output would be
i is 0
*/
/*
Compare Two Numbers Java Example
This Compare Two Numbers Java Example shows how to compare two
numbers
using if else if statements.
*/
public class CompareTwoNumbers {
public static void main(String[] args) {
//declare two
numbers to compare
int num1 = 324;
int num2 = 234;
if(num1 > num2){
System.out.println(num1 + " is greater than
" + num2);
}
else if(num1 < num2){
System.out.println(num1 + " is less than
" + num2);
}
else{
System.out.println(num1 + " is equal to
" + num2);
}
}
}
/*
Output of Compare
Two Numbers Java Example would be
324 is greater
than 234
*/
/*
If Else-If
statement Example
This Java
Example shows how to use if else-if statement in Java program.
*/
public class IfElseIfElseExample {
public static void main(String[] args) {
/*
* If Else-if statement is used to execute multiple of actions based upon
* multiple conditions.
* Sysntax of If Else-If statement is
*
* if(<condition1>)
* statement1
* else if(<condition2>)
* statement2
* ..
* else
* statement3
*
* If <condition1> is true, statement1 will be executed, else if
<condition2>
* is true statement2 is executed and so on. If no condition is true, then
else
* statement will be executed.
*/
int i = 10;
if(i > 100)
System.out.println("i is grater than 100");
else if(i > 50)
System.out.println("i
is grater than 50");
else
System.out.println("i is less than 50");
}
}
/*
Output would be
i is less than 50
*/
/*
Determine If Year Is Leap Year Java Example
This Determine If Year Is Leap Year Java Example shows how to
determine whether the given year is leap year or not.
*/
public class DetermineLeapYearExample {
public static void main(String[] args) {
//year we want to
check
int year = 2004;
//if year is
divisible by 4, it is a leap year
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
System.out.println("Year
" + year + " is a leap year");
else
System.out.println("Year
" + year + " is not a leap year");
}
}
/*
Output of the
example would be
Year 2004 is a
leap year */
WHILE LOOP
While
loop executes group of Java statements as long as the boolean condition
evaluates to true.
While
loop syntax
1. while(<boolean condition>){
2.
3.
<Block of statements>;
4.
5. }
Block of statements is any valid Java code. Boolean condition is any valid Java expression that evaluates to boolean value. Braces are options if there is only one statement to be executed.
While
loop evaluates the boolean expression, and if it is true, it executes the block
of statements. It continuously executes the block of statements until the
boolean condition evaluates to false value. After that loop is terminated and
statement immediately after the loop body will be executed.
/*
While loop
Example
This Java
Example shows how to use while loop to iterate in Java program.
*/
public class SimpleWhileLoopExample {
public static void main(String[] args) {
/*
* Syntax of while loop is
*
* while( <condition> )
* <loop body>
*
* where <condition> is a boolean expression. Loop body is executed
as long
* as condition is true.
*
* Loop body may contain more than one statments. In that case it should
be
* enclosed in a block.
*/
int i = 0;
while(i < 5)
{
System.out.println("i is : " + i);
i++;
}
/*
* The following code will create an infinite loop, since j < 5 will
always
* evaluated to true
*/
//int j = 0;
//while(j < 5)
// System.out.println("j is : " + j);
}
}
/*
Output would be
i is : 0
i is : 1
i is : 2
i is : 3
i is : 4
*/
DO WHILE
Do
while loop executes group of Java statements as long as the boolean condition
evaluates to true.
It
is possible that the statement block associated with While loop never get
executed because While loop tests the boolean condition before executing the
block of statements associated with it.
In
Java programming, sometime it's necessary to execute the block of statements at
least once before evaluating the boolean condition. Do while loop is similar to
the While loop, but it evaluates the boolean condition after executing the
block of the statement.
Do
While loop syntax
1. Do{
2.
3.
<Block of statements>;
4.
5. }while(<boolean condition>);
Block
of statements is any valid Java code. Boolean condition is any valid Java
expression that evaluates to boolean value. Braces are options if there is only
one statement to be executed.
/*
Do While
loop Example
This Java
Example shows how to use do while loop to iterate in Java program.
*/
public class DoWhileExample {
public static void main(String[] args) {
/*
* Do while loop executes statment until certain condition become false.
* Syntax of do while loop is
*
* do
* <loop body>
* while(<condition>);
*
* where <condition> is a boolean expression.
*
* Please not that the condition is evaluated after executing the loop
body.
* So loop will be executed at least once even if the condition is false.
*/
int i =0;
do
{
System.out.println("i
is : " + i);
i++;
}while(i < 5);
}
}
/*
Output would be
i is : 0
i is : 1
i is : 2
i is : 3
i is : 4 */
FOR LOOP
For
loop executes group of Java statements as long as the boolean condition
evaluates to true.
For
loop combines three elements which we generally use: initialization statement,
boolean expression and increment or decrement statement.
For
loop syntax
1. for( <initialization> ; <condition> ; <statement> ){
2.
3.
<Block of statements>;
4.
5. }
The initialization statement is executed before the loop starts. It is generally used to initialize the loop variable.
Condition
statement is evaluated before each time the block of statements are executed.
Block of statements are executed only if the boolean condition evaluates to
true.
Statement
is executed after the loop body is done. Generally it is being used to
increment or decrement the loop variable.
Following
example shows use of simple for loop.
1. for(int i=0 ; i < 5 ; i++)
2. {
3.
System.out.println(“i is : “ + i);
4. }
It is possible to initialize multiple variable in the initialization block of the for loop by separating it by comma as given in the below example.
1. for(int i=0,
j =5 ; i < 5 ; i++)
It is also possible to have more than one increment or decrement section as well as given below.
1. for(int i=0; i < 5 ; i++,
j--)
However it is not possible to include declaration and initialization in the initialization block of the for loop.
Also,
having multiple conditions separated by comma also generates the compiler
error. However, we can include multiple condition with && and ||
logical operators.
/*
Simple For
loop Example
This Java
Example shows how to use for loop to iterate in Java program.
*/
public class SimpleForLoopExample {
public static void main(String[] args) {
/* Syntax of for loop is
*
* for(<initialization> ; <condition> ; <expression> )
* <loop body>
*
* where initialization usually declares a loop variable, condition is a
* boolean expression such that if the condition is true, loop body will
be
* executed and after each iteration of loop body, expression is executed
which
* usually increase or decrease loop variable.
*
* Initialization is executed only once.
*/
for(int index = 0; index < 5 ; index++)
System.out.println("Index is : " + index);
/*
* Loop body may contains more than one statement. In that case they
should
* be in the block.
*/
for(int index=0; index < 5 ; index++)
{
System.out.println("Index is : " + index);
index++;
}
/*
* Please note that in above loop, index is a local variable whose
scope
* is limited to the loop. It can not be referenced from outside
the loop.
*/
}
}
/*
Output would be
Index is : 0
Index is : 1
Index is : 2
Index is : 3
Index is : 4
Index is : 0
Index is : 2
Index is : 4
*/
/*
List Even Numbers Java Example
This List Even Numbers Java Example shows how to find and list
even
numbers between 1 and any given number.
*/
public class ListEvenNumbers {
public static void main(String[] args) {
//define limit
int limit = 50;
System.out.println("Printing Even numbers between 1 and
" + limit);
for(int i=1; i <= limit; i++){
// if the number is divisible by 2 then it is even
if( i % 2 == 0){
System.out.print(i + " ");
}
}
}
}
/*
Output of List
Even Numbers Java Example would be
Printing Even
numbers between 1 and 50
2 4 6 8 10 12 14
16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50
*/
/*
Declare
multiple variables in for loop Example
This Java
Example shows how to declare multiple variables in Java For loop using
declaration
block.
*/
public class DeclaringMultipleVariablesInForLoopExample {
public static void main(String[] args) {
/*
* Multiple variables can be declared in declaration block of for loop.
*/
for(int i=0,
j=1, k=2 ; i<5 ; i++)
System.out.println("I
: " + i + ",j :
"+ j + ", k : " + k);
/*
* Please note that the variables which are declared, should be of same
type
* as in this example int.
*/
//THIS WILL NOT COMPILE
//for(int i=0, float j; i < 5; i++);
}
}
/*
Java Pyramid 1
Example
This Java Pyramid example shows how to generate pyramid or
triangle like
given below using for loop.
*
**
***
****
*****
*/
public class JavaPyramid1 {
public static void main(String[] args) {
for(int i=1; i<= 5 ;i++){
for(int j=0; j < i; j++){
System.out.print("*");
}
//generate a new line
System.out.println("");
}
}
}
/*
Output of the
above program would be
*
**
***
****
*****
*/
/*
Prime Numbers Java Example
This Prime Numbers Java example shows how to generate prime
numbers
between 1 and given number using for loop.
*/
public class GeneratePrimeNumbersExample {
public static void main(String[] args) {
//define limit
int limit = 100;
System.out.println("Prime numbers between 1 and
" + limit);
//loop through the
numbers one by one
for(int i=1; i < 100; i++){
boolean isPrime = true;
//check to see if the number is prime
for(int j=2; j < i ; j++){
if(i % j == 0){
isPrime = false;
break;
}
}
// print the number
if(isPrime)
System.out.print(i + " ");
}
}
}
/*
Output of Prime
Numbers example would be
Prime numbers
between 1 and 100
1 2 3 5 7 11 13 17
19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97