Spring_2008_Iterating_in_Java_MATLAB_and_Python_to_Java_Tutorial

Spring_2008_Iterating_in_Java_MATLAB_and_Python_to_Java_Tutorial

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Spring 2008 CS 1316: Representing Structure and Behavior Dawn Finney 1 Iterating in Java: MATLAB and Python to Java Tutorial Introduction to for and while Loop Structure in Java The general while loop structure for Java adheres to the following form: INITIAL-STATEMENT while( CONTINUING-CONDITION ) ITERATIVE-STATEMENT The general for loop structure for Java adheres to the following form: for ( INITIAL-STATEMENT ; CONTINUING-CONDITION ; ITERATIVE-STATEMENT ) The INITIAL-STATEMENT section is evaluated once before the loop begins and is usually where a counter variable is declared and initialized. The CONTINUING-CONDITION section typically contains at least one boolean statement usually regarding the counter variable and is evaluated before each iteration begins. The ITERATIVE-STATEMENT section is evaluated at the end of each iteration and often contains a statement iterating the counter variable. Though the for loop is merely a modified form of the while loop, by convention and ease they are used to achieve different purposes. A for loop is used when the exact number of iterations in known, while a while loop is used when the number of iterations is dependent upon satisfying some conditional. For instance, a program simulating a person eating until he is full would probably use a while loop, because the person must continue eating until the condition of being full is satisfied. However, if another person decided that she would only eat
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 3

Spring_2008_Iterating_in_Java_MATLAB_and_Python_to_Java_Tutorial

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online