Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

Nested Loops

Nested loops are quite helpful in processing information. We will learn how nested loops are used by taking the example of a pattern problem. An integer value is to be taken as an input from the user and the following pattern is to be printed based on the input. If the input is seven, the following pattern needs to be printed.


One thing that is obvious on observing this pattern is that loops are used to print the pattern. This is because, we are not aware in advance of the number of *'s that are to be printed. Further, we need nested loops: a for loop nested within another for loop. the outer for loop keeps track of the line number. Or, in other words, it is used to count the number of lines we are printing. The inner for loop is used to keep track of the number of *'s we are printing. Urther, the number of stars on a particular line is equal to the number line number which in some way can be realted to the outer loop control variable. keeping these things in mind, we can write the following code which prints this pattern. The number n is atken as an input from the user:

for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= i; j++) {

Note that we have used the print() method and not the println() method to display the stars as we want the *'s to be displayed on the same line. And after the inner loop is executed, we go to the next line using the println() statement.

Privacy Policy