Control Structures in Java

The control structures in java will be used to modify the sequential flow of a program. That is, the order in which the instructions on it are executed.

In a program, instructions are executed from top to bottom, and from left to right, depending on the order in which they appear. The different instruction blocks of our program will follow this sequential flow to execute the instructions. The instruction blocks shall be delimited by braces brackets:

Block {
instruction 1;
instruction 2; instruction 3;
instruction 4;
}

However, in some cases we will need to break this sequential flow. Reasons for that can be many, we may need to make a decision between one or several possible options, or maybe we need to perform some instructions several times. Therefore, we need certain structures that allow us to interrupt the sequential flow of the program, and move on to execute other blocks of code.

In this post, we will study the operation of the following control structures:

  • Decision making: if-then, if-then-else, switch.
  • Loop: for, while, do-while

Decision making structures

Continue reading Control Structures in Java

Exercises with variables in Java

After the previous post about variables in java, in this one, I leave some exercises to practice what I explained then. This will be the way of acting for the rest of the Java topics, firstly I explain the topic and then, in the following post, I will propose a series of exercises and their solutions.

There are several possible solutions to solve the same problem, some more efficient than others. Here you can see a possible solution for each proposed exercise, which is not the only one possible.

Continue reading Exercises with variables in Java

Variables in Java

In this post, I am going to talk about variables in Java. I will explain how to declare and initialize them, the types of primitive variables that we handle in Java, how to change the type of a variable and its scope.
But before we start, we must introduce a series of preliminary concepts:
• A variable is a container, a memory location, that stores a value while a program is running.
• We put a name to each variable, so that we can reference it in our code, and use the value it stores.
• Variables are of one type. The type of data that we intend to store in the said variable. For example, if a variable is of type int, it will be used to store integer values. However, if it is of type String, it is used to store a string of characters, that is, text.
• Variables have to be declared. In this declaration, we indicate the type of the variable and its name.
• A variable has to be initialized to be used in our program. Initializing a variable is putting a value in it.

Continue reading Variables in Java

Starting with Java

As we are starting with Java, the first thing we will have to do is download the official version of the JDK (Java Development Kit). For this, we are going to go to the Oracle website, which is the owner of Java, and we download the JDK.

Oracle recommends NetBeans as IDE (Integrated development environment) for programming with Java. So in this course, we will use said IDE that you can download together with Java from this URL.

However, for the first lessons of the course, we will not perform any installation, we will use an online Java compile, with which we can practice from our browser.

Compiling and running Java Online.

There are several Java compilers online. Just type in Google: “java online compiler” and we will list several actions.

Continue reading Starting with Java

Exercises of introduction to programming

In this post, I propose two exercises of introduction to programming, the first topic of my Java notes. It is about thinking, proposing a solution to the proposed problem, and then translating it into a flow diagram and pseudocode.

There are several possible solutions to solve the same problem, some more efficient than others. But of course, before we start programming, we have to propose the solution to the problem that we want to solve by programming.

Here you can see a possible solution for each proposed exercise, which of course is not the only one possible and which is probably not the most efficient.

Continue reading Exercises of introduction to programming

Introduction to programming

Before I start programming in Java, I explain to my students an introduction to programming. I tell you what programming consists of, what an algorithm is, a flow chart and a pseudo-code. But above all, I insist that programming is about solving problems.

Preliminary concepts

Several concepts should be internalized before starting to program. At least we should be clear about the following:

Continue reading Introduction to programming