Going into sophomore year, the most coding experience I had from inside of a classroom was block coding on scratch. In the two years since I started coding in the computer science principles class, I’ve realized that coding is incredibly easy. In my opinion, the hardest part of coding is knowing what you want to do with your program. While I think the thought process is the hardest part, I do admit that there were a few things about coding that I wish I had known while learning. Instead of talking about obscure facts in coding, I decided to write a whole article about how to read and write code (in Java) starting from zero. I will be using Visual Studio Code to show my code segments.
First things first, you need to know how to read code. Reading code is quite like reading a book. From top to bottom, left to right. Every time there is a new line, treat it like a new sentence. When debugging—or fixing errors in your code—make sure to read the code line by line. Something that helps me is to read the code out loud because just like when you read out loud, you could hear the grammatical errors.
The next thing that one should know is the difference between different data types. Data types are different types of variables that are used to store different types of information. The main data types are the following:
The simplest way to interact in your code is a print statement. A print statement is a command that displays information on the screen. Basically, it’s the universal way to show text on your computer screen. The basic format is the following:
As you can see, we used the string that we previously created inside of the statement. While this time, I decided to use the AyalaBDT string, any of the other variables would be usable in this situation. If you’d like, you could even manually code in a message as long as you format it correctly such as the following:
One more thing to note about print statements is that if you change the “print” to “println”, the printed statement will go onto a new line. You can think of this as “print line new.”
The next major topic to cover is math. You heard it right, there’s math in coding. If you’ve passed 3rd grade, chances are you’ll be fine during this next section. In coding, there are the basic arithmetic operations of addition (+), subtraction (-), multiplication (*), division (/), and exponentials (^). The one tricky part of coding operations is the one that most people have never heard of, modulus (%). The way modulus works is that it is a basic division operator, but instead of returning what a number is divisible by, it returns the remainder. Congratulations, you have successfully learned 90% of the math needed to code in Java.
A cool thing about Java is that it was coded by geniuses who knew people would be lazy. That’s why they created functions to make our lives easier. Here is an example of a few math operations that have shortcuts.
The final thing I want to go over in this article is conditional statements. Conditional statements are how your code makes decisions. If a condition is met, then the code executes a conditional statement. If the condition is not met, the code could ignore the statement, or execute a part of the code that only happens when the condition is not met. To put it simply, if a condition is met, then something will happen. There are a few types of operators used in conditionals such as less than (<), greater than (>), equal to ( ==), less than or equal to (<=), and greater than or equal to (>=). Here is the format of a basic if statement:
So if x is greater than 1, the system will print, “Your answer is greater than 1!”
If x is not greater than 1, but instead less than 1, the system will print, “Your answer is less than 1!” If your answer is neither of those, the system will print, “Your answer is 1!”
That’s the basics to coding. Now, you should be able to create a simple program that can made decisions. While there are so many things that still need to be gone over, I’m afraid it will have to be in my next article. I plan on talking about user inputting data, combination operators, repetition, lists, arrays, 2D arrays, and hashmaps. If there is anything in particular anyone would like to learn about, feel free to leave a comment. Have a good day!