Demonstration of loops

[XML] [Smart Pascal] [JavaScript] [Python] [output]

If you play the Countdown game you should find it useful to learn the 75 times table. This simple program makes use of a for loop to print it out.

We used the Blockly Playground to generate JavaScript and Python code from the same blocks.

See the following page for a while loop and a version of a repeat loop.

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="controls_for" id="ra6g4iVX%jh0U6!oOMp_" x="38" y="39">
    <field name="VAR">i</field>
    <value name="FROM">
      <shadow type="math_number" id="OaA/K@quBn!2]eCBu6kc">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <value name="TO">
      <shadow type="math_number" id="YKvK,z-WA2}#pA,n;U3+">
        <field name="NUM">13</field>
      </shadow>
    </value>
    <value name="BY">
      <shadow type="math_number" id="tvp|UEz_!bwk889v84K(">
        <field name="NUM">1</field>
      </shadow>
    </value>
    <statement name="DO">
      <block type="text_print" id="A+ve0fTS6UC/V1-o,[^2">
        <value name="TEXT">
          <shadow type="text" id="@-e/DcI1$l}m;?|[VY+#">
            <field name="TEXT">abc</field>
          </shadow>
          <block type="text_join" id="C7$k6yzWM`WxW5`%5?3^">
            <mutation items="3"></mutation>
            <value name="ADD0">
              <block type="variables_get" id="ZJlM(DH`iesuY7R$aO-v">
                <field name="VAR">i</field>
              </block>
            </value>
            <value name="ADD1">
              <block type="text" id=")OrrZ2pTJSL0N^b,[)hP">
                <field name="TEXT"> times 75 is </field>
              </block>
            </value>
            <value name="ADD2">
              <block type="math_arithmetic" id="Y~5Vw4G.oG(FT,}u1pYX">
                <field name="OP">MULTIPLY</field>
                <value name="A">
                  <shadow type="math_number" id="V3=G,q4c$c.,MF%PgV(7">
                    <field name="NUM">1</field>
                  </shadow>
                  <block type="variables_get" id="N_s-`Dfuh|N$tc-JB]Ua">
                    <field name="VAR">i</field>
                  </block>
                </value>
                <value name="B">
                  <shadow type="math_number" id="GJFRib2*y*=6Q;mr~Aux">
                    <field name="NUM">75</field>
                  </shadow>
                </value>
              </block>
            </value>
          </block>
        </value>
      </block>
    </statement>
  </block>
</xml>

Generated Smart Pascal Code

function Str(v: Variant) : string;
begin
  asm
    if (@v === 0) {
      @Result = '0';
    }
    else if (@v === '') {
      @Result = '';
    }
    else if (isNaN(@v)) {
      @Result = @v;
    }
    else {
      @Result = (@v).toString();
    }
  end;
end;



for var i := 1 to 13 do
begin
  Console.writeln(StrJoin([Str(i), Str(' times 75 is '), Str(i * 75)],''));
end;
    

Generated JavaScript

var i;


for (i = 1; i <= 13; i++) {
  window.alert([i,' times 75 is ',i * 75].join(''));
}

Generated Python Code

i = None


for i in range(1, 14):
  print(''.join([str(x) for x in [i, ' times 75 is ', i * 75]]))

Copy of Output

1 times 75 is 75
2 times 75 is 150
3 times 75 is 225
4 times 75 is 300
5 times 75 is 375
6 times 75 is 450
7 times 75 is 525
8 times 75 is 600
9 times 75 is 675
10 times 75 is 750
11 times 75 is 825
12 times 75 is 900
13 times 75 is 975
Programming - a skill for life!

XML, images and generated Smart Pascal code of examples including loops, arrays, validation and recursion