Next: Drawing conditional branch Up: PADEL syntax Previous: Block

## Draw loop process

There are two types of loop. One is previously checked loop, or a loop that checks condition at first and executes its inner body only if the condition is true. The another one is lately checked loop, or a loop that executes its inner loop body at first and then checks its condition, deciding whether the inner body is to be executed again or not. There is also a special type of previously checked loop that repeats its inner process for given number of time, thereby repeating for given constant number. In PADEL, you use while  for previously check repetition, do while  for lately checked repetition, and for  for constant numbered repetition.

This example shows how these loops are used:

```    do{
input N
}while(N < 1)

for(i = 1, N){
input A[i]
}

for(i = 2, N){
j = i
while(j >= 2  &&  A[j-1] > A[j]){
A[j-1] <-> A[j]
j = j - 1
}
}

for(i = 1, N){
print A[i]
}
```

This is an algorithm to sort N data and print the result.

First, it get number of data, N. If N is less than 1, it urges to input a valid number. This is an example of lately checked loop; it first executes some processes and then checks its condition.

Next, it receives n data. This is written using for since the same process is executed for constant number of time. After that, it compares current value with previous and if the previous is larger than the current, exchange them and repeat this process again. In this case, while is used because it is previously checked repetition. By the way, both for and whileare previously checked repetition and their PAD figures will be the same. I strongly recommend you, though, to use for for constant repetition and while for all other previously checked repetition.

Now, I am going to tell you some tips.

First, you do not have to enclose repetition body with { }   if the repetition body is one command only.

Thus, the following example is OK:

```    for(i = 1, N)
input A[i]
```

Of course, enclosing it with { } is no problem. Moreover, the repetition body may be empty as well:

```    for(i = 1, N){
}
```

In this case, you should enclose the body with { }.

However, the next C-like program is an error.

```    for(i = 1, N)  input A[i]
```

This is because there are two commands, repetition command for(i = 1, N) and input A[i] in one line.

Of course, the next example is also an error:

```    for(i = 1, N){  input A[i]  }
```

You can insert a return coe just before { and just after } in do while statement.

```    do
{
input N
}
while(N < 1)

for(i = 1, N)
{
input A[i]
}
```

You cannot make a new line in other places.

You can add spaces as you like.

```    for  (  i = 1, N  )  {
input A[i]
}
```

You can use any characters in conditional statement.

 Next: Drawing conditional branch Up: PADEL syntax Previous: Block