Showing posts with label java. Show all posts
Showing posts with label java. Show all posts

Sunday, 18 March 2012

If Else statement


If statement is very powerful decision making statement and is used to control the flow of execution of statement .
If we want to execute a statement on some condition .  than we should use the if statement
Syntax  :-
If(Condition){
//Statement
}
Example :-
If(a<b){
System.out.println(a is smaller than b);
}
In above example first it check that if a is less than b only if it is true then next statement will be executed  otherwise no statement will be executed.
If statement can be implemented in the following  ways:-
1.       Simple if statement
2.       If else statement
3.       Else if ladder
4.       Nested if else statement

1)      Simple if statement :-
If (condition){
Statement - block - x
}
                Statement –block – y
                In this is if the condition is true  then statement block x will be executed  and then statement block y will be executed, otherwise the statement  block x will be skipped only statement block y will be executed.
Example :
Class SimpleIf{
Public static void main(String args[]){
Int a=10,b=5;
If(a>b){
System.out.println(“a is greater”);
}
System.out.println(“Thanks for coming”);
}
}
Output:
a is greater
Thanks for coming

IF Else statement:-
If(condition)
{
Statement – block -x
}
else
{
Statement – block – y
}
Statement-z
In this if the condition is true then statement block x will be executed and statement block y will be skipped and then statement z will be executed and condition will false then statement block x will be skipped and statement block y will be executed and statement z will be executed.
Nested  if else:-
If(condition 1){
                If(condition 2){
Statement  -1
}
else{
statement - 2
}
}
else{
statement -3
}
In the above example if condition-1 is false then statement -1 and statement-2  will be skipped and only statement -3 will be executed and if the condition -1 will true then condition-2 will be checked and if condition -2 will be true then statement -1 will be executed otherwise statement -2 will be executed.
Example
Int  a=5;
Int b=10;
Int c=15;
If(a>b){
                If(a>c){
System.out.println(“a is greater”);
}
else{
System.out.println(“c is greater”)
}
}
else{
if(b>c){
System.out.println(“b is greater”);
}
else{
System.out.println(“c is greater”);
}

}
Else if ladder:-  If we want to use multiple decision in our program then we have an another option that is else if ladder.
Syntax:
If(condition 1) {
Statement 1
}
                else if(condition 2){
                Statement 2
}
else if(condition 2){
                Statement 2
}
else{
Statement  3
}
In this all the condition is evaluated from top to down which condition is that statement will be executed if all condition will be false then final else statement will be executed.
Example
If(marks>90){
System.out.println(“First division”)
}
                else If(marks>80){
System.out.println(“Second division”)
}
else If(marks>60){
System.out.println(“Third division”)
}
else {
System.out.println(“Pass”)
}
This will end our if statement

Friday, 9 March 2012

Loop in java


Looping is used to develop programs which are having some repetitive process. When a particular block of statement have to reapete then loops are used . it makes the programmer job easier , because instead of writing same lines again and again , loop can be used.
 Loop is consist of  two segments one is body and other is exit condition . exit condition is a condition which will end the loop. Means the sequence of statement will reapete until the condition is true.
A looping process have four main steps , which are as follows:
1)      Setting and initialization of a counter
2)      Execution of statements
3)      Test for condition that whether  the condition is true or false
4)      Increment/decrement counter;
Java supports three type of loop statements :
While
Do while
For
While loop:- while loop is an entry-controlled loop statement . at first step the test condition is evaluated , if it is true then the statement is executed and if it is false the exit from loop. After execution of statement again the condition is evaluated and this process is repeated until the condition is true.
Syntax of while
Initialization :
While (test condition){
Body of loop
Increment/decrement
}  
Example of while loop
class whileLoop{
                public static void main(String args[]){
int i=0;
while(i<5){
System.out.println(i);
i++;
}
}
}
Output:
0
 1
