Now that you know how to use basic data types and basic operations on those types,

we're going to look today at conditionals and loops,

which are going to provide a way to do

computations that you couldn't contemplate doing without a computer.

First, we're going to talk about conditionals or the if statement.

In general, we're talking about adding

some more building blocks to your basic programming skills.

Last time, we talked about primitive data types and

math and text I/O and assignment statements.

That's pretty much what you get with a calculator.

With conditionals and loops,

really if we go to infinity and beyond as you'll see. All right.

So, the idea of conditionals and loops has to do with control flow in a computer.

It's the sequence of statements that are actually executed in a program.

With conditionals and loops,

we can choreograph the control flow.

Before, in the previous lecture,

we had what's called a straight line control flow,

where we just execute one statement after the other,

all our programs were of that form.

With conditionals and loops,

we can have a much more complicated control flow that

enables us to do a much much richer set of calculations,

and this is the kind of thing that we're going to look at next.

So, the simplest statement to talk about is called the if statement,

and that's where we execute certain statements

depending on the values of certain variables.

We evaluate an expression,

and if it's true,

we execute a statement.

So, here's a very simple example of an if statement,

if x less than zero x equals minus x.

This replaces x with the absolute value of x,

you test the Boolean expression,

x less than zero,

and if it's true,

then it sets x to minus x and if it's false,

it leaves X alone.

Either way, the result is that x has the absolute value of x.

You could also have an else option to execute

some different statement if the Boolean expression is false.

So, for example, this if statement with the else option computes the maximum of x and y.

If x is greater than y,

max equals x, that's the bigger one,

else if that condition is false,

max equals y, and y is the bigger one,

computes the maximum of x and y.

That's the if statement.

Here's an example of an if statement in a full program to simulate a coin flip.

All we do is generate a random double value

using math.random and if the result is less than a half,

we print out heads,

otherwise we print out tails.

So, each time we run this,

we might get a different result.

Now in this case, heads, heads, tails, heads.

Very simple use of an if statement.

So, just here's another one.

What does this program do? Can we think about it?

Well, it's called TwoSort,

so that tells the answer.

What it does is it reads two integers from

the command line and then print some out in numerical order.

Let's look at each statement.

A, is read from the first command line argument,

b from the second.

Then the if statement says if b is less than a,

these curly braces so that means you can put a bunch of

statements inside an option for an if statement or an else alternative.

It can be a sequence of statements,

and this sequence of statements is one of the first ones we

examined when we talked about the assignment statement that exchanges a and b,

but a into t,

b into a and put t back into b.

Then once that's done,

if b was less than a, we exchange them.

Now a is less than b.

If a was less than b, we didn't do anything.

In either case, a is less than b at the end,

and we print them out in numerical order.

So, after seeing that one,

you might think for a while about how you would put three numbers,

integers a, b and c in numerical order.

So that, whatever order they get typed in the command line,

they come out in numerical order.

This is a little program with three if statements that achieves this goal.

The first one does just but before,

it makes a smaller than b.

The second one makes a smaller than c,

and it is already smaller than b,

so it's smaller than both of them,

and then the third one makes be smaller than c. So,

a is smaller than both of them,

b smaller than c, then they're going to come out in numerical order.

So, that's an example of use of an if statement,

and again any order that the arguments get typed in,

they come out in numerical order.

Here's a example of an if statement use in a real program,

and a very typical example and one of

the reasons we want to talk about if statements right away.

Our program that we wrote that showed off

the different operations on integers had the possibility of dividing by zero,

and if division by zero happened,

that program would crash.

It's much better for the programmer, that's you,

to anticipate a condition that would make the program crash,

and check for it, and take some appropriate action or notify the user or whatever else.

In this case, if b equals zero,

we don't want to try to divide or compute the remainder.

So, we replace the statement that did

the division or computed the remainder with an if-else.

If that denominator is going to be zero,

we print out division by zero, and don't do it.

Otherwise, we go ahead and do it.

That's a much more robust programs of this where we got before.

If we do it with zero we get division by zero and we maintain control of the computation.

It's a good programming practice to use

conditionals to check for and avoid run-time errors,

and that's a good example of it.

So, that's a quick introduction to the if statement.