RU beehive logo ITEC dept promo banner
ITEC 120
2019fall
asbrennem
ibarland

loops
and RE-assigning to variables

Preview: We'll talk a while, leading up to a while-loop, like

    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.
  

re-assigning to a variabe

      String name;       // declare the variable
      name = "ibarland"; // initialize the variable
      
      name = "hello, " + name + "!";   // RE-assigning to the variablek
    
When we re-assign to a variable, the previous value is entirely replaced! Maybe it's more like a mail-slot than a bucket: when you place a new value into the slot, it pushes the old one out.

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
      
      name = "hello, " + name + "!"; // this makes the variable-name a LIE!
      String greeting = "hello, " + name + "!";  //  a descriptive variable name
    

Consider the problem of appending “.........” to a chapter-title, perhaps for use in a table of contents. We want to keep adding "." until the title&dots are 40 characters long altogether.

    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;
    }

    // This copy-pasting is annoying, because...

    String toc4; // the toc-line, with up to 4 "." included.
    if (length(toc3) < 40) {
        toc4 = toc3 + ".";
    }
    else {
        toc4 = toc3;
    }


    String toc5; // the toc-line, with up to 5 "." included.
    if (length(toc4) < 40) {
        toc5 = toc4 + ".";
    }
    else {
        toc5 = toc4;
    }

    // ...because I have to re-name each variable!

    String toc6; // the toc-line, with up to 6 "." included.
    if (length(toc5) < 40) {
        toc6 = toc5 + ".";
    }
    else {
        toc6 = toc5;
    }

    // Please, make it stop!
    

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 tocn holds the current notion of the evolving table-of-contents line. We might say, it's the tocSoFar. …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 {
        tocSoFar = tocSoFar;    // hmm, this is kinda pointless.  We can omit the 'else'.
    }

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    // Okay, the copy-paste is now easy, when we re-assign to the variable `tocSoFar`.

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }
    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    // But still,

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }
    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }

    // How much is enough?
    if (length(tocSoFar) < 40) {
        tocSoFar = tocSoFar + ".";
    }
    

Syntax update: The else clause that is just “/* do nothing */” is admittedly useless. So, an empty else 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 an if initializes a var, or returns, 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 */ }”.

while

Java has a special keyword, which means “take the limit of the n if statements, as n → ∞”. That is, keep repeating the if however many times is necessary, until the condition becomes false:

    while (condition) {
        expression
    }
    
So the above example becomes:
    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 is toc?
  1. 39 characters long
  2. 40 characters long
  3. 41 characters long

It can be helpful to use println to see what is happening as our program runs:

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.


logo for creative commons by-attribution license
This page licensed CC-BY 4.0 Ian Barland
Page last generated
Please mail any suggestions
(incl. typos, broken links)
to ibarlandradford.edu
Rendered by Racket.