Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Do While Statement

    Code:
    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.)
    Code:
     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

  • #2
    New Coder
    Join Date
    Mar 2008
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Code:
     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 ?
    Last edited by jpnv8; 03-31-2008 at 09:03 AM.

  • #3
    New to the CF scene
    Join Date
    Jun 2006
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    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);


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •