# Flow Control

How do programs flow, and how do we control that flow?

## Intro

With the basic flow control mechanisms introduced here, we can decide which path our program will take. Computers aren’t very smart and everything needs to be laid out, including the flow of our program. Can you imagine how complex a flow control could get when you have to think and program for every possible scenario?

## If statement

The if statement should be easy to understand. It checks if a condition is true to run a piece of code. For example, we want to print “Too hot” if the temperature is more than 40 degrees (Celsius).

``````if(temperature > 40){
}``````
``````if temperature > 40 :
``````

The above code examples will not run since we do not have the temperature! But here is an example that we can run. It will print 0 to 9 repeatedly forever because we reset the count back to 0 whenever it reaches 10. Note how we used == instead of =. In coding, = is an assignment and not a check of equality. Use = to assign a value to a variable and == to check if a value is equal to another.

``````var count = 0;

while(true){
count =  count + 1;
if(count == 10){
count = 0;
}
}``````
``````count = 0

while True:
count = count + 1
if count == 10:
count = 0
``````

## else

The else statement works hand in hand with the if statement. It is there to indicate the opposite of the if statement.

``````var temperature = 47;

if(temperature > 40){
}
else{
}``````
``````temperature = 47

if temperature > 40 :
else:
``````

You can also have another if statement with else. In Python, this becomes elif.

What output would you expect if you keep temperature at 47? What if you change it to 5 or to 30? Change the temperature variable in the below example to different values and observe the printed text on the screen.

``````var temperature = 47;

if(temperature > 40){
}
else if(temperature < 10){
}
else{
}``````
``````temperature = 47

if temperature > 40:
elif temperature < 10:
else:
``````

## Switch Statement

The switch statement is not available in Python and we will not use it in the lessons, but here is an example. The switch statement below uses the variable count. Based on what the value of count is determines which line of code will run. The key word break is where the code exits the switch statement after executing the section. If count doesn’t equal any of the cases then it will automatically execute the default section of code.

``````var count = 5;

switch(count){
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
default:
break;
}``````

Change the value of count, run the program and view the results.

## What’s Next?

You are nearly done with the basics of the language, but we still need to learn about operators.

## BrainStorm

Is there any difference between how humans and computers when it comes to “if”? Computers are very “black and white” with no grey in between. If we program a robot car to cross the intersecting when the traffic light is green, the robot car will do just that without taking in any other variables. Would you cross if there is a person/car in front of you? To us, this is automatic as our brains make millions of decisions around the clock. Teaching a machine to mimic what we do and factor it all in requires very sophisticated software. This is called artificial intelligence. And even then, how do you teach a machine to have feelings?