Control structures available in solidity language are if...else, do, while, for, break, continue, and return. They work exactly the same as other languages such as C-language or JavaScript.
Some examples are shown here:
- if: If x is equal to 0 then assign value 0 to y else assign 1 to z:
if (x == 0) y = 0; else z = 1;
- do: Increment x while z is greater than 1:
do{ x++; } (while z>1);
- while: Increment z while x is greater than 0:
while(x > 0){ z++; }
- for, break, and continue: Perform some work until x is less than or equal to 10. This for loop will run 10 times, if z is 5 then break the for loop:
for(uint8 x=0; x<=10; x++) { //perform some work z++ if(z == 5) break; }
It will continue the work similarly, but when the condition is met, the loop will start again.
- return: Return is used to stop the execution of a function and returns an optional value. For example:
return 0;
It will stop the execution and return value of 0.