![]() |
![]() |
|
Preview:
We'll talk a while, leading up to a
String chapterTitle = "An Unexpected Party"; // task: Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String toc = chapterTitle + " "; while (length(toc) < 40) { toc = toc + "."; } // after finishing the loop, `toc` is at least 40 chars long. |
String name; // declare the variable name = "ibarland"; // initialize the variable name = "hello, " + name + "!"; // RE-assigning to the variablek |
Problem: What does the above variable hold? The name, or a greeting? It would have been better to create a new variable with a descriptive name:
String name; // declare the variable name = "ibarland"; // initialize the variable |
Consider the problem of appending “.........” to a chapter-title, perhaps for use in a table of contents.
We want to keep adding
String chapterTitle = "An Unexpected Party"; // Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String toc0 = chapterTitle + " "; String toc1; // the toc-line, with up to 1 "." included. if (length(toc0) < 40) { toc1 = toc0 + "."; } else { toc1 = toc0; } String toc2; // the toc-line, with up to 2 "." included. if (length(toc1) < 40) { toc2 = toc1 + "."; } else { toc2 = toc1; } String toc3; // the toc-line, with up to 3 "." included. if (length(toc2) < 40) { toc3 = toc2 + "."; } else { toc3 = toc2; } |
The copy/pasting would be easier, if we didn't have to use a new variable-name each time.
And heck, the idea is that each
.
…Hey, that's a name that can be re-assigned to;
it's name makes it clear that its value is in-progress, so it's not so much of a lie:
String chapterTitle = "An Unexpected Party"; // Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String tocSoFar = chapterTitle + " "; if (length(tocSoFar) < 40) { tocSoFar = tocSoFar + "."; } else { |
Syntax update: Theelse clause that is just “/* do nothing */ ” is admittedly useless. So, an emptyelse branch can be omitted. We'll call this a “one-armed if”.However, in this course we'll do this only when re-assigning to a variable; we'll continue our existing rule:
If one branch of anif initializes a var, orreturn s, then the other branch should do likewise (and shouldn't be omitted!). It's only when we re-assign to already-initialized variables, that we even reach a situation where we'd want/need “else { /* do nothing */ } ”.
Java has a special keyword, which means
“take the limit of the n
while (condition) { expression… } |
String chapterTitle = "An Unexpected Party"; // task: Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String toc = chapterTitle + " "; while (length(toc) < 40) { toc = toc + "."; } |
Question: When this loop finishes, how long istoc ?
- 39 characters long
- 40 characters long
- 41 characters long
It can be helpful to use
class Lect04c { void demoLoop() { String chapterTitle = "An Unexpected Party"; // task: Create the string "An Unexpected Party ..................." which is exactly 40 chars long // for use in a table-of-contents ("toc") String tocSoFar = chapterTitle + " "; while (length(tocSoFar) < 40) { tocSoFar = tocSoFar + "."; System.out.println( "debug: tocSoFar is " + length(tocSoFar) + " chars long: " + tocSoFar ); } System.out.println("After loop finishes:"); System.out.println( "tocSoFar is " + length(tocSoFar) + " chars long: " + tocSoFar ); } } |
We finish by observing that this example is begging to be made into a function. What should the parameter(s) be? What should they be named? See my solution.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |