Programming Course: Lesson one

In the first class of programming course, we talked about a couple of ideas.

First, we talked about Boolean logic and the notion of TRUE and FALSE. These two things are very simple to think about. The examples we used involved a couple of students and a sibling.

We said “Nick is a boy” is TRUE. We also said that “Ryan is a boy” is TRUE. And we said that “Lily is a boy” is FALSE. Using that, we could expand to talk about the logical operators.

“Nick is a boy” AND “Ryan is a boy” is TRUE.

“Nick is a boy” OR “Ryan is a boy” is TRUE.

“Nick is a boy” AND “Lily is a boy” is FALSE.

“Nick is a boy” OR “Lily is a boy” is TRUE.

“Lily is a boy” AND “Nick is a boy” is FALSE.

“Lily is a boy” OR “Nick is a boy” is TRUE.

We also looked at logical negation, or the idea that NOT TRUE is FALSE and NOT FALSE is TRUE. This gets a little confusing when you try to fit it into English, but we tried:

NOT “Nick is a boy” is FALSE.

NOT “Lily is a boy” is TRUE.

Then we looked into code for these logical operators.

AND is &&, OR is ||, and NOT is !. Using T for TRUE and F for FALSE, we get the following values:

T && T == T

T && F == F

F && T == F

F && F == F

T || T == T

T || F == T

F || T == T

F || F == F

!T == F

!F == T

There’s a lot of ground to cover in logic, particularly with Boolean Algebra, but that got us started, so we’ll leave off there, with a final note: If you’re interested in more, you can find it here.

Moving on, we talked about variables. Variables are a little tricky because they’re pretty different in different languages. But for our purposes, beginning programming, we discussed what variables are for. Variables are there to hold stuff for us, so we can do work without always knowing what values are going to be.

We talked about a program that prints the greater of two numbers. The simplest way to do this, if you know the two numbers going in is to just write one line of code:

`console.log(1);`

if you’re comparing two numbers and 1 is the lower one. The problem is, when you’re programming things that are smarter than that, you won’t be able to know in advance what the values you’re using are. So, we added variables to our problem, like this:

`var input1 = 1;`

var input2 = 2;

So, we have two variables that we can use. We can change them if we want to, we can read them, we can compare them, we can print them out to the screen:

`console.log(input1);`

So, knowing that we can store things, and knowing that computers are good at making decisions, we need to find out HOW a computer makes decisions.

The easiest way for that is with a conditional statement — for our purposes, an if-statement:

`if ([boolean equation here]) {`

[do the stuff you want to do here]

}

That’s the syntax for an if-statement. We learned about boolean equations above, but we have to take a little time to learn a way to make those TRUE and FALSE values.

One way is to use mathematical comparators:

>, <, ==, !=, >=, <= That is, greater-than, less-than, equal-to, not-equal-to, greater-than-or-equal-to, and less-than-or-equal-to. There are more ways than this to generate boolean values, but these are the simplest, and they suit our purposes well. Greater than is a great place to start, because we want to see which is the biggest number of our two:

`if (input1 > input2) {`

console.log("The bigger number is input1, which is: " + input1);

}

Ok, there’s a little bit to talk about here. The console.log line has a + sign in it, but for now, let’s just say that when you’re dealing with a string, it just mashes everything together.

The first line is what we’re most concerned with. Greater than is true if the first number is bigger than the second. So, if input1 is 10 and input2 is 2, this would evaluate to true. The curly-braces {} hold everything that happens if this case is true. So you could have more than one line inside those curly-braces and it would only execute if the condition in line 1 is true.

Great, so assuming input1 is bigger than input2, we’re done. But, as we said, we can’t assume that. So, we need more. We could just add:

`if (input2 > input1) {`

console.log("The bigger number is input2, which is: " + input2);

}

That would definitely get us further down the road. I mean, you’ve got the cases where one is bigger or the other is bigger, and that should be just about it, right? Except that we’re missing one case. What about if they’re equal? That should be easy by now. We have another comparator to check equality:

`if (input1 == input2) {`

console.log("input1 and input2 are equal, and their value is: " + input1);

}

And that closes all the cases. If neither one is bigger, then they must be equal. So, we’re done. But that is a little bit of a bog. Three separate if statements. What if our comparison was more complex?

`if (input1 > 0 && input2 > 0 && (input1 + input2) < 100) {`

console.log("both are greater than zero, and their sum is less than 100");

}

That’s great, but how would you do something if that wasn’t true?

Sure, you could code it explicitly:

`if (input1 < 0 || input2 < 0 || (input1 + input2) > 100) {`

console.log("no they aren't");

}

But there’s a simpler way:

`if (input1 > 0 && input2 > 0 && (input1 + input2) > 100) {`

console.log("both are greater than zero, and their sum is less than 100");

} else {

console.log("no they aren't");

}

That’s the all-powerful else. If the condition in the initial if-statement is true, the block under it is executed. However, if the condition is false, then you can put an else statement there to be executed instead. You can chain if-else statements so they look like this:

`if([initial boolean equation]) {`

[first code block]

} else if ([second boolean equation]) {

[second code block]

} else {

[default code block]

}

So, if we go back to our original problem, we have something we can use for that:

`if (input1 > input2) {`

console.log("The bigger number is input1, which is: " + input1);

} else if (input2 > input1) {

console.log("The bigger number is input2, which is: " + input2);

} else {

console.log("input1 and input2 are equal, and their value is: " + input1);

}

We ended the lesson by coding up the reverse of this, which was to print out the smaller of two numbers, or if they were equal, then to print out the word equal. Most answers looked something like this:

`var input1 = 1;`

var input2 = 2;

```
```

`if (input1 < input2) {`

console.log(input1);

} else if (input2 < input1) {

console.log(input2);

} else {

console.log("Equal");

}

Then I tested the programs by entering different values for the variables, one where input1 was bigger, one where input2 was bigger, and one where they were equal.

There was a lot to think about by the end of the lesson, and the syntax can sometimes be tricky, that’s for sure. It starts out being frustrating that the language is so picky, especially for students who are used to the ambiguity of human languages, but once you learn to properly state things, there’s almost no opportunity to be misunderstood. Learning how to properly state things is what makes someone a programmer.