# Turing/Control Structures and Logical Expressions

For all of these codes, don't forget to put end if/loop/for at the end of the if/loop/for statement.

## If statements

Now, try this code:

```   var text : string
get text
if (text = "Hello") then
put "Hello, world"
elsif (text = "Bananas") then
put "Bananas are good"
else
put "Bye."
end if
```
• If the user enters "Hello, world" then the output is "Hello"
• If the input is "Bananas" then the output is "Bananas are good"
• If the input is anything else, then the ouput is "Bye."

## Loops

Loops do exactly what they sound like. The program from the loop to the end loop Try this code:

```   var a : int := 0
loop
put a
a := a + 1
end loop
```

This program should output numbers indefinitely from 0 up. Now, let's say that we want to only put the numbers from 0 to 100. We need to put an 'exit when' statement.

```   var a : int := 0
loop
put a
exit when a = 100 %put the exit when right here
a := a + 1
end loop
```

## For statements

For statements are really useful. The program:

```   for i : small..big by intervals
end for
```

is the same as

```   var i : real := small
loop
exit when i = big
i := i + intervals
end loop
```

If that was confusing, a for statement pretty much gives the variable 'i' all the values from small to big.

```  for i : 1..10
put i, ", "
end for
```

would output

```  1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
```

They are really useful for assigning values to an entire array, for example, this program gives the value '0' for every value in the array.

```  var asdf : array 1..10,1..10 of int
for i : 1..10
for j : 1..10
asdf (i,j) := 0
end for
end for
```

## Exercises

1. Make the user input a user name and password. (Hint: If statement)
2. Make the program give the user as many tries as they want, or if you want a challenge, 5 tries (Hint: Loop)
3. Re-do exercise 2 (input 10 values from an array) from the last one, with for statements

 Project: Turing Previous: Variables and Types — Turing/Control Structures and Logical Expressions — Next: Functions and Subroutines