java prepared by gary langner types of loops u for loop (entrance controlled) –do an action for a...
TRANSCRIPT
JavaJavaPrepared by Gary Langner
Types of Loops
for loop (entrance controlled)
– do an action for a definite number of times
while loop (entrance controlled
– do an action while a condition is true do…..while (exit controlled)
– do an action until a condition is true
Boolean Operators= = Is equal to
< Is less than
> Is greater than
<= Is less than orequal to
>= Is grater than orequal to
!= Is not equal to
|| Or
&& And
! Not
For Loop (with single statement)
For (<Initialization expression>;< termination condition>; <update expression>)
for (i = 1; i <= 10; i++)
System.out.println(i);
1 <---------------------------Output2345678910
No semicolon!
No braces “{“ on single statement
For Loop (with multiple statement)
Int amt, num,total1=0, total2=0;
for (int j=1; j <= 10; j++){ //begin for loop amt = SavitchIn.readInt(); // or Scanner class num = SavitchIn.readInt(); total1+= amt; //accumulator statement adds up all amt and stores answer in total1 total2+= num; //accumulator statement adds up all num and stores answer in total2
System.out.println( “The number is” + EasyFroamt.format (num,6));} //end for loop
Note: the indenting for readability
Note: the bracket alignment
Loop Body
For Loop (downward counting loop)
for (int k = 20; k >= 15; --k)
System.out.print( “K = “+EasyFormat.format (k,9);
For Loop (counting by more than one)
int sum = 0;
//add the EVEN numbers 2-10
for (int j = 2; j <=10; j += 2)
sum+ = j;
System.out.println(“sum “)
For Loop (style tips)
//loop limits can be defined as constants or as variables and //then have assigned values
final int LOOPLIMIT = 500;
int lcv; //allows reuse in the program
for (lcv = 1; lcv <= LOOKLIMIT; lcv++)
//loop body
int loopLimit;
loopLimit=SavitchIn.readInt()OR
While Loop
While (boolean expression)
loop statement;
Entrance controlled (pretest)
While <Boolean expression>
Statements after loop
Loop BodyTrue
False
While LoopOften used for sentinel value (terminal value)
The boolean expression MUST have a value prior to the loop
Loop control variable must change IN THE loop and become TRUE (otherwise infinite loop)
power2 = 1;while (power2 < 100){ //begin loop
System.out.println( power2);power2*=2;} //end loop
see next page
No semicolon
Multiplies power2 by 2 with each iteration
While Loop (sentinel value)
int numscores = 0;int sum = 0;int score;System.out.print( “Enter a score (-999 to quit) ”);int score = SavitchIn.readInt(); //or scanner classwhile (score != -999){
numscores++; //simple counter (counts by 1’s)sum += score; //adds up all scores and stores in sumSystem.out.print( “Enter a score (-999 to quit) ”);score=SavitchIn.readInt() //or scanner class
} //end whileSystem.out.print(“sum of scores= “) + sum);
Double Prompt and input required
While Loop (compound conditions)
int a=SavitchIn.readInt();
int b=SavitchIn.readInt();
while ((a > 0) && (b>0))
{
statements;
} //end while
Do Loop Post test or entrance controled loop
Do body of loop
While<boolean
expression>
True
False
Do LoopUsed when it is necessary to loop at least once.
The boolean expression MUST have a value before it is used at the end of the loop.
Loop control variable must change IN THE loop and become FALSE (otherwise infinite loop)
Generally used less frequently except for data validation.
j=0;do{
j+=2;System.out.println( j);
} while (j != 5);
No semicolon
next pg
BAD! j must become five for loop to terminate. This is infinite—Must Ctl Alt Del to stop
Nested Loops
for (int k = 1; k <=5; ++k){ for (int j = 1; j <=3; ++j)
System.out.print(EasyFormat.format((k+j),4));System.out.println();;
} //end for loop2 3 4
3 4 54 5 65 6 76 7 8
output
Loops-writing style Each loop should have its own level of indenting. The { and } should start in the same column Comments should describe what a loop does.
(place inside of loop) Comments should be used at end of loop i.e..
//end of for loop Isolate loops by skipping a line before and after
the loop.
Now on to some programming practice...
Go back to the assignments page and download the first looping program. Good Luck!