Introduction to Software Development
GWU Computer Science
If you do not have your Codio course ready, use any text editor or simple IDE. Some possibilities are:
Before we move forward, let's catch up (complete any remaining work from the previous module)
In this case, make sure we've got:
Boolean values are true or false. And we'll be using them often in a few weeks.
Since these are a new type of values, then we need another variable type to store them.
That variable type is calle boolean, and it can store either true or false.
in Java, we declare a boolean variable like this:
boolean myBool;
myBool = false;
myBool = true;
boolean myBool = true;
Conditions refer to questions that can be asked and that result in either true or false. In other words:
a Condition evaluates to a boolean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | public class PrintCountdown { public static void main(String args[]) { // Set up an initial count value double count = 10; // Now print and decrease it one by one System.out.println(count--); // "--" acts AFTER we resolve the value, so it prints a 10 System.out.println(count--); // it prints a 9 System.out.println(count--); // it prints a 8 System.out.println(count--); // it prints a 7 System.out.println(count--); // it prints a 6 System.out.println(count--); // it prints a 5 System.out.println(count--); // it prints a 4 System.out.println(count--); // it prints a 3 System.out.println(count--); // it prints a 2 System.out.println(count--); // it prints a 1 System.out.println("Blastoff!"); // it prints Blastoff! } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class AwesomePrintCountdown { public static void main(String args[]) { // Set up an initial count value double count = 10; // Now print and decrease it one by one while (count > 0) { System.out.println(count--); } System.out.println("Blastoff!"); // it prints Blastoff! } } |
The new structure is:
The flow of execution for a while statement is:
Consider the following program and figure out if there is some sort of potential issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class ForAWhile1 { public static void main(String args[]) { // Set up an initial count value double count = 10; // Now print and decrease it one by one while (count < 0) { System.out.println(count--); } System.out.println("Blastoff!"); // it prints Blastoff! } } |
Now, look at the following program and figure out if there is some sort of potential issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class ForAWhile2 { public static void main(String args[]) { // Set up an initial count value double count =10; // Now print and decrease it one by one while (count > 0) { System.out.println(count++); } System.out.println("Blastoff!"); // it prints Blastoff! } } |
Finally, check the following program and figure out if there is some sort of potential issue:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | public class ForAWhile3 { public static void main(String args[]) { // Set up an initial count value double count = 10; // Now print and decrease it one by one while (count > 0) { System.out.println(count); } System.out.println("Blastoff!"); // it prints Blastoff! } } |
This last issue is common! Not updating the code that affects the condition may result in trouble!!!
Consider the following program:
What we would like is a way to organize repetition.
We will do this using a version of the for-loop:
Let's zoom in on the for-loop and dissect it:
More about this below.
you can read this as: "for i starting at 0, ending at 5, and incrementing by 1 do ... (whatever)"
Consider this example:
Activity 3:
Type, compile and execute the above program.
Activity 4:
In
MyForLoopPrint.java,
change the loop conditions so that the numbers 1 through 10
are printed. Also add a print statement after the
for-loop, that prints anything.
Observe:
Mental execution:
To explore for-loops further, we'll look at some variations of the basic for-loop:
for (int count=0; count<6; count++) { printBigO (); }
for (int i=10; i<16; i++) { printBigO (); }
for (int i=1; i<=5; i++) { printBigO (); }
for (int i=0; i<6 i=i+1) { printBigO (); } for (int i=0; i<6; i+=1) { // Using the += operator. printBigO (); }Activity 5: Write a program to print out the odd numbers from 1 to 25. Use a single for-loop with the appropriate increment and write your code in Odd25.java.
for (int i=5; i>0; i--) { System.out.println (i); }
We'll start by writing a program to print a little
number triangle like this:
1
22
333
4444
Notice: there's repetition across a row of numbers: a potential use of for-loops!
Here's the program, using for-loops:
Observe:
Next, observe that the upper-limits of the for-loops are themselves increasing:
Another way to say this:
When the value is 2, print a row of 2's
When the value is 3, print a row of 3's
When the value is 4, print a row of 4's
Think for next class: Can we use a for loop to automate the upper limits of the for loops themselves?