Demonstration of while loop

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

This is a simple demonstration of a while loop in which the user is prompted to enter words. The loop does not repeat again if the letter q is entered.

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

The blocks

The blocks

Code having exactly the same effect is obtained by changing the loop option from while to repeat and changing the operator of the condition from not equals to equals:
Repeat option

Repeat option

The corresponding Smart Pascal code of the start of the loop changes to:
while ( not ((LowerCase(strInput) = 'q'))) do    

(In the usual Pascal repeat loop the until condition is tested at the end of the loop and the code within the loop must execute at least once).

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="variables_set" id="=^1)68([-6,Om~rubF+~" x="38" y="13">
    <field name="VAR">strInput</field>
    <value name="VALUE">
      <block type="text" id="=;i@.#xQ%cX:q,c0W`KX">
        <field name="TEXT"></field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="*H0!$ZJD5Zx1t91-BrY5">
        <field name="VAR">strWords</field>
        <value name="VALUE">
          <block type="text" id="-=ig.+,MLQ]NL8Th|A[q">
            <field name="TEXT">Words entered: </field>
          </block>
        </value>
        <next>
          <block type="controls_whileUntil" id="L2V4O8hDgCZ6!hO=bEd-">
            <field name="MODE">WHILE</field>
            <value name="BOOL">
              <block type="logic_compare" id="mGc?h=mZllbso%Yr++yC">
                <field name="OP">NEQ</field>
                <value name="A">
                  <block type="text_changeCase" id="|2CMAIviQ?G)42Yqschv">
                    <field name="CASE">LOWERCASE</field>
                    <value name="TEXT">
                      <shadow type="text" id="xdDHEaJ9G:4UV%Py=S69">
                        <field name="TEXT">abc</field>
                      </shadow>
                      <block type="variables_get" id="as:3Fce]4e5bM?MJ[SR_">
                        <field name="VAR">strInput</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="B">
                  <block type="text" id="7Fxe?^KuO:,O*m4!s]j4">
                    <field name="TEXT">q</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO">
              <block type="variables_set" id="by6EB,FsBwv7%6#*kx+T">
                <field name="VAR">strInput</field>
                <value name="VALUE">
                  <block type="text_prompt_ext" id="#KK~B~n/GV,Iu+|t1rui">
                    <mutation type="TEXT"></mutation>
                    <field name="TYPE">TEXT</field>
                    <value name="TEXT">
                      <shadow type="text" id="k,MsI94EKW6JT%-**X_R">
                        <field name="TEXT">Enter word or q to quit</field>
                      </shadow>
                    </value>
                  </block>
                </value>
                <next>
                  <block type="controls_if" id="2(VbVtO3(yF-Wdkq8g~f">
                    <value name="IF0">
                      <block type="logic_compare" id="@49L8qTdYRZjM!FqfAF*">
                        <field name="OP">NEQ</field>
                        <value name="A">
                          <block type="text_changeCase" id=";Jke+s{M!sfhwI{dp{wc">
                            <field name="CASE">LOWERCASE</field>
                            <value name="TEXT">
                              <shadow type="text" id="xdDHEaJ9G:4UV%Py=S69">
                                <field name="TEXT">abc</field>
                              </shadow>
                              <block type="variables_get" id="WfjCVgd-I$[zt7jKk=_K">
                                <field name="VAR">strInput</field>
                              </block>
                            </value>
                          </block>
                        </value>
                        <value name="B">
                          <block type="text" id="yKs?c3_J)RZ#N|xTf?j=">
                            <field name="TEXT">q</field>
                          </block>
                        </value>
                      </block>
                    </value>
                    <statement name="DO0">
                      <block type="text_append" id="z=:`1G{3?7ue~~V9ZA.t">
                        <field name="VAR">strWords</field>
                        <value name="TEXT">
                          <shadow type="text" id="=N]/W,C5NW-?xZb.Ulp_">
                            <field name="TEXT"></field>
                          </shadow>
                          <block type="variables_get" id="SK1+B((_!WLQoCwYhhGI">
                            <field name="VAR">strInput</field>
                          </block>
                        </value>
                        <next>
                          <block type="text_append" id="o8I!HukmC2Ar?$,l-3hJ">
                            <field name="VAR">strWords</field>
                            <value name="TEXT">
                              <shadow type="text" id="=N]/W,C5NW-?xZb.Ulp_">
                                <field name="TEXT"></field>
                              </shadow>
                              <block type="text" id="dN9dLD{Q`cI`^F6gup_+">
                                <field name="TEXT"> </field>
                              </block>
                            </value>
                          </block>
                        </next>
                      </block>
                    </statement>
                  </block>
                </next>
              </block>
            </statement>
            <next>
              <block type="text_print" id="h4;9+)[^[T(.`^CMk.+L">
                <value name="TEXT">
                  <shadow type="text" id="33wX!u!}J8gGh{!y`]Jg">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="variables_get" id="CG]gxC|$AbHf)8sgW`PY">
                    <field name="VAR">strWords</field>
                  </block>
                </value>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var strWords: String;
var strInput: String;



strInput := '';
strWords := 'Words entered: ';
while ((LowerCase(strInput) <> 'q')) do
begin
  strInput := prompt('Enter word or q to quit');
  if ((LowerCase(strInput) <> 'q')) then
  begin
    strWords += strInput;
    strWords += ' ';
  end;
end;
Console.writeln(strWords);
    

Generated JavaScript

var strWords, strInput;


strInput = '';
strWords = 'Words entered: ';
while (strInput.toLowerCase() != 'q') {
  strInput = window.prompt('Enter word or q to quit');
  if (strInput.toLowerCase() != 'q') {
    strWords = String(strWords) + String(strInput);
    strWords = String(strWords) + String(' ');
  }
}
window.alert(strWords);

Generated Python Code

strWords = None
strInput = None

def text_prompt(msg):
  try:
    return raw_input(msg)
  except NameError:
    return input(msg)


strInput = ''
strWords = 'Words entered: '
while strInput.lower() != 'q':
  strInput = text_prompt('Enter word or q to quit')
  if strInput.lower() != 'q':
    strWords = str(strWords) + str(strInput)
    strWords = str(strWords) + str(' ')
print(strWords)

Copy of Test Output

Words entered: one two three
Programming - a skill for life!

Simple programs that output the 75 times table and input words from the user