Introduction to Java For Loops

Purpose :
The for statement is similar to the while statement, but it is often easier to use if you are counting or indexing because it combines three elements of many loops: 
initialization, testing, and Increment.

Initialization: It allows the variable to be initialize. Such as: int i = 0; int j = 2; 
Termination (or condition): It allows to check the certain condition. If condition is true then all statements and processes written in the for block will be executed otherwise ignored. Condition such as: i <= 5; i <j; 
Increment: It allows the how much increase the given variable. Such as: i++; j++;

While Statement Introduction in Java

The while statement is used to repeat a block of statements while some condition is true. The condition must become false somewhere in the loop, otherwise it will never terminate. 

Purpose :
Purpose - to repeat statements The purpose of the while statement is to repeat a group of Java statements many times. It's written just like anif statement, except that it uses the while keyword. 

If Statement examples in Java

Example 1 :
If Student marks is grater than equal 60 print "passed" else "failed"

 * To change this template, choose Tools | Templates
 * and open the template in the editor.
package statement;

import java.util.Scanner;

 * @author ACHCHUTHAN
public class Result {

If Statement Introduction in Java

Purpose :
The purpose of the if statement is to make decisions, and execute different parts of your program depending on a boolean true/false value. About 99% of the flow decisions are made with if. [The other 1% of the decisions use the switch statement.]

 General Forms :
The if statement has this form, where condition is true or false.
... // Do these statements before.
 if (condition) {
   ... // Do this clause if the condition is true.
... // Do these statements after.

Java Language Keywords


Here is a list of keywords in the Java programming language. You cannot use any of the following as identifiers in your programs. The keywords const and goto are reserved, even though they are not currently used. truefalse, and null might seem like keywords, but they are actually literals; you cannot use them as identifiers in your programs.

Primitive Data Types and Operations in Java

 Primitive Data Types 
The primitive data types are predefined data types, which always hold the value of the same data type, and  the values of a primitive data type don't share the state with other primitive values. These data types are named by a reserved keyword in Java  programming language. 

Programming Style and Simple "Hello World"program in java

Programming Style and Documentation

  • Appropriate Comments
  • Naming Conventions
  • Proper Indentation and Spacing Lines
  • Block Styles

Appropriate Comments , Naming Conventions

  •   Choose meaning and descriptive names.
  •   Variables and method names:
  • Use lowercase. If the name consists of several                       words, concatenate all in one, use lowercase for the first  word, and capitalize the first letter of   each  subsequent word in the name.
  • For example, the variables radius and area, and the method compute Area.

Naming Conventions, cont.

  • Class names:
  • Capitalize the first letter of each word in the name. For example, the class name ComputeArea.
  • Constants:
  • Capitalize all letters in constants. For example, the constant PI

Proper Indentation and Spacing

  •  Indentation
  • Indent two spaces.
  •  Spacing
  • Use blank line to separate segments of the code.

Programming Errors

  • Syntax Errors
  • Detected by the compiler
  • Run time Error
  • Causes the program to abort
  • Logic Errors
  • Produces incorrect result

Creating Your First Application

  • A source file contains code, written in the Java programming language, that you and other programmers can understand. You can use any text editor to create and edit source files.
  • The Java programming language compiler (javac) takes your source file and translates its text into instructions that the Java virtual machine can understand. The instructions contained within this file are known as bytecodes.
  • The Java application launcher tool (java) uses the Java virtual machine to run your application.

Create a source file

To create a source file, you have two options:
  • You can save the file on your computer and avoid a lot of typing. Then, you can go straight to Compile the Source File into a .class File.
  • Or, you can use the following (longer) instructions.
First, start your editor. You can launch the Notepad editor from the Start menu by selectingPrograms > Accessories > Notepad. In a new document, type in the following code:
import java.util.*;
public class Welcome
public static void main(String[ ] args){
System.out.println("Hello World");
  1. Using the Save in combo box, specify the folder (directory) where you'll save your file. In this example, the directory is java on the C drive.
  1. In the File name text field, type "", including the quotation marks.
  1. From the Save as type combo box, choose Text Documents (*.txt).
  1. In the Encoding combo box, leave the encoding as ANSI.

Compiling Programs

  • On command line
  • Bring up a shell, or "command," window. You can do this from the Start menu by choosing Command Prompt (Windows XP), or by choosing Run... and then entering cmd. The shell window should look similar to the following figure.The prompt shows your current directory. When you bring up the prompt, your current directory is usually your home directory for Windows XP (as shown in the preceding figure.To compile your source file, change your current directory to the directory where your file is located. For example, if your source directory is java on the C drive, type the following command at the prompt and press Enter:
cd C:\java
  • Now the prompt should change to C:\java>.
  •  Compile:
  • javac
  •  Run:
  • java HelloWorld

Include a summary at the beginning of the program to explain what the program does, its key features, its supporting data structures, and any unique techniques it uses.
Include your name, class section, instruction, date, and a brief description at the beginning of the program.
Your first application, HelloWorldApp, will simply display the greeting "Hello world!". To create this program, you will: 
  • Create a source file
  • Compile the source file into a .class file
  • Run the program

Save the code in a file with the name To do this in Notepad, first choose theFile > Save As menu item. Then, in the Save As dialog box:

    • Output:
           The program prints Hello World! to the screen.

Copyright © 2010-2019 Java Examples

Designed by Templateism. Built with Blogger Templates.