Hello World,
Today we will be discussing conditionals. These
conditionals were pretty easy for me to understand. Basically you have 3
conditionals, or at least 3 that we will cover today. They are “if”, “else if”,
and “else”. You may be familiar with these conditionals if you have had any
programming experience before in a common programming language.
Conditionals are commonly used to test a comparison. I
made a basic program that uses user input to test which number is the largest
of 3 numbers. Of course anyone can tell you which number is largest off first
glance of 3 numbers. However, this program uses all 3 conditionals in an easy
to understand way.
Below is my program.
Basically, it starts by asking the user to enter 3
numbers. The numbers are instance variables as learned in a previous post. Then
there are some conditionals. A, b, and c correspond to the first number
inputted, the second number inputted, and the third number inputted
respectively. The first tests if a is larger than both c and b. If it is, then
it is displayed as the largest. Then within each if and if else statement,
there is a test to see if the highest number is positive or negative. If it is
greater than zero, it is positive. Otherwise it is negative (else). The second
conditional asks if c is larger than b and a. If it is, the program figures out
if it is negative or positive again. The third conditional asks if b is larger
than both a and c, then figures out if it is negative or positive. Then the
last else if statement tests if a is larger than b, but c is larger than b,
then tests the negative and positive.
This is how a potential output may look.
Hope you enjoyed this lesson. Let me know if anything
is unclear.
Thanks!