Loop statements
In general, statements are executed sequentially, the first statement is executed first, and followed by the second, and so on.
Sometimes we may need to execute the statements several times, then we will use loop statement.
A loop statement allows us to execute a statement or group of statements multiple times.
There are 2 kinds of loop statements in Silk:
while statement
A while statement repeatedly executes statement(s) as long as a given condition is true.
while syntax:
while(condition)
{
statement(s);
}
The condition may be any expression, and the loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the loop.
while sample:
i=0;
while(i<10) //condition, any expression
{
// execute the following statements while the condition is true
print("i=",i);
i++;
}
if the condition is true, the statements within the
curly brace will be executed repeatedly.
The above while loop will be executed 10 times, and the result is as follows:
i= 0
i= 1
i= 2
i= 3
i= 4
i= 5
i= 6
i= 7
i= 8
i= 9
for statementA for statement allows you to efficiently write a loop that needs to execute a specific number of times.
for syntax:
for ( init; condition; update )
{
statement(s);
}
Here is the flow of control in a for loop:
- The init statement is executed first, and only once. This step allows you to declare and initialize any loop control variables.
- The condition is evaluated. If it is true, the statement(s) is executed. If it is false, the statement(s) will not be executed.
- After the statement(s) executes, the flow of control jumps back up
to the update statement, and it will update any loop control variables.
- The condition will be evaluated again. If it is true, the statement(s) executes and the process repeats itself.
After the condition becomes false, the loop terminates.
init, condition and update can be blank, then the for will become for(;;), condition will be always true, it's equal to while
(1), so we may need to use break statement to terminate the loop.
for sample:
for(i=0;i<10;i++)
{
print("i=",i);
}
result:
i= 0
i= 1
i= 2
i= 3
i= 4
i= 5
i= 6
i= 7
i= 8
i= 9
nested loop
We can
use one loop inside another loop, it's called nested loop.nested loop sample:
for (i=1;i<10;i++)
{
for(j=1;j<=i;j++)
{
result=j*i;
print(j,"x",i,"=",result);
}
print("");
}
Sometimes we may need to use loop control statements to change execution from its normal sequence.
Silk supports the following loop control statements:
break
When a break statement is encountered inside a loop, the loop is
immediately terminated and the program control resumes at the next
statement following the loop.
If
you are using nested loops, the break statement will stop the execution
of the innermost loop and start executing the next line of code after
the block.break sample:
//while loop
a = 1;
while( a < 10 )
{
print("a =", a);
a++;
if( a > 5)
{
//use break to terminate the loop
break;
}
print("while");
}
print("end");
The result is as follows:
a = 1
while
a = 2
while
a = 3
while
a = 4
while
a = 5
end
continue The continue
statement will not terminate the loop, it forces the next
iteration of the loop to take place, skipping any code in between.
For the
for loop, continue statement causes the conditional test and increment portions of the loop to execute.
For the
while loop, continue statement causes the program control to pass to the conditional tests.
continue sample:
a = 1;
while( a < 10 )
{
if( a == 5)
{
a++;
continue;
}
printf("a = %d\n", a);
a++;
}
The result is as follows:
a = 1
a = 2
a = 3
a = 4
a = 6
a = 7
a = 8
a = 9