...

View Full Version : Do While Statement



jpnv8
03-29-2008, 03:41 AM
import java.io.*;

public class DoWhile

{
public static InputStreamReader reader= new InputStreamReader (System.in);
public static BufferedReader input= new BufferedReader (reader);
public static void main (String [] args)
throws Exception

{

int x=0; //when do we need to initialize a variable

//DO-WHILE STATEMENT

do{
x++;
System.out.println(x);

} while(x<3);

A.)

int x=0; //when do we need to initialize a variable
-When do we need to initialize a variable? Sometimes int x,y; works without assigning a value to it. thx

jpnv8
03-31-2008, 09:00 AM
int x=0; //when do we need to assign a value to variable. Sometimes int x,y; works without assigning any value to it. thx

sorry .wrong grammar ?

solata
03-31-2008, 04:06 PM
In your case x must have the value assigned, becouse later in the do while statment you incrase its value by one. What would the statment increase if there would be no value assigned to x?

Here are some examples:

import java.io.*;

public class DoWhile

{
public static InputStreamReader reader= new InputStreamReader (System.in);
public static BufferedReader input= new BufferedReader (reader);
public static void main (String [] args)
throws Exception

{

int a; //it doesnt need a value assigned as it gets a value later
int x; //same here
int y=0; //this one needs a value


x=y; //x gets a value


do{
x++;
System.out.println(x);
a=x; //a gets a value here
} while(x<3);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum