Java for Beginners

Java is a very popular programming language and it is used in a variety of occupations. Here is a quick tutorial for those of you who want to start learning Java.

Variables

Variables in Java are used to hold data, you can think of them as containers. For example, if you wanted to create a variable to hold a number, you would use the int data type. The main data types in Java are boolean, char, int, double, and String. Declaring a variable is very simple, all you have to do is type dataType variableName = value;  For example, if you wanted to create a variable that holds the value of whether it is sunny or not, you would type boolean isSunny = false; This tells us that it is not sunny.

Conditional Statements

Conditional statements are used when you want to execute statements based on a condition. For example, if you wanted to tell the user to go outside if it was sunny, you would have to use an if statement. In this case, the code will look like this.

if(isSunny == true) {
   System.out.println("You should play soccer today");

else {
   System.out.println("You should still play soccer unless it is raining");
}

First, the if statement checks if it is in fact sunny outside. If it is sunny, then the computer writes the statement to the log; otherwise, the computer writes the second statement to the log. You can also use an else if statement if you wanted to have three or more conditions.

Loops

There are two main types of loops: the for loop and the while loop. You want to use the for loop when you know how many times you want to run a series of statements, and you want to use a while loop when you do not know. For example, if you wanted to print the numbers 1-100, you would use a for loop. This is because you know how many times the loop should iterate.

for(i=1; i<=100; i++)
{
   System.out.println(i);
}

The first statement is the header of the loop and it tells the loop how many times it needs to iterate. i=1 initializes the counter variable to 1, i<=100 tells the loop to iterate until i is less than or equal to 100, and finally i++ increments the counter variable by one for each iteration of the loop. The body of the loop simply tells the computer to print out the value of i for each iteration.

The next type of loop is the while loop, and this is used when you do not know the number of times the loop should iterate. The format for the while loop looks like this

while (true){
    statement(s);
}

 

The while loop continues to iterate while the condition in the header is true. Be careful, you need to make sure the condition in the header eventually becomes false. Otherwise, the loop will turn into an infinite loop (a loop that runs forever).

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.