Home Robotics C++ Physics II AP Physics B Electronics AP Java Astronomy Independent Study Summer Session Contests  About
                                                       

Control Structures

 

 

while

   

class WhileLoops

{

  public static void main(String[] args)

  {

    int temperature = 50;

    while (temperature < 55)

    {

      System.out.println("Temperature is less than 55");

      temperature = temperature + 1;

    }

  }

}

 

 

Pre and Post Increment

 
 
public class Increment
{
   public static void main( String args[] )
   {
      int c;
  
      c = 5;
      System.out.println( c );           // print 5
      System.out.println( c++ );      // print 5 then postincrement
      System.out.println( c );          // print 6
 
      System.out.println();      // skip a line
 
      c = 5;
      System.out.println( c );        // print 5
      System.out.println( ++c );   // preincrement then print 6
      System.out.println( c );       // print 6
   }
}
 

 

for

 

 
import java.awt.Graphics;
import javax.swing.JApplet;
 
public class ForCounter extends JApplet
{
   public void paint( Graphics g )
   {
      // Initialization, repetition condition and incrementing
      // are all included in the for structure header.
      for ( int counter = 1; counter <= 10; counter++ )
         g.drawLine( 10, 10, 250, counter * 10 );
   }
}
 
 
// Counter-controlled repetition with the for structure
import javax.swing.JOptionPane;
 
import javax.swing.*;
public class Sum
{
   public static void main( String args[] )
   {
      int sum = 0;
 
      for ( int number = 2; number <= 100; number += 2 )
         sum += number;
 
      JOptionPane.showMessageDialog( null,
         "The sum is " + sum,
         "Sum Even Integers from 2 to 100",
         JOptionPane.INFORMATION_MESSAGE );
 
      System.exit( 0 );   // terminate the application
   }
}

 
 
// Calculating compound interest
import java.text.DecimalFormat;
import javax.swing.JOptionPane;
import javax.swing.JTextArea;
 
public class Interest
{
   public static void main( String args[] )
   {
      double amount, principal = 1000.0, rate = .05;
 
      DecimalFormat precisionTwo = new DecimalFormat( "0.00" );
      JTextArea outputTextArea = new JTextArea( 11, 20 );
 
      outputTextArea.append( "Year\tAmount on deposit\n" );
 
      for ( int year = 1; year <= 10; year++ )
      {
         amount = principal * Math.pow( 1.0 + rate, year );
         outputTextArea.append( year + "\t" +
            precisionTwo.format( amount ) + "\n" );
      }
 
      JOptionPane.showMessageDialog(
         null, outputTextArea, "Compound Interest",
         JOptionPane.INFORMATION_MESSAGE );
 
      System.exit( 0 );  // terminate the application
   }
}

 

do while

 

 

class WhileLoops

{

  public static void main(String[] args)

  {

    int temperature = 50;

    do

    {

      System.out.println("Temperature is less than 55");

      temperature = temperature + 1;

    }

    while (temperature < 55);

  }

}

 

 

if-else

 


if (Boolean)

{

    statement;

}

else

{

    statement;

}