Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle
2 Repetition Structures A repetition structure is used when the same action(s) are to be executed repeatedly. There are several types: A repetition structure is used when the same action(s) are to be executed repeatedly. There are several types: while while for for do-while do-while
3 Repetition Structures while while Usage: Usage: Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Format: while (Condition) { // Braces are only needed when there // are multiple statements Statement(s) } Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 } Example: int counter = 0; while (counter < 100) { // Loops as long as counter is less than 100 out.writeln(counter); // Display counter counter = counter + 1; // Increment counter by 1 }
4 Repetition Structures for for Usage: Usage: "Does it all", so to speak "Does it all", so to speak Used to specify that an action is to be repeated as long as some condition remains true Used to specify that an action is to be repeated as long as some condition remains true May never execute if the result of the initial test is false May never execute if the result of the initial test is false Often used to specify that an action is to be repeated a pre-determined number of times Often used to specify that an action is to be repeated a pre-determined number of times Format: for (LCV Initialization; Condition; LCV Update ) { // LCV stands for “loop control variable.” Statement(s) // Braces are only needed when there are multiple statements } Format: for (LCV Initialization; Condition; LCV Update ) { // LCV stands for “loop control variable.” Statement(s) // Braces are only needed when there are multiple statements } Example: int counter; for (counter = 0; counter < 100; counter++) // Loops as long as counter is less than 100 out.writeln(counter); // Display counter Example: int counter; for (counter = 0; counter < 100; counter++) // Loops as long as counter is less than 100 out.writeln(counter); // Display counter
5 Constants A constant is a variable that must be initialized with a constant expression when it is declared and which cannot be modified thereafter (within the program). A constant is a variable that must be initialized with a constant expression when it is declared and which cannot be modified thereafter (within the program). The syntax used to identify a variable as a constant in Java is: public static final Type Variable = Constant ; The syntax used to identify a variable as a constant in Java is: public static final Type Variable = Constant ; Example: public static final int base = 2; Example: public static final int base = 2; The word public means that there are no restrictions on where the name base can be used The word public means that there are no restrictions on where the name base can be used The word final means that 2 is the final value assigned to base The word final means that 2 is the final value assigned to base We’ll address the meaning of the word static later. We’ll address the meaning of the word static later.
6 Constants Why use constants? Why use constants? A constant is useful when you need to use the same value repeatedly within a program. A constant is useful when you need to use the same value repeatedly within a program. One advantage to using a constant is that if you have to change it, you only have to change it one place in the program. One advantage to using a constant is that if you have to change it, you only have to change it one place in the program.
7 Sample Programs Now let’s look at some sample programs that demonstrate the use of for and while loops and constants: Now let’s look at some sample programs that demonstrate the use of for and while loops and constants: twoPowers.java twoPowers.java twoPowers.java twoPowersWithFor.java twoPowersWithFor.java twoPowersWithFor.java