2
3
4
Do while loop:-
In while loop first condition is tested then body of loop is executed .In in do while loop first the body of loop is executed and then condition is executed. So the minimum chance of execution of while loop is 0 an the minimum chance of execution of do while loop is 1. Because whether the condition is true or false the statement will be executed atleast for one time.
Syntax of do while loop
Initialization
do
{
Body of loop
}
While(test condition)
We can use nested loop that is loop inside loop according to requirement .
class doWhileLoop{
                public static void main(String args[]){
int row, column, x;
row=1;
do{
column =1;
do{
x=row*column;
column=column+1;
}while(column<=3);
System.out.println();
Row=row+1;
}while(row<=3);
}
}
Output
1    2   3
2    4    6
3   6     9

For loop:-
For loop is most widely used loop and very useful loop .because in for loop initialization , condition and increment/decrement are in a single row.
Syntax of for loop
For(initialization; test condition; increment/decrement){
Body of loop
}

Example of for loop:
public class foorloop {
      public static void main(String[] args) {
            for(int i=1;i<=10;i++){
                  System.out.println("2  * " +i + "=" + 2*i );
            }
      }
}

Output
2  * 1=2
2  * 2=4
2  * 3=6
2  * 4=8
2  * 5=10
2  * 6=12
2  * 7=14
2  * 8=16
2  * 9=18
2  * 10=20

We can use nested loops that is loop inside loop as many as required . There is no limit of nesting loops .

Thursday, 8 March 2012

What is jsp


JavaServer Pages (JSP) is a server-side technology that allows developers to create Web-based applications that use Java components.
JavaServer Pages (JSP) technology is technology for delivering dynamic content to web clients in a secure and well-defined way.
Servlets are powerful and sometimes they are a bit cumbersome when it comes to generating complex HTML. Most servlets contain a little code that handles application logic and a lot more code that handles output formatting. . For these reasons, web application developers turn towards JSP as their preferred servlet environment
The JavaServer Pages specification extends the Java Servlet API to provide web application developers with a robust framework for creating dynamic web content on the server using HTML, and XML templates, and Java code A JSP page is a text based document containing static HTML and dynamic actions which describe how to process a response to the client in a more powerful and flexible manner. Most of a JSP file is plain HTML but it also has, interspersed with it, special JSP tags.

Documents the JSP file, but is not included in the response                                
<%-- comment --%>
No equivalent.
Declares variables or methods valid in the page’s scripting language.
<%! declaration; [ declaration; ]+ ... %>               
<jsp:declaration>
declaration [ declaration; ]+ </jsp:declaration
Contains an expression valid in the page’s scripting language.
<%= expression %> 
<jsp:expression>
expression
</jsp:expression>
Contains a code fragment valid in
the page’s scripting language.. 
<% code fragment %>             
<jsp:expression>
code fragment
</jsp:expression>
To process a JSP file, we need a JSP engine that can be connected with a web server Firstly when a web browser seeks a JSP file through an URL from the web server, the web server recognizes the .jsp file extension in the URL requested by the browser and understands that the requested resource is a JavaServer Page. Then the web server passes the request to the JSP engine. The JSP page is then translated into a Java class, which is then compiled into a servlet.
This translation and compilation phase occurs only when the JSP file is requested for the first time, or if it undergoes any changes to the extent of getting retranslated and recompiled. For each additional request of the JSP page thereafter, the request directly goes to the servlet byte code, which is already in memory. Thus when a request comes for a servlet, an init() method is called when the Servlet is first loaded into the virtual machine, to perform any global initialization that every request of the servlet will need. Then the individual requests are sent to a service() method, where the response is put together. The servlet creates a new thread to run service() method for each request. The request from the browser is converted into a Java object of type HttpServletRequest, which is passed to the Servlet along with an HttpServletResponse object that is used to send the response back to the browser. The servlet code performs the operations specified by the JSP elements in the .jsp file.
Example of a jsp file
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
      <form action="abc">
<%    int a=2;
      int b=3;
            if(a<3){
            out.print(a);
            }
            else{
                  out.print(b);
            }
%>
      </form>
</body>
</html>
Related Posts Plugin for WordPress, Blogger...