Do-while loop in jave is used to run a specific code statement for first time when you run the program and then next time will ask for condition.
syntex
do{
statement(s);
-----
}while(expression);
Example
Write a program to get summation of values entered by programmer untill user enter's 0.(0 will terminate program)
import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class conditional // name of class
{
public static void main(String[] args)
{
float number,sum=0;// sum is variable to store summation of given numbers
System.out.println("enter the numbers you wan to sum");
System.out.println("enter 0 to terminate");
Scanner sc = new Scanner(System.in);
do {
number= sc.nextFloat();//number stores value from user
sum =number+sum; // sum add numbers and return summation of all given numbers
}while(number!=0); // when user enters 0 , program termiates loop
System.out.println("Summation is "+sum);
}
}
Output
syntex
do{
statement(s);
-----
}while(expression);
Example
Write a program to get summation of values entered by programmer untill user enter's 0.(0 will terminate program)
import java.util.Scanner; // this package util and Class Scanner is declared to take input from user
/*
*@ scanner class
* @author khant vyas
*/
public class conditional // name of class
{
public static void main(String[] args)
{
float number,sum=0;// sum is variable to store summation of given numbers
System.out.println("enter the numbers you wan to sum");
System.out.println("enter 0 to terminate");
Scanner sc = new Scanner(System.in);
do {
number= sc.nextFloat();//number stores value from user
sum =number+sum; // sum add numbers and return summation of all given numbers
}while(number!=0); // when user enters 0 , program termiates loop
System.out.println("Summation is "+sum);
}
}
Output
do while in java |
No comments:
Post a Comment