diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index 2e6a98460d1313ff4b416059a47a0ca8c74858cc..48ad1f16d56886d1228084441e1c2fe9155a823b 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -153,7 +153,7 @@ </figure> <section xml:id="sd1_sect_statements_if"> - <title>The if statement</title> + <title>The if conditional statement</title> <figure xml:id="sd1_fig_if"> <title><code>if</code> ... <code>else</code> for block selection</title> @@ -900,9 +900,13 @@ Decimal value 11 not yet implemented</screen> } }</programlisting> </figure> + </section> + + <section xml:id="sd1_sect_switch"> + <title>The <code>switch</code> statement</title> <figure xml:id="sd1_fig_switch"> - <title>Better: The <code>switch</code> statement</title> + <title>Better: Using <code>switch</code></title> <programlisting language="java">... switch(number) { @@ -1293,6 +1297,118 @@ Midweek</screen> </qandaset> </section> + <section xml:id="sd1_sect_loops"> + <title><code>while</code>, <code>do ... while</code> and <code>for</code> loops</title> + + <figure xml:id="sd1_fig_loopWhy"> + <title>Why loops?</title> + + <glosslist> + <glossentry> + <glossterm>Objective</glossterm> + + <glossdef> + <para>Execute the same code multiple times.</para> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Solution</glossterm> + + <glossdef> + <para>Copy / paste the code in question:</para> + + <programlisting language="java">System.out.println("Do not copy!"); +System.out.println("Do not copy!"); +System.out.println("Do not copy!"); +System.out.println("Do not copy!");</programlisting> + </glossdef> + </glossentry> + + <glossentry> + <glossterm>Problem</glossterm> + + <glossdef> + <para>Only works if number of repetitions is known at compile time.</para> + </glossdef> + </glossentry> + </glosslist> + </figure> + + <figure xml:id="sd1_fig_loopBadParameterization"> + <title>Hard to parameterize</title> + + <para>Limited workaround for repetition count given at runtime:</para> + + <programlisting language="java">int repetitions; + +repetitions = 3; // value dynamically set + +switch(repetitions) { + case 5: System.out.println("Do not copy!"); + case 4: System.out.println("Do not copy!"); + case 3: System.out.println("Do not copy!"); + case 2: System.out.println("Do not copy!"); + case 1: System.out.println("Do not copy!"); +}</programlisting> + + <para>Problem: Clumsy and limited (five lines of output max.)</para> + </figure> + + <figure xml:id="sd1_fig_loopParamSolution"> + <title>A <code>while</code> loop</title> + + <programlisting language="java">int repetitions = 3; <co linkends="sd1_callout_whileLoop-1" xml:id="sd1_callout_whileLoop-1-co"/> +int loopCounter = 1; <co linkends="sd1_callout_whileLoop-2" xml:id="sd1_callout_whileLoop-2-co"/> + +while (loopCounter <= repetitions <co linkends="sd1_callout_whileLoop-3" xml:id="sd1_callout_whileLoop-3-co"/>) { + System.out.println("Do not copy!"); <co linkends="sd1_callout_whileLoop-4" xml:id="sd1_callout_whileLoop-4-co"/> + loopCounter++; <co linkends="sd1_callout_whileLoop-5" xml:id="sd1_callout_whileLoop-5-co"/> +}</programlisting> + + <screen>Do not copy! +Do not copy! +Do not copy!</screen> + </figure> + + <calloutlist> + <callout arearefs="sd1_callout_whileLoop-1-co" xml:id="sd1_callout_whileLoop-1"> + <para>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> + </callout> + + <callout arearefs="sd1_callout_whileLoop-3-co" xml:id="sd1_callout_whileLoop-3"> + <para>Condition to be checked before each new execution.</para> + </callout> + + <callout arearefs="sd1_callout_whileLoop-4-co" xml:id="sd1_callout_whileLoop-4"> + <para>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> + </callout> + </calloutlist> + + <figure xml:id="sd1_fig_loopParamSolutionSyntaxSugar"> + <title>Combining increment and termination condition</title> + + <programlisting language="java">int repetitions = 3; +int loopCounter = 1; + +while (loopCounter++ <= repetitions) { + System.out.println("Do not copy!"); +}</programlisting> + + <screen>Do not copy! +Do not copy! +Do not copy!</screen> + </figure> + </section> + <section xml:id="ex"> <title>Exercises</title> @@ -1467,7 +1583,7 @@ Midweek</screen> <qandadiv> <qandaentry> <question> - <para>Producing the following ASCII art for configurable Xmas tree sizes is a bit more challenging :</para> + <para>Producing the following ASCII art for configurable Xmas tree sizes is more challenging :</para> <screen> \ / -->*<-- @@ -1529,7 +1645,7 @@ Midweek</screen> System.out.println("-->*<--"); // "-->*<--" string. for (int x = 0; x < numberOfRowGroups + 1; x++) { // The tree's lower top "/ \". - System.out.print(' '); // We need numberOfRows+1 + System.out.print(' '); // We need again numberOfRows+1 } // preceding spaces. System.out.println("/_\\"); @@ -1575,7 +1691,7 @@ Midweek</screen> <para>So far we have not yet introduced methods. In anticipation of upcoming lessons we provide an alternate solution by introducing a method <methodname>printIndented(...)</methodname> which prints a string being indented by a given number of whitespace characters. The System.out.format(...) method will be explained in <xref linkend="sw1SectSquareNumbersFormatted"/>.</para> - <para>This effectively makes our code more readable:</para> + <para>This enhances our code's readability:</para> <programlisting language="java">public class XmasTree { @@ -1639,8 +1755,7 @@ Midweek</screen> // printIndent (numberOfRowGroups, "[___]"); // Indenting the bottom trunk ... } -} -</programlisting> +}</programlisting> </answer> </qandaentry> </qandadiv>