Let’s Learn How To Code — Part 002

Cody Engel
5 min readJun 6, 2017

--

Welcome back future programmers, if this is your first time seeing this series I strongly encourage you to check out Let’s Learn How To Code — Part 000 to make sure you’ve properly been introduced to this series. Then I’d recommend going through each article until you find yourself back here.

Programming is almost exactly like flying a kite… Not really, I just enjoyed the picture from Denin Williams on Unsplash.

In this article I’m going to introduce you to control flow but it will likely take several more articles to fully cover all of them. Essentially control flow is the way you control what code your program will execute in specific scenarios, it may also determine how many times to execute the code. Understanding and mastering flow of control is incredibly important and so if you do have any questions please don’t hesitate to ask questions in the comments.

So let’s get started, we’re going to a program which takes user input and tells them whether the number is even or odd. We’re going to build off of our knowledge of modulus to build this. As we did in the past, create a new package named part_002 and then create a new Kotlin file named app.

fun main(args : Array<String>) {

val userInput = Scanner(System.`in`)

// Prompt the user to enter a number.
System.out.print("Enter a number to evaluate: ")
val number = userInput.nextLine().toInt()

// If it is divisible by 2, then it's even

// Otherwise it is odd
}

Alright so to start out we need to request the user for input from their keyboard. This ends up looking very similar to what we have already done in the past (yay for programs that build off of each other). So now we have to worry about how to display a message to the user based on whether the number of divisible by two or not. In order to do this we will have to learn about choices with control flow, you may also hear these referred to as conditional expressions. Within Kotlin it is phrased as:

if (some condition) {
then do something
} else if (some other condition) {
then do something if this condition is true but the first one
wasn't
} else {
then do something else if none of the above conditions are true
}

These conditions must resolve to a boolean value, a boolean can only have two values: true or false. These conditions will typically boil down to one of two categories: comparing numbers or comparing strings. Okay I’ll flesh out the example a little more before we continue with our program.

if (number > 21) {
// wow, number is greater than 21!
} else if (number >= 10 && number < 20) {
// wow, number is greater than or equal to 10 AND less than 20!
} else if (number == 0 || number != -1) {
// wow, number equals 0 OR number does not equal -1!
} else {
// okay, so most likely the number is -1
}
// Now we are outside of the if/else if/else block

So in this example we’ve gone over a few operators which include greater than (>), greater than or equal (>=), less than (<), equals (==), and does not equal (!=). Another operator not covered is less than or equal to which can be denoted as <=.

You’ll also notice some symbols breaking up conditions within the else if statements, those are known as and and or which mean a similar thing to what they mean in English (wow that sentence seems weird, sorry). If you want to use an and it’s denoted by two ampersands (&) and if you want to use an or it’s denoted by two pipes (|). If you and two statements together then they both must be true for your if/else if condition to resolve to true otherwise it will be skipped. Likewise if you only require that one of the conditions is true then you can or the conditions together.

The final thing I want to highlight is that code will read from top to bottom, left to right. Once one of the if/else if blocks resolves to true the code within the curly braces will run and then it will skip the rest of the if/else if/else block. For example, if the number was 22 then the code within the first set of curly braces would run and then the rest of code within the other brackets would be ignored, even if the number satisfies one of the other conditions.

Now that we are armed with that knowledge how might we write our program? The way I would write this program looks something like this:

fun main(args : Array<String>) {

val userInput = Scanner(System.`in`)

// Prompt the user to enter a number.
System.out.print("Enter a number to evaluate: ")
val number = userInput.nextLine().toInt()

// If it is divisible by 2, then it's even
if (number % 2 == 0) {
System.out.println("$number is even!")
} else { // Otherwise it is odd
System.out.println("$number is odd!")
}

}

So we first check if the number modulo 2 is equal to 0 and if it is then we say the number is even. Else (otherwise) we know the number must be odd so we print out that the number is odd. Let’s go ahead and run this program a few times to see what the output is.

Just in case you forgot how to run the program.

You should notice that when you give the program an even number it prints out “yourNumber is even!” Then if you give the program an odd number it should print out “yourNumber is odd!” Keep in mind that the maximum value for integers is 2,147,483,647 and if you give the program a number larger than that it will crash. If you want to test larger numbers then I suggest using a long instead.

So that’s it for now, until our next program I strongly suggest that you play around with this program more and add some more conditions to give additional output. Maybe test if a number is even and greater than some value or if a number is odd and less than some other value. Once you are ready feel free to continue onto part 003.

--

--