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.

import java.util.*;
public class while1{
     public static void main(String[]args){
     int x = 1;
    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:
A-type = 88 and above
B-type = 82-87
C-type = 75-81
D-type = 74 and below

import 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");
    }      
}}

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");
    }         
}}