how does the loops blocks work

838 views
Skip to first unread message

Ahmed Alngar

unread,
Apr 13, 2019, 4:48:44 PM4/13/19
to Blockly
hello 
I want to know how the loops blocks work and if  I make a custom block what's the sequence of executing the code
or how it works
thanks

Beka Westberg

unread,
Apr 13, 2019, 6:53:37 PM4/13/19
to Blockly
Hello,

If you want to see the code the loop blocks generate you can see that here.
If you want to see the json used to define how the loop blocks look and act you can see that here.
If you want to know about creating a custom block you can see that here.
If you want to know ways to execute generated code you can see that here.

Generally I don't like link posts because they're discouraging, but I think it was best in this case. If you have any more specific questions I'd be happy to try and help!

I hope that helps get you started,
Beka

Ahmed Alngar

unread,
Apr 14, 2019, 9:07:00 AM4/14/19
to Blockly
thanks for replying Beka 

beka.PNG

can you explain this code to me

Beka Westberg

unread,
Apr 14, 2019, 10:43:44 AM4/14/19
to Blockly
Hello,

A little bit of a tricky thing about this code is that it's generating javascript for both of these blocks:

RepeatBlocks.jpg

Blockly.JavaScript['controls_repeat_ext'] = function(block) {
 
// This if/else statement figures out how many times the loop
 
// needs to repeat.
 
// If it is the top block in the picture above block.getField('TIMES')
 
// will return a field, otherwise it will return null and do the else statement.
 
if (block.getField('TIMES')) {
   
// This figures out what the number on the top block is, and
   
// converts it to a string to be used below.
   
var repeats = String(Number(block.getFieldValue('TIMES')));
 
} else {
   
// This figures out what the number on the bottom block is, and
   
// converts it to a string to be used below. For more info about
   
// valueToCode see link (1)
   
var repeats = Blockly.JavaScript.valueToCode(block, 'TIMES',
       
Blockly.JavaScript.ORDER_ASSIGNMENT) || '0';
 
}


 
// This generates code for all of the blocks inside the repeat block.
 
// for more info about statementToCode see link (2)
 
var branch = Blockly.JavaScript.statementToCode(block, 'DO');


 
// This adds a check so that the generated code doesn't loop
 
// infinitely or for too long. For more info about this see link (3)
  branch
= Blockly.JavaScript.addLoopTrap(branch, block.id);


 
// This initialized the code variable.
 
var code = '';
 
 
// This grabs a name for the variable inside the for(...) statement
 
// that won't collide with any user defined variables. For example
 
// if the user already defined a 'count' variable, this variable would
 
// be called something like 'count1'.
 
var loopVar = Blockly.JavaScript.variableDB_.getDistinctName(
     
'count', Blockly.Variables.NAME_TYPE);
 
 
// This runs if there is a block like image (4), where the input
 
// has other code inside of it. It makes sure that that code is
 
// evaluated before the loop starts.
 
var endVar = repeats;
 
if (!repeats.match(/^\w+$/) && !Blockly.isNumber(repeats)) {
   
// This grabs a name for the variable above the for(...) statement.
   
var endVar = Blockly.JavaScript.variableDB_.getDistinctName(
       
'repeat_end', Blockly.Variables.NAME_TYPE);
   
// This adds the variable asignment before the for loop.
    code
+= 'var ' + endVar + ' = ' + repeats + ';\n';
 
}


 
// This puts all the pieces together to form the gnerated code.
  code
+= 'for (var ' + loopVar + ' = 0; ' +
      loopVar
+ ' < ' + endVar + '; ' +
      loopVar
+ '++) {\n' +
      branch
+ '}\n';


 
// This returns the code so it can be added to other code strings or used.
 
return code;
};

(4) : 

RepeatBlocks2.jpg


This block generates the below code:
var repeat_end = 2 + 1;
for (var count = 0; count < repeat_end; count++) {
}

For more info on generating code this page is very helpful.

I hope that helps! If you have any further questions please reply!
Beka

Ahmed Alngar

unread,
Apr 15, 2019, 6:05:14 AM4/15/19
to blo...@googlegroups.com
Thank you so much


--
You received this message because you are subscribed to the Google Groups "Blockly" group.
To unsubscribe from this group and stop receiving emails from it, send an email to blockly+u...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages