Another types of loop is the "for loop". This "for loop" and "loop while" has the same function but they have different source code. Here are some simple example of this type of loop.
import java.util.*;
public class for1{
public static void main(String[]args){
for(int x=0;x<=5;x++)
{
System.out.println(x);
}
}}
output:
0
1
2
3
4
5
import java.util.*;
public class for1{
public static void main(String[]args){
for(int x=5;x>=0;x--)
{
System.out.println(x);
}
}}
output:
5
4
3
2
1
0
import java.util.*;
public class for1{
public static void main(String[]args){
for(int x=5;x>=0;x--)
{
System.out.print(x);
}
}}
output:
543210
These three example of java program are "for looping".
Java Programming Language
This all about java programming language. I'm putting it online to share it with you guys. I hope it helps a lot.
Thursday, July 7, 2011
Monday, May 30, 2011
Learning Java 09 - Loop_while
Looping is the process of repeating a certain action, statement, graphic or even a whole program. There are two types of loop, the "for" loop and the "while" loop. But this two types of looping creates same results, its' just the way of programming it that makes difference. In this post, let's all talk about "while" loop.
So, here's a very simple example of "while" loop. Programming is more on logic, so just try to analyze the logic of this following programs.
output:
1
2
3
4
5
import java.util.*;
public class while2{
public static void main(String[]args){
int x = 0;
while (x <= 4)
{
x++;
System.out.println(x);
}
}}
output:
1
2
3
4
5
The first and second program has the same output, but their source code are not the same. The value of x on the first program starts at one then ends at five and it has 1,2,3,4,5 output. The value of x on the second program starts at zero then ends at four and then it also has 1,2,3,4,5 output. The difference in these two programs is, in the first one it displays first the value of x before it increments (x++). But on the second program, the increment (x++) was added before it displayed the value of x.
import java.util.*;
public class while3{
public static void main(String[]args){
int x = 5;
while (x > 0)
{
System.out.println(x);
x--;
}
}}
output:
5
4
3
2
1
These last program, the output is opposite from the first and second program. It starts at five and ends at one. Its all because of the decrement (x--), unlike the first two programs we increment (x++) its value.
So, here's a very simple example of "while" loop. Programming is more on logic, so just try to analyze the logic of this following programs.
import java.util.*;
public class while1{
public static void main(String[]args){
public class while1{
public static void main(String[]args){
int x = 1;
while (x <= 5)
{
System.out.println(x);
x++;
}
}}
while (x <= 5)
{
System.out.println(x);
x++;
}
}}
output:
1
2
3
4
5
import java.util.*;
public class while2{
public static void main(String[]args){
int x = 0;
while (x <= 4)
{
x++;
System.out.println(x);
}
}}
output:
1
2
3
4
5
The first and second program has the same output, but their source code are not the same. The value of x on the first program starts at one then ends at five and it has 1,2,3,4,5 output. The value of x on the second program starts at zero then ends at four and then it also has 1,2,3,4,5 output. The difference in these two programs is, in the first one it displays first the value of x before it increments (x++). But on the second program, the increment (x++) was added before it displayed the value of x.
import java.util.*;
public class while3{
public static void main(String[]args){
int x = 5;
while (x > 0)
{
System.out.println(x);
x--;
}
}}
output:
5
4
3
2
1
These last program, the output is opposite from the first and second program. It starts at five and ends at one. Its all because of the decrement (x--), unlike the first two programs we increment (x++) its value.
Friday, May 27, 2011
Learning Java 08 - Condition Using "||" and "&&"
Note: ("||" = or) , ("&&" = and) "and" is very useful in ranging numbers while "or" can use in either the option you were about to state. The program below is about grade classifications and we used "and" condition here in classifying the grades.
here's the classifications:
Okay, here's another example. But this program is using "or" condition. The instruction in this program is enter the last number of year you were born. Then it will classify you in which group you belong, group 1 or group2. If the last number of year you were born is odd, you will be in group 2. But, if it's even number, you'll be in group 1.
here's the classifications:
A-type = 88 and above
B-type = 82-87
C-type = 75-81
D-type = 74 and belowimport java.util.*;
public class andor{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your grade: ");
int grade = genioler.nextInt();
if (grade < 75){
System.out.println("D-type");
}
else if (grade < 82 && grade >= 65){
System.out.println("C-type");
}
else if (grade < 88 && grade >= 82){
System.out.println("B-type");
}
else {
System.out.println("A-type");
}
}}
public class andor{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your grade: ");
int grade = genioler.nextInt();
if (grade < 75){
System.out.println("D-type");
}
else if (grade < 82 && grade >= 65){
System.out.println("C-type");
}
else if (grade < 88 && grade >= 82){
System.out.println("B-type");
}
else {
System.out.println("A-type");
}
}}
Okay, here's another example. But this program is using "or" condition. The instruction in this program is enter the last number of year you were born. Then it will classify you in which group you belong, group 1 or group2. If the last number of year you were born is odd, you will be in group 2. But, if it's even number, you'll be in group 1.
import java.util.*;
public class andor2{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter the last number of the year you were born: ");
int num = genioler.nextInt();
if (num==0 || num==2 || num==4 || num==6 || num==8){
System.out.println("You are in group 1");
}
else {
System.out.println("You are in group 2");
}
}}
public class andor2{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter the last number of the year you were born: ");
int num = genioler.nextInt();
if (num==0 || num==2 || num==4 || num==6 || num==8){
System.out.println("You are in group 1");
}
else {
System.out.println("You are in group 2");
}
}}
Sunday, April 10, 2011
Learning Java 07 - Condition "else if"
Condition "else if" is used when the option in the condition you set is more than two. If there is only two option, you shouldn't be using "else if" anymore, just use "if-then-else". The program below shows an example on how to use "else if".
import java.util.*;
public class condition2{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = genioler.nextInt();
if (age < 18){
System.out.println("You are not allow to enter");
}
else if (age > 60){
System.out.println("You are not allow to enter");
}
else {
System.out.println("You are allow to enter");
}
}}
if (age < 18) - This is the first condition, if the user input a value below 18, then this program will run.
else if (age > 60) - This is the second option, above 60. If the age you enter is above 60, then this program will run.
else - If the age is not belong in the first two option, then this is the program that will run.
import java.util.*;
public class condition2{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = genioler.nextInt();
if (age < 18){
System.out.println("You are not allow to enter");
}
else if (age > 60){
System.out.println("You are not allow to enter");
}
else {
System.out.println("You are allow to enter");
}
}}
if (age < 18) - This is the first condition, if the user input a value below 18, then this program will run.
else if (age > 60) - This is the second option, above 60. If the age you enter is above 60, then this program will run.
else - If the age is not belong in the first two option, then this is the program that will run.
Friday, April 8, 2011
Learning Java 06 - Condition "if-then-else"
Here's a simple program about condition using "if" then followed by "else".
import java.util.*;
public class condition1{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = genioler.nextInt();
if (age <= 17){
System.out.println("You are not allow to enter");
}
else {
System.out.println("You are allow to enter");
}
}
}
Note:
( == ) - equal
( > ) - greater than
( < ) - less than
( >= ) - greater than or equal
( <= ) - less than or equal
( != or <>) - not equal
( && ) - and
( || ) - or
import java.util.*;
public class condition1{
public static void main(String[]args){
Scanner genioler = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = genioler.nextInt();
if (age <= 17){
System.out.println("You are not allow to enter");
}
else {
System.out.println("You are allow to enter");
}
}
}
Note:
( == ) - equal
( > ) - greater than
( < ) - less than
( >= ) - greater than or equal
( <= ) - less than or equal
( != or <>) - not equal
( && ) - and
( || ) - or
Labels:
condition java,
condition program,
else,
genioler,
if,
if condition,
if then else,
import java,
java condition,
java if,
java language,
java programming,
learning java,
programming,
study java
Wednesday, April 6, 2011
Learning Java 05 - Double
In java programming language, double is an variable that is responsible in holding decimal numbers. So use "int" for whole numbers and use "double" for decimal numbers. Here's a simple program that uses double.
import java.util.*;
public class double1{
public static void main(String[]args){
double burprice = 26.50; //burger price
double sofprice = 10.75; //softdrink price
double canprice = 2.25; //candy price
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of burger: ");
int numbur = scan.nextInt();
System.out.print("Enter number of softdrink: ");
int numsof = scan.nextInt();
System.out.print("Enter number of candy: ");
int numcan = scan.nextInt();
double totbur = numbur * burprice;
double totsof = numsof * sofprice;
double totcan = numcan * canprice;
double total = totbur + totsof + totcan;
System.out.println("Total: "+total);
}
}
output:
Enter number of burger: 3 //example you input 3
Enter number of softdrink: 5 //5 here
Enter number of candy: 4 //and 4 here
Total: 142.25 //then it will display the total amount.
Here's how to set the digit of the decimal numbers. BTW if you set it into two digit or in any decimal digit numbers, it will automatically round off.
import java.util.*;
public class double1{
public static void main(String[]args){
double burprice = 26.50; //burger price
double sofprice = 10.75; //softdrink price
double canprice = 2.25; //candy price
Scanner scan = new Scanner(System.in);
System.out.print("Enter number of burger: ");
int numbur = scan.nextInt();
System.out.print("Enter number of softdrink: ");
int numsof = scan.nextInt();
System.out.print("Enter number of candy: ");
int numcan = scan.nextInt();
double totbur = numbur * burprice;
double totsof = numsof * sofprice;
double totcan = numcan * canprice;
double total = totbur + totsof + totcan;
System.out.println("Total: "+total);
}
}
output:
Enter number of burger: 3 //example you input 3
Enter number of softdrink: 5 //5 here
Enter number of candy: 4 //and 4 here
Total: 142.25 //then it will display the total amount.
Here's how to set the digit of the decimal numbers. BTW if you set it into two digit or in any decimal digit numbers, it will automatically round off.
import java.text.*;
public class double2 {
public static void main(String[] args) {
double x = 1.235567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(x));
}}
public class double2 {
public static void main(String[] args) {
double x = 1.235567;
DecimalFormat df = new DecimalFormat("#.##");
System.out.print(df.format(x));
}}
Monday, April 4, 2011
Learning Java 04 - Computation
In java, you can create a simple computation program. Like the program below, this program will ask the user to input his age. Then it will display his age and automatically compute his age five years from now.
import java.util.*;
public class computation1{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scan.nextInt();
System.out.println("\nYour age is "+ age);
int fiage = age + 5;
System.out.println("Your age 5 years from now is "+fiage);
}
}
int fiage = age + 5; - The statement which compute your age five years from now."fiage" is the label and "age" is the age the user entered.
(" ...... "+fiage); - Note: this the proper way to display any variable.
(fiage+"........."); - If the variable is before any statement.
("..."+fiage+"..."); - If the variable is in between two statement.
Here's another program:
example: 50/liter
import java.util.*;
public class computation2{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Number of Liter: ");
int lit = scan.nextInt();
int total = lit * 50;
System.out.print("\nTotal: "+ total);
System.out.print("\nAmount Received: ");
int amtrec = scan.nextInt();
int chnge = amtrec - total;
System.out.println("Change: "+ chnge);
}
}
output:
//example you purchase 5 liters.
Number of liters: 5
//then it will compute and display the total amount.
Total: 250
//example you give 300 for payment.
Amount Received: 300
//then it will also automatically compute and display the change.
Change: 50
import java.util.*;
public class computation1{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Enter your age: ");
int age = scan.nextInt();
System.out.println("\nYour age is "+ age);
int fiage = age + 5;
System.out.println("Your age 5 years from now is "+fiage);
}
}
output:
//enter your age here, example 20.
Enter your age: 20
//then program will display the age you entered.
Your age is 20
//and then it will automatically compute and display your age.
Your age 5 years from now is 25
int fiage = age + 5; - The statement which compute your age five years from now."fiage" is the label and "age" is the age the user entered.
(" ...... "+fiage); - Note: this the proper way to display any variable.
(fiage+"........."); - If the variable is before any statement.
("..."+fiage+"..."); - If the variable is in between two statement.
Here's another program:
example: 50/liter
import java.util.*;
public class computation2{
public static void main(String[]args){
Scanner scan = new Scanner(System.in);
System.out.print("Number of Liter: ");
int lit = scan.nextInt();
int total = lit * 50;
System.out.print("\nTotal: "+ total);
System.out.print("\nAmount Received: ");
int amtrec = scan.nextInt();
int chnge = amtrec - total;
System.out.println("Change: "+ chnge);
}
}
output:
//example you purchase 5 liters.
Number of liters: 5
//then it will compute and display the total amount.
Total: 250
//example you give 300 for payment.
Amount Received: 300
//then it will also automatically compute and display the change.
Change: 50
Subscribe to:
Comments (Atom)