Programming advice o..

Download Report

Transcript Programming advice o..

Programming advice on using the
while-statement
Infinite loop
• Facts:
• The while-statement is very powerful
• With more power, comes more responsibility, because
• You can create a never ending program using
loop statements !!!
Infinite loop (cont.)
• Infinite loop:
• Infinite loop = a loop statement that does not end
Infinite loop (cont.)
• Example:
public class While02
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 )
// While-statement
{
System.out.println(a); // Print a
// NO a++ statement !!!
}
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Infinite loop (cont.)
• Output:
1
1
....
(never ending printing of 1's)
Infinite loop (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/
While02.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac While02.java
• To run:
java While02
Infinite loop (cont.)
• Note:
• You must type control-C in the terminal window to
terminate the Java program when you do run it !
Prime directive of the while-statement
• Very important programming rule:
• The while-body must contain statements that will
affect (change) the outcome of the loop-continuationcondition of the while-loop !!!
Otherwise, you will certainly have an infinite loop in the
program.
Common errors with while-loops
• Common error 1: bogus semicolon
Example:
public class Error01
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 ) ;
// BOGUS semicolon !!
{
System.out.println(a); // Print a
a++;
// Increment a
}
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Common errors with while-loops (cont.)
• The program is read syntactically as follows:
public class Error01
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 )
;
// While body contains no statements
{
System.out.println(a); // Print a
a++;
// Increment a
}
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Common errors with while-loops (cont.)
• Results:
• The program contains an infinite loop that keep
testing the loop-continuation-condition
The program will run forever but will not make any
progress !!!
Common errors with while-loops (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/
Error01.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac Error01.java
• To run:
java Error01
Use control-C to terminate the program !!!
Common errors with while-loops (cont.)
• Common error 2: forgetting to use statement block
Example:
public class Error02
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 )
System.out.println(a); // Print a
a++;
// Increment a
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Common errors with while-loops (cont.)
• The program is read syntactically as follows:
public class Error02
{
public static void main(String[] args)
{
int a;
a = 1;
while ( a <= 10 )
System.out.println(a); // Body of while-loop
a++;
// Outside the while loop !!
System.out.println("Done");
System.out.println("Exit: a = " + a);
}
}
Common errors with while-loops (cont.)
• Result:
• The program contains an infinite loop that repeatedly
prints 1, 1, 1, ...
Common errors with while-loops (cont.)
• Example Program: (Demo above code)
– Prog file:
http://mathcs.emory.edu/~cheung/Courses/170/Syllabus/07/Progs/
Error02.java
• How to run the program:
• Right click on link and save in a scratch directory
• To compile: javac Error02.java
• To run:
java Error02
Use control-C to terminate the program !!!