Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Sample Code

 

 

Example 1

for Loops

Example 2

Nested for Loops

Example 3

while Loops

Example 4

Using break Statement

Example 5

Triangles

 

Mystery

 

Example 1

for Loops

 

public class Iterations

{

      public static void main(String[] args)

      {

            for(int rows = 1; rows <6;rows++)

                  System.out.println("Printing row number " + rows);

      }

}

 

Output

 

Printing row number 1

Printing row number 2

Printing row number 3

Printing row number 4

Printing row number 5

 

Example 2

Nested for Loops

 

public class Iterations

{

      public static void main(String[] args)

      {

            for(int rows = 1; rows <6;rows++)

                  for (int columns = 1; columns < 5; columns++)

                  {                            

                        System.out.print("x");

                        if (columns % 4 == 0)

                        {                            

                              System.out.println();

                        }    

                  }    

      }

}

 

Output

 

xxxx

xxxx

xxxx

xxxx

xxxx

 

 

Example 3

while Loops

 

public class Iterations

{

      public static void main(String[] args)

      {

            int age = 14;

            while (age < 18)

            {    

                  System.out.println ("You cannot vote");

                  age = age + 1;

            }    

 

      }

 

}

 

Output

 

You cannot vote

You cannot vote

You cannot vote

You cannot vote

 

 

Example 4

Using break Statement

 

public class Iterations

{

      public static void main(String[] args)

      {

                  int sentinel = 0;

                  for (int i = 1; i < 6; i++)

                  {

                        if (i % 2 == 0)

                        {

                              sentinel = i;

                              break;

                        }

                  }

                  System.out.println("First even number is " + sentinel);

      }                

 

}

 

Output

 

First even number is 2

 

 

Example 5

Triangles

 

public class Triangles

{

      public static void main (String[]args)

      {

            for (int rows2 = 1; rows2 < 9 ;rows2++)

            {

                  for (int columns2 = 1; columns2<(rows2+1);columns2++)

                  {

                        System.out.print("a");

                  }

                  System.out.println();

            }

            for (int rows3 = 1; rows3 < 9 ;rows3++)

            {

                  for (int columns3= 1; columns3<(10-rows3);columns3++)

                  {

                        System.out.print("b");

                  }

                  System.out.println();

            }

            for (int rows3 = 1; rows3 < 9 ;rows3++)

            {

                  for (int columns3= 1; columns3<(10-rows3);columns3++)

                  {

                        System.out.print(" ");

                  }

                  System.out.print("c");

                  System.out.println();

            }

            for (int rows1 = 1; rows1 < 9 ;rows1++)

            {

                  for (int columns1 = 1; columns1<(rows1+1);columns1++)

                  {

                        System.out.print(" ");

                  }

                  System.out.print("d");

                  System.out.println();

            }

      }

}

 

Output













































Output

a

aa

aaa

aaaa

aaaaa

aaaaaa

aaaaaaa

aaaaaaaa

bbbbbbbb

bbbbbbb

bbbbbb

bbbbb

bbbb

bbb

bb

b

        c

       c

      c

     c

    c

   c

  c

 c

 d

  d

   d

    d

     d

      d

       d

        d