Wednesday, September 29, 2010

example of an operational loop(the "for" loop)

//Display the sum of even numbers from 1-100.

public class operationalloop
{
public static void main (String[]args)
            {
              int a,b,sum=0; //declaring variables(make sure you initialized the value of the sum for you will be                                      //using it as a container in the body of the program.
              
              for(a=2;a<=100;a+=2)
              {
                    sum=sum+a; //this is where you add up to the value of sum
/*note:
       This is how the looping goes:
       it will be read as - "(initialization) a=2, if (logical test)a<=100 then (operation) sum=sum+a, (increment) a+=2)*/
               }
              System.out.println("the sum is "+sum); //this is how to display the sum
            }
}

OUTPUT WILL BE:
the sum is 2550

Display "Hello World!"

public class helloworld
{
public static void main (String[]args)
            {
              System.out.println("Hello World!");
            }
}

OUTPUT WILL BE:
Hello World!