Day to Day: Learning Java Programming Language – Part 2

Moving a step ahead of the previous article on Day-to-Day: Java Programming Part – I. Here in this very post we will be learning control statements and loops in Java, which is very useful in developing an application.

Learning java Programming
Learning java Programming Part – 2
if statement

The if statement in Java works similar to if statement in any other programming language of the world including shell scripting.

Program 3:

class compare{ 
public static void main(String args[]){ 
int a,b; 
if(a < b)  
System.out.println("a(" +a+ ")is less than b(" +b+")");  
System.out.println("a(" +a+ ")is equal to b(" +b+")");  
System.out.println("a(" +a+ ")is greater than b(" +b+")"); 

Save it as: And Compile it and run as shown.

# javac
# java compare

Sample Output

a(10)is less than b(20) 
a(20)is equal to b(20) 
a(40)is greater than b(20)

Note: In the above program

  1. A class namely compare is defined.
  2. Two Integers are declared with the initial value of 10 and 20 respectively.
  3. The if statement checks the condition and act according to the statement. Syntax of if statement is if (condition) statement;
  4. System.out.println prints anything and everything that is placed between double quotes. Anything within the quotes are printed as it is, and outside of quotes are treated as variable.
  5. + is a concatenation, which is used to concatenate two parts of a statement.
for loop

If you have any programming experience, sure you would be aware of the importance of loop statements. Here again the for loop statement works similar to the for statement in any language.


class forloop{ 
public static void main(String args[]){ 
int q1; 
for (q1=0; q1<=10; q1++) 
System.out.println("The value of interger: "+q1); 

Save it as: And Compile it and run as shown.

# javac
# java forloop

Sample Output

The value of interger: 0 
The value of interger: 1 
The value of interger: 2 
The value of interger: 3 
The value of interger: 4 
The value of interger: 5 
The value of interger: 6 
The value of interger: 7 
The value of interger: 8 
The value of interger: 9 
The value of interger: 10

Note: In the above program all the statements and codes are more or less identical to the above program, except the for statement.

  1. The above for statement is a loop, which continues to execute again and again till the conditions are satisfied.
  2. The for loop, generally is divided in three chunks of codes separated by semicolon, each of which is very meaningful.
  3. The first part (q1=0, in the above program) is called initialiser. i.e., the above integer, q1 is forced to start with ‘0‘.
  4. The second part (q1<=10, in the above program) is called condition. i.e., the above integer is permitted to go up-to the value of 10 or less than 10, which ever is correct for the given situation.
  5. The Third and the last part (q1++, in the above code, which may be written as q+1) is called iteration.i.e., the above integer value is asked to increase with a value of ‘+1‘ every time the loop is executed, till the condition is satisfied.

Well the above program has only one linked statement to the ‘for loop‘. But in larger and more sophisticated program the loop statement could be linked to more than one statement or say a block of codes.

Program 5:

class loopblock{ 
	public static void main(String args[]){ 
		int x, y=20;		 
		System.out.println("x is: "+x); 
		System.out.println("y is: "+y); 

Save it as: And Compile it and run as shown.

# javac
# java loopblock

Sample Output

x is: 0 
y is: 20 
x is: 2 
y is: 18 
x is: 4 
y is: 16 
x is: 6 
y is: 14 
x is: 8 
y is: 12 
x is: 10 
y is: 10 
x is: 12 
y is: 8 
x is: 14 
y is: 6 
x is: 16 
y is: 4 
x is: 18 
y is: 2

Note: The above program is almost the same as the previous program, except it uses a block of codes linked with for loop. To execute more than one statement/block, we need to put all the statement as “{….codes/block..}” else the code won’t compile correctly.

Yeah we can use ‘x- –‘ or ‘x-1‘ for decrease statement in for loop where required.

After getting a glimpse of whole lot of codes, we need to know a little theory which will be helpful in the later stage of coding’s.

What we have seen till now is: Java programs are a collection of Whitespaces, identifiers, comments, literals, operators, separators and keywords.


Java is a free form language, you need not follow any indentation rule. You could write all the codes on a single line with one whitespace between each token and it will execute correctly. However it will be difficult to understand.


In Java identifiers are class name, method name or variable name. It could be uppercase, lowercase, their sequence or a combination of all of these with special characters like ‘$‘. However identifiers should never start with a numerical values.

Examples of valid identifiers in Java:

s4, New#class, TECmint_class, etc.

A constant value in Java is created using literals. e.g., ‘115′ is an integer literal. ‘3.14‘ is a float literal, ‘X‘ is a character constant and “tecmint is the best online site dedicated to foss technology” is a string literal.


comment has nothing to do with the execution of codes in Java or any other language, however comment in between the codes make them readable and human understandable. It is a good practice to write comments in between the lines of code, where required.

In Java anything between /** and **/ is meant for documentation and is a comment.

Certain separators are defined in Java.

  1. Parenthesis ()
  2. Braces {}
  3. Brackets []
  4. Semicolon ;
  5. comma ,
  6. Period .

Note: Each separator has a meaning and needs to be used where required, You can’t use one in place of other. We will discuss them in details, in the later phase of codes itself.


There are 50 reserved keywords defined in Java. These keywords can not be used as names for a variable, class or method as these keywords has predefined meaning.

abstract	continue	for	          new	        switch
assert	        default	        goto	          package	synchronized
boolean	        do	        if	          private	this
break   	double	        implements	  protected	throw
byte	        else	        import	          public	throws
case	        enum	        instanceof	  return	transient
catch	        extends	        int	          short	        try
char	        final	        interface	  static	void
class	        finally	        long	          strictfp	volatile
const	        float	        native	          super	        while

The keyword cons and keywords are reserved but not used. Feeling nervous with all these stuffs. You actually don’t need to be nervous, neither you need to memorise all these stuffs. You will be used to all these when you start living Java.

That’s all for now from me. Do not forget to tell us how you felt the article was, in comment section. I will be coming up with the next part of this very series, soon. Till then keep connected to Tecmint, stay tuned and healthy.

If you liked this article, then do subscribe to email alerts for Linux tutorials. If you have any questions or doubts? do ask for help in the comments section.

If You Appreciate What We Do Here On TecMint, You Should Consider:

TecMint is the fastest growing and most trusted community site for any kind of Linux Articles, Guides and Books on the web. Millions of people visit TecMint! to search or browse the thousands of published articles available FREELY to all.

If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.

Support Us

We are thankful for your never ending support.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.

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