class DoWhileDemo {
    public static void main(String[] args){
        int count = 1;
        do {
            System.out.println("Count is: "
                               + count);
            count++;
        } while (count <= 11);
    }
}

can someone please rxplain to me how does Do while statements work?
and how can i use it in my program?:

public static void main(String[] args) {
	
    String a =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
   
   String B= "B";
   String D="D";
   String W="W";
   
   int balance= 0;
   int deposit = 0;
   int withdraw= 0;
   int bal = 0;
   int wht = 0;
int ans;
int trans = 0;
   
   if(a.equalsIgnoreCase(B))
	{
		JOptionPane.showMessageDialog(null, "Your Balance is: ","BALANCE INQUIRY",JOptionPane.PLAIN_MESSAGE );
		ans=JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?","Generate Bank Slip",JOptionPane.YES_NO_OPTION);
		
		if (ans == 1){ //no
	JOptionPane.showConfirmDialog(null,"Would You Like Another Transaction?","Another Transaction",JOptionPane.YES_NO_OPTION);
		if (trans == 1)
		{
		JOptionPane.showMessageDialog(null, "Thanks For Using My Program!!");
		}
		else if (trans == 0)
		{ 
			 String b =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
		
		
		
		
		}
		
		}
	
		else if (ans == 0){ // yes
			JOptionPane.showMessageDialog(null, "BALANCE INQUIRY \n AMOUNT BALANCE: "+ balance+ "\nPrinting Bank slip.....");
		
		
		
		
		}
	
	
	
	}
   
   
   
   
   
   
 
    else if (a.equalsIgnoreCase(D))
    {
	
	int dep =Integer.parseInt(JOptionPane.showInputDialog (null,"Enter Your Deposit Amount" )); 
	
	balance = dep - wht;
	
	ans=JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?","Generate Bank Slip",JOptionPane.YES_NO_OPTION);
	
	if (ans == 1){ //no
JOptionPane.showConfirmDialog(null,"Would You Like Another Transaction?","Another Transaction",JOptionPane.YES_NO_OPTION);
	if (trans == 1)
	{
	JOptionPane.showMessageDialog(null, "Thanks For Using My Program!!");
	}
	else if (trans == 0)
	{ 
		 String c =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
	
	
	
	
	}
	
	}

	else if (ans == 0){ // yes
		JOptionPane.showMessageDialog(null, "DEPOSIT INQUIRY \n AMOUNT DEPOSIT: "+ dep+ "\nPrinting Bank slip.....");
	
	
	
	
	}
	
    }
	
    else if (a.equalsIgnoreCase(W))
	
    	 wht= Integer.parseInt(JOptionPane.showInputDialog(null," Enter the Withdrawal Amount:"));
	 
   
   if (balance <  wht){
	   JOptionPane.showMessageDialog(null, "Insufficient Amount");
   }
   else if (balance >= wht){
	   
   }
   
   
	}

}

thank u, please dont get tired answering my questions!

Recommended Answers

All 2 Replies

class DoWhileDemo {
    public static void main(String[] args){
        int count = 1;
        do {
            System.out.println("Count is: "
                               + count);
            count++;
        } while (count <= 11);
    }
}

can someone please rxplain to me how does Do while statements work?
and how can i use it in my program?:

public static void main(String[] args) {
	
    String a =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
   
   String B= "B";
   String D="D";
   String W="W";
   
   int balance= 0;
   int deposit = 0;
   int withdraw= 0;
   int bal = 0;
   int wht = 0;
int ans;
int trans = 0;
   
   if(a.equalsIgnoreCase(B))
	{
		JOptionPane.showMessageDialog(null, "Your Balance is: ","BALANCE INQUIRY",JOptionPane.PLAIN_MESSAGE );
		ans=JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?","Generate Bank Slip",JOptionPane.YES_NO_OPTION);
		
		if (ans == 1){ //no
	JOptionPane.showConfirmDialog(null,"Would You Like Another Transaction?","Another Transaction",JOptionPane.YES_NO_OPTION);
		if (trans == 1)
		{
		JOptionPane.showMessageDialog(null, "Thanks For Using My Program!!");
		}
		else if (trans == 0)
		{ 
			 String b =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
		
		
		
		
		}
		
		}
	
		else if (ans == 0){ // yes
			JOptionPane.showMessageDialog(null, "BALANCE INQUIRY \n AMOUNT BALANCE: "+ balance+ "\nPrinting Bank slip.....");
		
		
		
		
		}
	
	
	
	}
   
   
   
   
   
   
 
    else if (a.equalsIgnoreCase(D))
    {
	
	int dep =Integer.parseInt(JOptionPane.showInputDialog (null,"Enter Your Deposit Amount" )); 
	
	balance = dep - wht;
	
	ans=JOptionPane.showConfirmDialog(null, "Would you like to generate bank slip?","Generate Bank Slip",JOptionPane.YES_NO_OPTION);
	
	if (ans == 1){ //no
JOptionPane.showConfirmDialog(null,"Would You Like Another Transaction?","Another Transaction",JOptionPane.YES_NO_OPTION);
	if (trans == 1)
	{
	JOptionPane.showMessageDialog(null, "Thanks For Using My Program!!");
	}
	else if (trans == 0)
	{ 
		 String c =(JOptionPane.showInputDialog(null, "Automatic Teller Machine\n [B] Balance Inquiry\n [D]Deposit\n [W] Withdrawal\n [Q] Quit\n Select you Options:  ","Automatic Teller Machine",JOptionPane.PLAIN_MESSAGE));
	
	
	
	
	}
	
	}

	else if (ans == 0){ // yes
		JOptionPane.showMessageDialog(null, "DEPOSIT INQUIRY \n AMOUNT DEPOSIT: "+ dep+ "\nPrinting Bank slip.....");
	
	
	
	
	}
	
    }
	
    else if (a.equalsIgnoreCase(W))
	
    	 wht= Integer.parseInt(JOptionPane.showInputDialog(null," Enter the Withdrawal Amount:"));
	 
   
   if (balance <  wht){
	   JOptionPane.showMessageDialog(null, "Insufficient Amount");
   }
   else if (balance >= wht){
	   
   }
   
   
	}

}

thank u, please dont get tired answering my questions!

Here is the documents by oracle for the while and do while statements:http://docs.oracle.com/javase/tutorial/java/nutsandbolts/while.html.

"The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once."

but take a look at the java docs, it will give you a better idea of what the explanation means

here's the answer I gave in your previous thread :)
it's just a simple comparison between while and do-while.

... to answer your question about the do-while
the main difference is when you run your check
in the while loop, you check if the expression is valid before you run your first iteration.
if the initial value of the expression is false, this means the block of code in your while loop will never run.
in the do while, you check your expression after the first time you run your code block, so even if the expressions original value is false, you will always run your code block at least once.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.