-
Notifications
You must be signed in to change notification settings - Fork 0
Unit 1 ‐ Lesson 3
The bool
type was mentioned in Lesson 1. This data type may seem rather foreign and strange. Unlike numbers and text, booleans are not something most people think about on a daily basis. In programming, though, they play a very special role: they allow the programmer to tell the program how to make decisions.
Intimately tied to the bool
is the if
keyword. if
lets the computer know that you want it to perform a command if some condition is met. You define the condition for the program.
For example:
string input = Console.ReadLine();
double parsedInput = double.Parse(input);
bool parsedInputIsLessThanThree = parsedInput < 3;
if (parsedInputIsLessThanThree)
{
Console.WriteLine("Input is less than 3");
}
The above code determines if the input number is less than three; if it is less than three, print out "Input is less than 3". It uses a special operator, <
, to determine whether parsedInput
is less than three. The <
operator returns a boolean value; the input is either less than three (true) or it is not less than three (false). The <
operator is called a conditional operator because it returns a bool
as output.
The if
statement can be condensed to:
if (parsedInput < 3)
{
Console.WriteLine("Input is less than 3");
}
i.e., a bool
variable does not need to be declared ahead of time. The <
operation returns a bool
as its output, and so it is shorter and sometimes more easily readable to put this operation within the if
statement itself.
There are a number of conditional operators in C#, which are operations which return a boolean as output. Here are the most important ones:
>
- is greater than
<
- is less than
>=
- is greater than or equal to
<=
- is less than or equal to
==
- is equal to
!=
- is not equal to
&&
- and
||
- or
There is also a special operator which reverses a boolean value (i.e., true
becomes false
and false
becomes true
):
!
- not
There is another keyword relevant to conditional statements: else
. The else
keyword tells the program to perform an action if the previous if
statement evaluated to false
.
if (parsedInput < 3)
{
Console.WriteLine("Input is less than 3");
}
else
{
Console.WriteLine("Input is not less than 3");
}
The if
and else
statements can be combined to else if
.
if (parsedInput < 3)
{
Console.WriteLine("Input is less than 3");
}
else if (parsedInput == 3)
{
Console.WriteLine("Input equals 3");
}
else
{
Console.WriteLine("Input is not less than 3 or equal to 3");
}
Lesson 1 said that a program's role is to automate computations. We can make this considerably easier using loops. A loop is a section of code that tells the computer to repeat command(s) several times. There are two basic kinds of loops: for
loops and while
loops. A while
loop performs a set of commands as long as a condition is true
. A for
loop is actually just a different way to write a while loop, but it is structured with the intention of performing a set of commands a defined number of times.
Here's an example of a while loop:
int i = 0;
while (i < 10)
{
Console.WriteLine(i);
i = i + 1;
}
Before the while loop starts, a new integer called i
is declared and initialized to 0. The while
loop is entered and continues as long as i
is less than 10. During each iteration of the loop, i
is set to i + 1
. If you run this code, you will see that i
starts at 0 and ends at 9. This is easy to observe because we wrote i
to the console at each iteration.
The i = i + 1
step is called incrementing i
, and is often shortened by using the increment operator ++
. Thus i = i + 1
can be replaced with i++
. The decrement operator is --
, which is similar; it decreases the number by 1 (i.e., i = i - 1
becomes i--
).
The generic structure of a while
loop is:
while ([condition])
{
}
The structure of a for
loop looks a little strange:
for (int i = 0; i < 10; i++)
{
Console.WriteLine(i);
}
but it is actually identical to the above while loop example; however, much of the syntax is just compressed into one line. Again, we first declare an integer i
and initialize it to 0. The loop continues as long as i
is less than 10. After each iteration of the loop, i
is incremented.
Thus the typical structure of a for
loop becomes:
for (int i = [starting value]; i < [end value + 1]; i++)
{
}
You can also start at a high value and decrease i:
for (int i = [starting value]; i > [end value - 1]; i--)
{
}
Note the reversal of the <
to >
and the switch from i++
to i--
.
Generally speaking, for
loops are preferred because it is harder to make very bad mistakes, even though syntactically they are more difficult to understand at first compared to while
loops. Try running the example of a while
loop above but remove the i = i + 1;
line. You will notice that the while
loop never finishes; it prints 0 forever. This is called an infinite loop: a loop that never ends. If you write while
loops, the risk of writing a program that never finishes is high. Be extremely judicious in your use of while
loops. Try to avoid using them, and if you do really have to use one, be very careful about your logic to make sure that it will actually end at some point no matter what input is put in to your program.
- For this homework, use
int
andint.Parse
instead ofdouble
anddouble.Parse
to store numbers from the console input. - Write a conditional statement that prints "The input is divisible by 3 and 4" if the input is divisible by 3 and 4. If the number is not divisible by 3 and 4, print "The input is not divisible by 3 and 4".
- Write a
for
loop that prints multiples of 2, ending at 10. - Write a
while
loop that does the same. - Write a new function that computes the factorial of a number. Hint: You will need to use a loop and conditional statement(s). This can be quite challenging for beginners, so seek help from colleagues if you need to. You may want to print intermediate results to the console so you can manually check each stage of your math.
- If you wrote your function with a
for
loop, write another function that computes the factorial using awhile
loop. If you used awhile
loop in the first function, write another using afor
loop.