while LOOPS 2004 Pearson AddisonWesley All rights reserved
- Slides: 11
while LOOPS © 2004 Pearson Addison-Wesley. All rights reserved
LEARNING GOAL The student will be able to write more complex java programs using conditional structures and loops in Dr. Java, compile the source code without errors, and execute the java program. 4 *3 2 1 I can teach others to write, compile, and execute basic java programs that utilize conditional statements and loops and can determine the correct data type given a real world problem. I can write, compile, and execute basic java programs that utilize conditional statements and loops with no errors. I can write, compile, and execute basic java programs that utilize conditional statements but have difficulty with loops. I can understand the idea behind conditional statements.
Repetition Statements • Repetition statements allow us to execute a statement multiple times • Often they are referred to as loops • Like conditional statements, they are controlled by boolean expressions • Java has three kinds of repetition statements: § the while loop § the do loop § the for loop © 2004 Pearson Addison-Wesley. All rights reserved
The while Statement • A while statement has the following syntax: while ( condition ) { statement(s); } • If the condition is true, the statement is executed • Then the condition is evaluated again, and if it is still true, the statement is executed again • The statement is executed repeatedly until the condition becomes false © 2004 Pearson Addison-Wesley. All rights reserved
The while Statement • Typically adhere to the following strucuture: initialize variables while(condition ) { loop body – perform calculations; loop body – change varaibles; } In order for the loop to terminate, each iteration through the loop must move variables involved in the condition closer to satisfying the condition. © 2004 Pearson Addison-Wesley. All rights reserved
Logic of a while Loop condition evaluated true statement © 2004 Pearson Addison-Wesley. All rights reserved false
The while Statement • An example of a while statement: int count = 1; while (count <= 5) { System. out. println (count); count++; } • If the condition of a while loop is false initially, the statement is never executed • Therefore, the body of a while loop will execute zero or more times © 2004 Pearson Addison-Wesley. All rights reserved
The while Statement • Let’s Look at a Sample…. . Demo Time! © 2004 Pearson Addison-Wesley. All rights reserved
Infinite Loops • The body of a while loop eventually must make the condition false • If not, it is called an infinite loop, which will execute until the user interrupts the program • This is a common logical (semantic) error • You should always double check the logic of a program to ensure that your loops will terminate normally. • It is NOT good to make these types of loops on the computer. © 2004 Pearson Addison-Wesley. All rights reserved
Infinite Loops • An example of an infinite loop: int count = 1; while (count <= 25) { System. out. println (count); count = count - 1; } • This loop will continue executing until interrupted (Control-C) or until an underflow error occurs © 2004 Pearson Addison-Wesley. All rights reserved
Infinite Loops • Another example of an infinite loop: Don’t do this! It will always be true. while (4 > 2) { System. out. println (“I am ruining the computer!”); } • This loop will continue executing until interrupted (Control-C) or until an underflow error occurs © 2004 Pearson Addison-Wesley. All rights reserved
- Pearson education inc all rights reserved
- Pearson education inc. all rights reserved
- 2010 pearson education inc
- Copyright 2010 pearson education inc
- Pearson education inc. all rights reserved
- Specification by example
- Copyright 2015 all rights reserved
- All rights reserved sentence
- Freesound content licence
- Confidential all rights reserved
- Sentinel controlled repetition
- Copyright © 2015 all rights reserved