Modern Java - Writing Code
Modern Java - Writing Code
Written by Ian Elliot   
Thursday, 09 June 2016
Article Index
Modern Java - Writing Code
The flow of control
Conditionals - if


And finally - nesting

This is a lightening overview of the basic ways you can build program in Java - or any programming language for that matter.

This part of learning to program generalises to other languages. Once you have the basics of the default flow of control, conditionals and loops you can start to work on putting them together like basic building bricks used to create more complex things.

In general there are only two ways to put these building bricks together. You can put one after the other and then you have a conditional followed by a loop followed by a conditional.

A more complicated way of combining them is to put one structure inside another - nesting them. So you can have a loop inside a conditional inside a loop etc.

For example:

Suppose you want to print a message to say a number is odd or even. Then you might use something like 

int i;
for (i = 1; i <= 10; i++) {
 if (i / 2 * 2 == i) {
 } else {

You can see that there is a for loop and within it is an if statement. The if statement is nested within the for loop and so repeated ten times. The only "tricky" part of the program is condition within the if statement. If you take an integer like 3 and divide it by 2 the result isn't a fraction but the exact number of times 2 goes into 3 i.e. 1. This is integer division. When you multiply this by 2 you get 2 which is not equal to 3 and the number is odd. If you repeat the procedure using an even number like 4 you get 4/2 is 2 and 2*2 is 4 which is equal to 4 and so the program print even. Integer division may not be accurate but it is often very useful in programming. In general if a number x is even then x/2*2 is x but if it is odd then it is smaller than x - try it.


The more control statements you nest the more complicated the program becomes and this is a bad thing. Over the years we have found ways of avoiding building complicated structures like this  - but this is moving on to future topics. For now simply make sure you understand the conditional and the loop; you will see both used a lot in following chapters.


Modern Java
With NetBeans And Swing



  1. Getting started with Java

    In chapter 1 we tell you how to get started with modern Java development in the shortest possible time. The approach uses NetBeans and Swing and all of the resources used are free to download and use.

  2. Introducing Java - Swing Objects
    In the second chapter of our beginner's guide to Modern Java we find out more about objects by exploring the Swing framework with a simple hands-on example.

  3. Writing Code

    Using ifs and loops is one of the most difficult parts of learning how to program. Our beginners introduction to Java reaches the part all programmers have know and know well - how to write code.

  4. Command Line Programs
    Command line programming means doing things in the simplest possible way. We take a careful look at how data types and code build a program.

  5. User Interface - More Swing
    Finding out how to create a User Interface (UI) using the Java Swing library is not only a useful skill, it also is an ideal way to learn about objects and to make sure that the ideas really have sunk in.

  6. Working With Class
    The Swing components have provided an easy approach to the idea of objects, but there comes a time when you have to find out how to create your own. In this part of Modern Java, we look at the standard ideas of object-oriented programming.

  7. Java Class Inheritance
    Working with classes and objects is a very sophisticated approach to programming. You can't expect to absorb all of its implications in one go. We have already looked at the basics of class and objects. Now we need to look at encapsulation, constructors, overloading and inheritance.

  8. Java Data Types - Numeric Data 
    After looking at some of the advanced ideas of classes and objects we need to return to some simpler topics to make our understanding complete. We need to look more closely at data and, to get things moving, numeric data. 

  9. Java Data Types - Arrays And Strings
  10. Building a Java GUI - Containers
    In this chapter we get to grips with the idea of a container that is used to host components to build a user interface. We also find out how the Swing GUI Builder generates code to make it all much easier. 
  11. Advanced OOP - Type, Casting, Packages
  12. Value And Reference 
  13. Java Lambdas, SAMs And Events



If you would like the code for this project then register and click on CodeBin.

To be informed about new articles on I Programmer, install the I Programmer Toolbar, subscribe to the RSS feed, follow us on, Twitter, Facebook, Google+ or Linkedin,  or sign up for our weekly newsletter.





or email your comment to:






Last Updated ( Monday, 17 October 2016 )

RSS feed of all content
I Programmer - full contents
Copyright © 2018 All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.