diff --git a/Doc/Sd1/statements.xml b/Doc/Sd1/statements.xml index e0c944b0ae65c2fbfbaa2c8918b8e408a47ea70e..f63b38887aaa0a84e3230dacd0985218bdc32f3a 100644 --- a/Doc/Sd1/statements.xml +++ b/Doc/Sd1/statements.xml @@ -179,6 +179,13 @@ Done!</screen></td> </informaltable> </figure> + <figure xml:id="sd1_fig_ifSyntax"> + <title><code>if</code> execution syntax</title> + + <programlisting language="java">if (booleanExpression) + (block | statement)</programlisting> + </figure> + <figure xml:id="sd1_fig_ifElse"> <title><code>if</code> ... <code>else</code> block execution selection</title> @@ -231,6 +238,15 @@ Done!</screen></td> </informaltable> </figure> + <figure xml:id="sd1_fig_ifElseSyntax"> + <title><code>if ... else</code> execution syntax</title> + + <programlisting language="java">if (booleanExpression) + (block | statement) +else + (block | statement)</programlisting> + </figure> + <figure xml:id="sd1_fig_bestPracticeCompareEquals"> <title><code>Best practices comparing for equality</code></title> @@ -601,6 +617,34 @@ System.out.println("Failed!");</programlisting> </qandadiv> </qandaset> + <figure xml:id="sd1_fig_ifElseNested"> + <title>Nested <code>if ... else</code></title> + + <informaltable border="1"> + <tr> + <td valign="top"><programlisting language="java">if ('A' == grade || 'B' == grade) { + result = "Excellent"; +} else { + if ('C' == grade) { + result = "O.k."; + } else { + if ('D' == grade) { + result = "Passed"; + } else { + result = "Failed"; + } + } +}</programlisting></td> + + <td valign="top"><mediaobject> + <imageobject> + <imagedata fileref="Ref/Statements/if_elseIf_else.svg"/> + </imageobject> + </mediaobject></td> + </tr> + </informaltable> + </figure> + <figure xml:id="sd1_fig_ifElse_else"> <title><code>if ... else if ... else</code></title> @@ -625,6 +669,18 @@ System.out.println("Failed!");</programlisting> </informaltable> </figure> + <figure xml:id="sd1_fig_ifElse_elseSyntax"> + <title><code>if ... else if ... else</code> syntax</title> + + <programlisting language="java">if (booleanExpression) + (block | statement) +else if + (block | statement) +... +else + (block | statement)</programlisting> + </figure> + <figure xml:id="sd1_fig_useScannerClass"> <title>User input recipe</title> @@ -968,6 +1024,29 @@ switch(number) { Saturday</screen> </figure> + <figure xml:id="sd1_fig_switchSyntax"> + <title><code>switch</code> Syntax</title> + + <programlisting language="java">switch(expression) { +case value_1 : + [statement(s);] + [break; ] +case value_2 : + [statement(s); ] + [break;] + ... +case value_n : + [statement(s); ] + [break;] +default: + [statement(s);] + [break;] +}</programlisting> + + <screen>Enter a weekday number (1=Monday, 2=Tuesday,...) :>6 +Saturday</screen> + </figure> + <qandaset defaultlabel="qanda" xml:id="sd1_qanda_whyBreak"> <title>Why <quote>break</quote>?</title> @@ -1522,6 +1601,13 @@ Do not copy!</screen></td> </callout> </calloutlist> + <figure xml:id="sd1_fig_whileSyntax"> + <title><code>while</code> syntax</title> + + <programlisting language="java">while (booleanExpression) + (block | statement)</programlisting> + </figure> + <figure xml:id="sd1_fig_loopParamSolutionSyntaxSugar"> <title>Combining increment and termination condition</title> @@ -1561,6 +1647,12 @@ Do not copy!</screen></td> </mediaobject></td> </tr> </informaltable> + </figure><figure xml:id="sd1_fig_doWhileSyntax"> + <title><code>do ... while</code> syntax</title> + + <programlisting language="java">do + (block | statement) +while (booleanExpression);</programlisting> </figure><qandaset defaultlabel="qanda" xml:id="sd1_qanda_tellEvenOdd"> <title>Even or odd?</title> @@ -1678,6 +1770,58 @@ while (i < 5 <coref linkend="sd1_callout_for-2-co"/>) { </calloutlist> </figure> + <figure xml:id="sd1_fig_forSyntax"> + <title><code>for</code> syntax</title> + + <programlisting language="java">for ( init ; booleanExpression ; update ) + (block | statement)</programlisting> + </figure> + + <figure xml:id="sd1_fig_forVariableScope"> + <title><code>for</code> variable scope</title> + + <informaltable border="1"> + <tr> + <td><programlisting language="java">for (int i = 0 <co linkends="sd1_callout_forVariableScope-1" xml:id="sd1_callout_forVariableScope-1-co"/>; i < 3; i++) { + System.out.println(i); +} + +System.out.println(i); <emphasis role="bold">// Error: i undefined</emphasis></programlisting></td> + + <td><programlisting language="java">int i; <co linkends="sd1_callout_forVariableScope-2" xml:id="sd1_callout_forVariableScope-2-co"/> +for (int i = 0; i < 3; i++) { + System.out.println(i); +} + +System.out.println(i); // o.K.</programlisting></td> + </tr> + </informaltable> + + <calloutlist> + <callout arearefs="sd1_callout_forVariableScope-1-co" xml:id="sd1_callout_forVariableScope-1"> + <para>Scope of variable i bound to <code>for</code> loop</para> + </callout> + + <callout arearefs="sd1_callout_forVariableScope-2-co" xml:id="sd1_callout_forVariableScope-2"> + <para>Variable <code>i</code> defined in <quote>current</quote> scope.</para> + </callout> + </calloutlist> + </figure> + + <figure xml:id="sd1_fig_forWhileRelation"> + <title><code>for</code> <abbrev>vs.</abbrev> while relationship</title> + + <informaltable border="1"> + <tr> + <td><programlisting language="java">while ( expression ) + (block | statement)</programlisting></td> + + <td><programlisting language="java">for ( ;expression ;) + (block | statement)</programlisting></td> + </tr> + </informaltable> + </figure> + <qandaset defaultlabel="qanda" xml:id="sd1QandaOnlyEven"> <title>Printing even numbers</title>