Range Check using Blockly

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

This is a demonstration of a range check of an input integer. An input is accepted if it falls within the range between the lower and upper limit (inclusive).

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

The blocks

The blocks

XML Code of Blocks

<xml xmlns="http://www.w3.org/1999/xhtml">
  <block type="procedures_defreturn" id="q`Ja2;gdPCEU40h]~WCS" x="-87" y="-13">
    <mutation>
      <arg name="intAge"></arg>
      <arg name="intLowerLimit"></arg>
      <arg name="intUpperLimit"></arg>
    </mutation>
    <field name="NAME">RangeCheck</field>
    <comment pinned="false" h="80" w="160">Returns true if intAge is within range intLowerLimit to intUpperLimit</comment>
    <statement name="STACK">
      <block type="controls_if" id=";BCb[txI[O;w7J}4BvV?">
        <mutation else="1"></mutation>
        <value name="IF0">
          <block type="logic_operation" id="3}x8?JXxJN)^z__FM0,p">
            <field name="OP">AND</field>
            <value name="A">
              <block type="logic_compare" id="sZ:VM6a5Dk0`!}e(g0}[">
                <field name="OP">GTE</field>
                <value name="A">
                  <block type="variables_get" id="qUB7Ye^@zbl0||7`*%Yr">
                    <field name="VAR">intAge</field>
                  </block>
                </value>
                <value name="B">
                  <block type="variables_get" id=":nr{uW0G:am`eqvitSeW">
                    <field name="VAR">intLowerLimit</field>
                  </block>
                </value>
              </block>
            </value>
            <value name="B">
              <block type="logic_compare" id="O/-]vND@oK~aiogdZQ?(">
                <field name="OP">LTE</field>
                <value name="A">
                  <block type="variables_get" id="*tNCb+_t7X}[k1$2v]Ki">
                    <field name="VAR">intAge</field>
                  </block>
                </value>
                <value name="B">
                  <block type="variables_get" id=")(w#[^,44=WRUgmoHX~t">
                    <field name="VAR">intUpperLimit</field>
                  </block>
                </value>
              </block>
            </value>
          </block>
        </value>
        <statement name="DO0">
          <block type="variables_set" id="d$`^K*C[B.RCDeOU]=G-">
            <field name="VAR">boolRangeCheckOK</field>
            <value name="VALUE">
              <block type="logic_boolean" id="o8g)W}(F![)@fbB}Pcrq">
                <field name="BOOL">TRUE</field>
              </block>
            </value>
          </block>
        </statement>
        <statement name="ELSE">
          <block type="variables_set" id="F/:qzA4n6%.fyerXSnw^">
            <field name="VAR">boolRangeCheckOK</field>
            <value name="VALUE">
              <block type="logic_boolean" id="B;ST#=9DUPZ)}MIe`^-J">
                <field name="BOOL">FALSE</field>
              </block>
            </value>
          </block>
        </statement>
      </block>
    </statement>
    <value name="RETURN">
      <block type="variables_get" id="|Gc8wWo_[P4V$Z$Y+bkf">
        <field name="VAR">boolRangeCheckOK</field>
      </block>
    </value>
  </block>
  <block type="variables_set" id="FTz%Kot4S463;W=XL!Nk" x="-87" y="188">
    <field name="VAR">intInputAge</field>
    <value name="VALUE">
      <block type="math_number" id="qnxo7-(O6JpiMg|ESffI">
        <field name="NUM">0</field>
      </block>
    </value>
    <next>
      <block type="variables_set" id="kdfn^WdrxXuZF~C[]!YB">
        <field name="VAR">boolRangeCheckOK</field>
        <value name="VALUE">
          <block type="logic_boolean" id="Kr;C,@x|jH}]jY;wc/05">
            <field name="BOOL">FALSE</field>
          </block>
        </value>
        <next>
          <block type="controls_whileUntil" id="0uc1)~jDVT_35ovJy{iW">
            <field name="MODE">WHILE</field>
            <value name="BOOL">
              <block type="logic_compare" id="-zF25pe]}t|c:~k2k3-!">
                <field name="OP">EQ</field>
                <value name="A">
                  <block type="procedures_callreturn" id="jZlqj9+RX[T?[fHR`bD$">
                    <mutation name="RangeCheck">
                      <arg name="intAge"></arg>
                      <arg name="intLowerLimit"></arg>
                      <arg name="intUpperLimit"></arg>
                    </mutation>
                    <value name="ARG0">
                      <block type="variables_get" id="Yv%VJy)Jqiz;`)r6v%gp">
                        <field name="VAR">intInputAge</field>
                      </block>
                    </value>
                    <value name="ARG1">
                      <block type="math_number" id="Qp|DZ3*TxT@RCLh}qf5:">
                        <field name="NUM">11</field>
                      </block>
                    </value>
                    <value name="ARG2">
                      <block type="math_number" id=";H?g`TrnQ!rj8nLa8Q`u">
                        <field name="NUM">18</field>
                      </block>
                    </value>
                  </block>
                </value>
                <value name="B">
                  <block type="logic_boolean" id="D09rrMbC,PUXQhJb9)$k">
                    <field name="BOOL">FALSE</field>
                  </block>
                </value>
              </block>
            </value>
            <statement name="DO">
              <block type="variables_set" id="e1B1^=mK=_iL[;_iy3Ff">
                <field name="VAR">intInputAge</field>
                <value name="VALUE">
                  <block type="math_round" id="EN{uR]6FsB0VaW?p+XQD">
                    <field name="OP">ROUND</field>
                    <value name="NUM">
                      <shadow type="math_number" id="RuHpKs}Y3fo~lw;AHc)_">
                        <field name="NUM">3.1</field>
                      </shadow>
                      <block type="text_prompt_ext" id="daHEQC|d2kd^%Z5p`/:e">
                        <mutation type="NUMBER"></mutation>
                        <field name="TYPE">NUMBER</field>
                        <value name="TEXT">
                          <shadow type="text" id="QChwyn7f8{Hjj(H!)DU`">
                            <field name="TEXT">Please enter age of student.</field>
                          </shadow>
                        </value>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </statement>
            <next>
              <block type="text_print" id="rhobk{+=KfYNAU]!xJeS">
                <value name="TEXT">
                  <shadow type="text" id="EljYCmC;;f}}XuBSqD5+">
                    <field name="TEXT">abc</field>
                  </shadow>
                  <block type="text_join" id="*Gs2,pSriFrpT%`@OLv[">
                    <mutation items="3"></mutation>
                    <value name="ADD0">
                      <block type="text" id=")5On.)9F5hjVyL+,?=F7">
                        <field name="TEXT">Age </field>
                      </block>
                    </value>
                    <value name="ADD1">
                      <block type="variables_get" id="3W?$;N|h+njile-L:}j,">
                        <field name="VAR">intInputAge</field>
                      </block>
                    </value>
                    <value name="ADD2">
                      <block type="text" id="[V@wy~(_%=DSQH}xI6!o">
                        <field name="TEXT"> accepted.</field>
                      </block>
                    </value>
                  </block>
                </value>
              </block>
            </next>
          </block>
        </next>
      </block>
    </next>
  </block>
