Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Do While Programmas Example:1

This document provides examples of do-while and while loops in Java. It demonstrates using do-while loops to iterate through arrays and calculate sums, and while loops to iterate from a starting number down to 1 or up to a maximum value. The key differences between static and dynamic binding are also summarized - static binding occurs at compile-time for private, static, final and overloaded methods, while dynamic binding of overridden methods happens at runtime.

Uploaded by

sardar5khan-8
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Do While Programmas Example:1

This document provides examples of do-while and while loops in Java. It demonstrates using do-while loops to iterate through arrays and calculate sums, and while loops to iterate from a starting number down to 1 or up to a maximum value. The key differences between static and dynamic binding are also summarized - static binding occurs at compile-time for private, static, final and overloaded methods, while dynamic binding of overridden methods happens at runtime.

Uploaded by

sardar5khan-8
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 8

----------------------------

DO WHILE PROGRAMMAS
Example :1
class DoWhileLoopExample2 {

public static void main(String args[]){

int arr[]={2,11,45,9};

//i starts with 0 as array index starts with 0

int i=0;

do{

System.out.println(arr[i]);

i++;

}while(i<4);

Output:
2

11

45

Example :2
{

public static void main(String args[]){

int arr[]={2,11,45,9};

//i starts with 0 as array index starts with 0

int i=0;

do{

System.out.println(arr[i]);

i++;

}while(i<4);

Output:

11

45

Example :3
import java.util.Scanner;
class Sum {

public static void main(String[] args) {

Double number, sum = 0.0;

Scanner input = new Scanner(System.in);

do {

System.out.print("Enter a number: ");

number = input.nextDouble();

sum += number;

} while (number != 0.0);

System.out.println("Sum = " + sum);

Enter a number: 2.5

Enter a number: 23.3

Enter a number: -4.2

Enter a number: 3.4

Enter a number: 0

Sum = 25.

Example :4
public class Test {

public static void main(String args[]) {

int x = 10;

do {

System.out.print("value of x : " + x );

x++;

System.out.print("\n");

}while( x < 20 );

value of x : 10

value of x : 11

value of x : 12

value of x : 13

value of x : 14

value of x : 15

value of x : 16

value of x : 17

value of x : 18

value of x : 19

WHLIE LOOP
Example :1
public static void main(String[] args) {

int myNumber = 1;

while(myNumber != 1000) {

if((myNumber % 2) == 0) {

System.out.println(myNumber + " is even");

number++;

Example : 2
class WhileLoopExample {

public static void main(String args[]){

int i=10;

while(i>1){

System.out.println(i);

i--;

10

8
7

Example : 3
public class Test {

public static void main(String args[]) {

int x = 10;

while( x < 20 ) {

System.out.print("value of x : " + x );

x++;

System.out.print("\n");

value of x : 10

value of x : 11

value of x : 12

value of x : 13

value of x : 14
value of x : 15

value of x : 16

value of x : 17

value of x : 18

value of x : 19

Example : 4
class whileLoop

public static void main(String args[])

int x = 1;

// Exit when x becomes greater than 4

while (x <= 4)

System.out.println("Value of x:" + x);

// Increment the value of x for

// next iteration

x++;

Output:
Value of x:1

Value of x:2

Value of x:3

Value of x:4

Static Binding and Dynamic Binding


Static binding happens at compile-time while dynamic binding happens at
runtime. Binding of private, static and final methods always happen at
compile time since these methods cannot be overridden. ... The binding of
overloaded methods is static and the binding of overridden methods is
dynamic.

the end

You might also like