Skip to main content

Posts

Showing posts from November, 2012

if else if statement

The if else if loop is useful when there is more than one condition is present. Syntax: if(condition) { ................... statements; .................. } else if (condition) { ................... statements; .................. } .................. .................. else if (condition) { ................... statements; .................. } else  { ................... statements; .................. } In this type of loop we can check any number of conditions. If any of the condition associated with the if else if block is found to be a true, then the statement associated with that block only executes. If a bock is found to be a true one, then all the else if and else blocks coming after the true block is skip from further checking. If non of the condition are found to be true then only the else part executes.  Example: import java.io.*; class VowelCheck { public static void main(St

The if else statement

In an if else  statement we also consider the else part of the Boolean statement. Syntax: if(condition) { ................... statements; .................. } else { ................... statements; .................. } In this loop if the condition is true then the if block only executes and if the Boolean statement is found to be false the the else part executes. Make sure that the else part is the first statement after the body closing  of if   statement. Example: int b = 6; if (b%2!=0) { System.out.println("Value of b is an odd number"); } else { System.out.println("Value of b is an even number"); } Note: If only one statement is present in a loop then you can avoid the brackets of the loop body. Example if (true) System.out.println("Hello i am the only statement in if "); keep in mind that if the brackets associated with any loop body mi

The if Loop

1. The if Loop: It is one of most useful condition checking statement in java. Syntax: if(condition) { ................... statements; .................. } Make sure that the condition is always a Boolean statement. Other than a Boolean statement is not allowed in if loop. If the Boolean statement is true, then only the loop body executes, otherwise skip the statement/statements associated with the loop. Example: int b = 6; if (b%2!=0) { System.out.println("Value of b is an odd number"); }

Conditional and Looping Statements In Java

A condition checking statement helps to execute a statement or block of statement based on certain condition  The execution of the statement or statements in a conditional statement is based on the weather the condition is true or false. The condition checking statements present in java are the following. 1.  simple if . 2. if else.   3. if else if . 4. if else if ladder. 5. Conditional operator. 6. switch case   A loop statement helps the repeated execution of a statement/statements based on a condition. Java support most of the looping statements. The loop statements available in java are given bellow. 1. while loop 2. do while loop 3. for loop 4. for each loop

Methods in Java

Methods are sets of java statements which have a specific meaning. Methods are also known as functions. By using functions a complex program is divided into sets of well defined modules. By using methods in a complex java program, the program become reliable and understandable.  In java the syntax for defining a method is given bellow. Syntax: access_specifier return_type method_name(Formal Arguments) { local variable deceleration; statements; } Exmple: public String getMessage(String msg) { String message; message = msg; return message; } The access specifier may be public, private or protected. The variables declaring inside  a method have scope only within the body of that method. It is recommended that the method name in java started with small letters and if the method name contains more than one words, then separates them with capital letters. A function in java have two parts. 1. Function definition and 2. Function calling

Saving A Java Program

Before saving a program in java, just keep the following points in mind 1. Every java program must be save in  a file with .java extension. 2 .If the program contain only one class then you have to save the program in any appropriate name as you like. Consider the following example. class SavingDemo { public static void main(String args[]) { System.out.println("Display from SavingDemo class"); } } In the above example you can see that only one class is present, so it is possible to save the above program with any file name.  Suppose i save the above program with file name MySavingDemo.java. For compiling this file i use the following command.                     javac MySavingDemo.java You can see that the program is succeessfully compiled using this file name. So now it is the time to run this program. For running this program i use the following command.          java MyDeviceDemo Here you can see that, the jvm will generates

Installing jdk

The first step for starting java programming is to prepare you computer for compiling and running a java source code.  You need to download the jdk (java development kit) for compiling and running a java program in your computer.  For downloading jdk just go to the following link.      Download jdk After the successful installation of jdk just open up the windows directory and open up the program files. Open the folder named java in it, you can see that there are two types of folders are present with names jdk and jre. Steps for setting the environment variable for java 1. Open the jdk folder then open the bin folder in it and copy that path from the   address bar. 2. Now open the system property and got to the advanced system settings. 3. Click the environment variable button in that window. 4. Now click the edit button on the window that appear next. 5. Set variable name as path and paste the java compiler path ( program files--->java-->jdk--->bin ) as  varia

First Program In Java

As usual the first program in java is also the program which shows a greeting message to the user. Here is the first program in java class FirstProgram { public static void main(String args[]) { System.out.println ("HELLO PRABEESH"); } } Output: Explaining the First Program: Everything in java is concern with objects and classes. Here also a class is present with name FirstProgram                                                                 class FirstProgram                     {                     } A class name must be a valid java identifier. It is a good programming practice to start every java class with a capital letter and if more than one word present in the class name then separates them with capital letters.  You can see here also the class name contains two words First And Program . So here i indicates the class name as FirstProgram . Syntax for declaring a class in java is given bellow.                   Access_specifier class Cla