</xml>

Generated Smart Pascal Code

var boolRangeCheckOK: Boolean;
var intAge: Integer;
var intInputAge: Integer;
var intLowerLimit: Integer;
var intUpperLimit: Integer;


{
  Returns true if intAge is within range intLowerLimit to intUpperLimit
}
function RangeCheck(intAge: Integer; intLowerLimit: Integer; intUpperLimit: Integer): Boolean;
begin
  if ((intAge >= intLowerLimit) and (intAge <= intUpperLimit)) then
  begin
    boolRangeCheckOK := true;
  end
  else
  begin
    boolRangeCheckOK := false;
  end;
  Result := boolRangeCheckOK;
end;


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;



intInputAge := 0;
boolRangeCheckOK := false;
while ((RangeCheck(intInputAge, 11, 18) = false)) do
begin
  intInputAge := Round(StrToFloat(prompt('Please enter age of student.')));
end;
Console.writeln(StrJoin([Str('Age '), Str(intInputAge), Str(' accepted.')],''));
    

Generated JavaScript

var boolRangeCheckOK, intUpperLimit, intAge, intLowerLimit, intInputAge;

/**
 * Returns true if intAge is within range intLowerLimit to intUpperLimit
 */
function RangeCheck(intAge, intLowerLimit, intUpperLimit) {
  if (intAge >= intLowerLimit && intAge <= intUpperLimit) {
    boolRangeCheckOK = true;
  } else {
    boolRangeCheckOK = false;
  }
  return boolRangeCheckOK;
}


intInputAge = 0;
boolRangeCheckOK = false;
while (RangeCheck(intInputAge, 11, 18) == false) {
  intInputAge = Math.round(parseFloat(window.prompt('Please enter age of student.')));
}
window.alert(['Age ',intInputAge,' accepted.'].join(''));

Generated Python Code

import math

boolRangeCheckOK = None
intUpperLimit = None
intAge = None
intLowerLimit = None
intInputAge = None

"""Returns true if intAge is within range intLowerLimit to intUpperLimit
"""
def RangeCheck(intAge, intLowerLimit, intUpperLimit):
  global boolRangeCheckOK, intInputAge
  if intAge >= intLowerLimit and intAge <= intUpperLimit:
    boolRangeCheckOK = True
  else:
    boolRangeCheckOK = False
  return boolRangeCheckOK

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


intInputAge = 0
boolRangeCheckOK = False
while RangeCheck(intInputAge, 11, 18) == False:
  intInputAge = round(float(text_prompt('Please enter age of student.')))
print(''.join([str(x) for x in ['Age ', intInputAge, ' accepted.']]))

Copy of Output

After attempting to enter the values 10, 19 and 11 eventual outcome was:

Age 11 accepted
Programming - a skill for life!

Introduction to validation (length check, range check, presence check, type check and character check)