From 8f445bd73a951d9b59d37e8b1b34ee97c9ce7267 Mon Sep 17 00:00:00 2001 From: Martin Goik <goik@hdm-stuttgart.de> Date: Thu, 28 Sep 2017 13:26:32 +0200 Subject: [PATCH] figure loop calculation, nested loops --- Doc/Sd1/statements.xml | 131 ++++++++++++++++++++++++++++++++++++----- 1 file changed, 116 insertions(+), 15 deletions(-) diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 91ce12449..9cf8c01a9 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -1481,23 +1481,23 @@ Do not copy!</screen></td> <calloutlist> <callout arearefs="sd1_callout_whileLoop-1-co" xml:id="sd1_callout_whileLoop-1"> - <para>Will be repeated this number of times</para> + <para>The code block will be repeated this number of times.</para> </callout> <callout arearefs="sd1_callout_whileLoop-2-co" xml:id="sd1_callout_whileLoop-2"> - <para>Helper variable keeping track of repetitions</para> + <para>Helper variable keeping track of repetitions.</para> </callout> <callout arearefs="sd1_callout_whileLoop-3-co" xml:id="sd1_callout_whileLoop-3"> - <para>Condition to be checked before each new execution.</para> + <para>Condition to be checked prior each execution.</para> </callout> <callout arearefs="sd1_callout_whileLoop-4-co" xml:id="sd1_callout_whileLoop-4"> - <para>Statement(s) to be repeated.</para> + <para>Code block of statement(s) to be repeated.</para> </callout> <callout arearefs="sd1_callout_whileLoop-5-co" xml:id="sd1_callout_whileLoop-5"> - <para>Increment helper variable each iteration.</para> + <para>Helper variable being incremented during each iteration.</para> </callout> </calloutlist> @@ -1712,6 +1712,27 @@ while (i < limit) { </qandadiv> </qandaset> + <figure xml:id="sd1_fig_nestedLoops"> + <title>Nested loops</title> + + <informaltable border="0"> + <tr> + <td><programlisting language="java">for (int i = 0; i < 6; i++) { + for (int j = 0; j < i; j++) { + System.out.print(i + j + " "); + } + System.out.println(); // newline +}</programlisting></td> + + <td><screen>1 +2 3 +3 4 5 +4 5 6 7 +5 6 7 8 9 </screen></td> + </tr> + </informaltable> + </figure> + <qandaset defaultlabel="qanda" xml:id="sd1QandaXmasTree"> <title>Merry Xmas</title> @@ -2675,9 +2696,89 @@ for (int row = 0; row < numberOfRows; row++) { </section> <section xml:id="sd1SectPlayingLottery"> - <title>Playing lottery</title> + <title>Loops and calculations</title> + + <figure xml:id="sd1_fig_loopCalculations"> + <title>Calculating values</title> + + <informaltable border="1"> + <tr> + <td><programlisting language="java">int limit = 5; +int sum = 0; + +for (int i = 1; i <= limit; i++) { + sum += i; +} + +System.out.println("0 + ... + " + limit + " = " + sum); </programlisting></td> + + <td><screen>0 + ... + 5 = 15 </screen></td> + </tr> + </informaltable> + </figure> + + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_allSummands"> + <title>Showing all constituents of a sum</title> + + <qandadiv> + <qandaentry> + <question> + <para>In <xref linkend="sd1_fig_loopCalculations"/> the resulting output was:</para> + + <screen>0 + ... + 5 = 15</screen> + + <para>Modify the code to show <computeroutput>1 + 2 + 3 + 4 + 5 = 15</computeroutput> instead.</para> + </question> + + <answer> + <para>First we need to print all values of our loop variable <code>i</code>:</para> + + <programlisting language="java">for (int i = 1; i <= limit; i++) { + System.out.print(i + " + "); + sum += i; +}</programlisting> + + <para>This yields <computeroutput>1 + 2 + 3 + 4 + 5 +</computeroutput>. Notice the last <quote><code>+</code></quote> operator: To avoid this operator being printed we may split our print statement and add an <code>if</code> guard inside our loop:</para> + + <programlisting language="java">for (int i = 1; i <= limit; i++) { + System.out.print(i); + <emphasis role="bold">if (i < limit) { // Do not print last value</emphasis> + System.out.print(" + "); + <emphasis role="bold">}</emphasis> + sum += i; +}</programlisting> + + <para>This leaves us with <computeroutput>1 + 2 + 3 + 4 + 5</computeroutput>. The only bit missing is a final print statement supplying the last operand and the sum in question:</para> + + <programlisting language="java">for (int i = 1; i <= limit; i++) { + System.out.print(i); + if (i < limit) { // Do not print last value + System.out.print(" + "); + } + sum += i; +} +<emphasis role="bold">System.out.println(" = " + sum);</emphasis></programlisting> + + <para>Instead of filtering a <quote><code>+</code></quote> operator we may as well terminate our loop earlier and move the last operand to the second print statement:</para> + + <programlisting language="java">int limit = 5; +int sum = 0; + +for (int i = 1; i < limit; i++) { + System.out.print(i + " + "); + sum += i; +} +sum += limit; // accounting for last term missing + +System.out.println(limit + " = " + sum);</programlisting> + </answer> + </qandaentry> + </qandadiv> + </qandaset> <qandaset defaultlabel="qanda" xml:id="sd1QandaPlayingLottery"> + <title>Playing lottery</title> + <qandadiv> <qandaentry> <question> @@ -3264,13 +3365,13 @@ for (int i = 2; i <= number; i++) { <qandadiv> <qandaentry> <question> - <para>Write a simple game where the user tries to guess a randomly selected number.</para> + <para>Write a simple game asking a user to guess a randomly chosen number.</para> - <para>Your program will randomly choose an integer number between zero and a configurable fixed upper limit e.g. 10. The user will have an again configurable number of tries to guess the number in question. Each time after entering a number your program will respond with either of the following:</para> + <para>Your program will select an integer (pseudo) random number between zero and an exclusive configurable fixed upper limit e.g. 10. The user will have an again configurable number of tries to guess the number in question. Each time after entering a number your program will respond with either of the following:</para> <glosslist> <glossentry> - <glossterm>User wins</glossterm> + <glossterm>Congratulations, you won!</glossterm> <glossdef> <para>You won!</para> @@ -3278,7 +3379,7 @@ for (int i = 2; i <= number; i++) { </glossentry> <glossentry> - <glossterm>User looses</glossterm> + <glossterm>You lost!</glossterm> <glossdef> <para>Game over, try another run?</para> @@ -3302,7 +3403,7 @@ for (int i = 2; i <= number; i++) { </glossentry> </glosslist> - <para>The system offers a maximum number of tries e.g. 6. In the following example the system randomly generated the secret number 5 internally. A possible dialogue looks like:</para> + <para>The system offers a maximum number of tries e.g. 6. In the following example the system initially selected the secret number 5. A possible dialogue looks like:</para> <screen>Try to guess my secret number between 0 and 10: You have 5 attempts @@ -3311,7 +3412,7 @@ Value is too low Input your guess:6 Value is too high Input your guess:5 -Congratulations! you won!</screen> +Congratulations, you won!</screen> <para>Regarding reading user input and creating random numbers please use the following recipe to get started:</para> @@ -3351,9 +3452,9 @@ try (final Scanner scan = new Scanner(System.in)) { System.out.print("Input your guess:"); final int userGuess = scan.nextInt(); if (userGuess < randomValue) { - System.out.println("Value is too low"); + System.out.println("Number is too low"); } else if (randomValue < userGuess) { - System.out.println("Value is too high"); + System.out.println("Number is too high"); } else { numberWasFound = true; break; @@ -3361,7 +3462,7 @@ try (final Scanner scan = new Scanner(System.in)) { } if (numberWasFound) { - System.out.println("Congratulations! you won!"); + System.out.println("Congratulations, you won!"); } else { System.out.println("Game over, try another run?"); } -- GitLab