You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Currently, CashScript only supports `do {} while ()` loops in the 0.13.0-next.0 pre-release. More advanced loop constructs will be added in the full 0.13.0 release. Keep in mind that in a `do {} while ()` loop, the condition is checked *after* the block of code within the loop is executed. This means that the block of code within the loop will be executed at least once, even if the condition is initially `false`.
154
+
Currently, CashScript supports `for`, `while` and `do-while` loops in the `0.13.0-next` pre-release.
155
155
156
156
:::caution
157
157
Loops in CashScript are currently in beta and may not fully behave as expected with debugging and console.log statements. The syntax for loops may change in the future.
158
158
:::
159
159
160
+
#### for loop
161
+
162
+
For loops are the main loop construct in CashScript and has a similar syntax to languages like JavaScript or C. The loop header consists of three parts: the initialization, the condition and the update. The initialization is executed only once before the loop starts. The condition is checked before each iteration of the loop. The update is executed after each iteration of the loop.
163
+
164
+
#### Example
165
+
```solidity
166
+
pragma cashscript ^0.13.0;
167
+
168
+
contract NoTokensAllowed() {
169
+
function spend() {
170
+
int inputIndex = 0;
171
+
172
+
// Loop over all inputs (variable length), and make sure that none of them contain tokens
While loops are similar to for loops, but the initialization and update are not specified, so it it loops as long as the condition is true. The condition is checked *before* each iteration of the loop (unlike do-while loops).
183
+
184
+
#### Example
185
+
```solidity
186
+
pragma cashscript ^0.13.0;
187
+
188
+
contract NoTokensAllowed() {
189
+
function spend() {
190
+
int inputIndex = 0;
191
+
192
+
// Loop over all inputs (variable length), and make sure that none of them contain tokens
Do-while loops are similar to while loops, but the condition is checked *after* the block of code within the loop is executed. This means that the block of code within the loop will be executed at least once, even if the condition is initially `false`.
0 commit comments