EZ Programing Demos

Slow motion, step by step, animated demonstrations of basic computer programing techniques

Links to topics:

Current topic:

If

Simple if statements make a decision whether to do something or not. Basically, an if statement asks if a certain Boolean expression is true or false. If the expression is true, then something happens as a reslut. That is, some computer code that resides in a section that we will call the 'true block' is executed only if the Boolean expression is true. If the expression is false, then the code in the true block is not executed.

Run this demo a few times and change the Boolean expression so that sometimes it is true and sometimes it is false. Notice that the code before and after the if statement is always executed, but the code within the true block of the if statement is executed only if you set up a Boolean expression that is true.

 Slow  Medium  Fast 
Click 'Run' to go.

The 'true block' of the if statement executes if the Boolean expression is true:

 

The 'true block' would not execute if the Boolean expression were false:

 

The code before and after the if statement always executes:

 

An if statement lets you perform a certain task when a Boolean expression is true and to do nothing when the Boolean expression is false. You could think of the true block as being skipped if the Boolean expression is false and being included if the Boolean expression is true.

If you want to do a certain job when the Boolean expression is true and another different job when it is false (rather than doing nothing when it is false), then you will need an 'if, else' statement. This is the subject of the next article.

 

Here is the flowchart for this if statement demo:

 

In an imaginary computer language an if statement could look like this:

    
    printLine("Before if");
    
    if(2 < 3)
    {
      printLine("Boolean expression is true.");
    }
    
    printLine("After if");
    

 

Suggested next article:

At EZ Math Movie

EZ Math Movie is a site that can help you experiment with and understand many topics in mathematics. Its main feature is an interactive animated (x, y) graph that you control with an actual programing language. There are many examples and tutorials, and EZ Math Movie is crossed referenced with both EZ Programing Demos and Zona Land Education.

 

Zona Land Education is a site with explanations and interactive diagrams covering many topics in physics and mathematics. Zona Land Education is cross referenced with EZ Math Movie, and it contains several animations that use EZ Math Movie's programing language.