>> DAVID J. MALAN: Let's start writing some more complex conditions.
In particular, let's write a program that prompts the user for an integer,
say, between 1 and 10, and then does a bit of analysis.
But this time reporting whether that number is small or medium or large in
size, drawing those distinctions fairly arbitrarily.
>> To do this, I'm going to rely on get int, that function from the CS50
library that does exactly that.
And I'm also going to leverage print f.
So I'm going to get started by including cs50.h as
well as standard io.h.
>> And going to then declare main in the usual way, int main void, open curly
brace, close curly brace.
And I'm then going to prompt the user for an integer.
Print f, please give me an int between 1 and 10.
>> Now let's get that int by declaring a variable called, say, n and assigning
it the return value of get int.
Let's now do a bit of analysis.
>> If n is greater than or equal to 0 and n is less than or equal to, say, 3,
then we're going to go ahead and print out you picked a small int.
>> Else, if the user picks, say, a medium sized value, let's
check for that as follows.
Else if n is greater than or equal to, say, 4 and n is less than or equal to,
say, 7, then I'm going to print out you picked a medium int.
>> Finally, I'm going to assume that if the value is between 8 and 10, they
picked a large int.
So to express that, I'll type, else if n is greater than or equal to 8 and n
is less than or equal to 10, go ahead and print you picked a large int.
>> Else, there's a fourth condition here.
If the user didn't cooperate and instead typed a value that's less than
0 or greater than 10, I want to simply reprimand them.
Else print out, you picked an invalid int.
>> Let's save the file.
Compile it with make non-switch.
Back at my prompt, I'm going to run it with dot slash non-switch.
And let's try few values.
>> First, let's be uncooperative and type in negative 1.
Fortunately, that was detected by our final branch in that condition.
Let's try again with dot slash non-switch, this time giving it 1.
I indeed picked a small int.
>> Let's do it again with dot slash non-switch, this time picking, say, 5.
And that's a medium int.
Let's now do again dot slash non-switch.
And give it a value of 10, which is indeed a large int.
>> Now it's worth noting that this program could have been implemented in
any number of ways.
First of all, it was completely arbitrary that I drew the lines that I
did among small, medium, and large ints.
We could have drawn those boundaries anywhere.
>> But more interestingly, I didn't have to express myself with all of these
greater than or equal to or less than or equal to signs.
I could have, for instance, rewritten if n is greater than or equal to 4 and
n is less than or equal to 7, as instead if n is greater than 3 and n
is less than 8, then print out you picked a medium int.
>> After all, if the user's input, by nature of get int, is an integer, we
can either test if that value is greater than 3 or greater than or
equal to 4.
And we could also check if that value less than 8 or less
than or equal to 7.