• We just launched and are currently in beta. Join us as we build and grow the community.

Advertise Here

Advertise Here

Advertise Here

Using do...while Loop Structure

Weetabox

LOL Master
W Rep
0
0
0
Rep
0
W Vouches
0
0
0
Vouches
0
Posts
91
Likes
147
Bits
2 MONTHS
2 2 MONTHS OF SERVICE
LEVEL 1 100 XP
The following Java program is a do…while Loop structure. In Java, do is a reserved word. As with the other repetition structures, a do…while statement can be either a simple or compound statement. If it is compound statement, enclose it between curly brackets. The expression is called loop condition. I will be using the JCreator IDE in developing the program.

To start in this tutorial, first open the JCreator IDE, click new and paste the following code:

  1. import

    java.util.*

    ;

  2. public

    class

    doWhile
  3. {
  4. static

    Scanner console =

    new

    Scanner(

    System

    .in

    )

    ;

  5. public

    static

    void

    main (

    String

    [

    ]

    args)
  6. {
  7. int

    num, temp, sum;

  8. System

    .out

    .print

    (

    "Enter a positive integer: "

    )

    ;
  9. num =

    console.nextInt

    (

    )

    ;
  10. System

    .out

    .println

    (

    )

    ;

  11. temp =

    num;

  12. sum =

    0

    ;

  13. do
  14. {
  15. sum =

    sum +

    num %

    10

    ;

  16. num =

    num /

    10

    ;

  17. }

    while

    (

    num >

    0

    )

    ;

  18. System

    .out

    .println

    (

    "The sum of the digits = "

    +

    sum)

    ;

  19. if

    (

    sum %

    3

    ==

    0

    )
  20. System

    .out

    .println

    (

    temp +

    " is divisble by 3"

    )

    ;
  21. else
  22. System

    .out

    .println

    (

    temp +

    " is not divisble by 3"

    )

    ;
  23. if

    (

    sum %

    3

    ==

    0

    )

  24. System

    .out

    .println

    (

    temp +

    " is divisble by 9"

    )

    ;
  25. else
  26. System

    .out

    .println

    (

    temp +

    " is not divisble by 9"

    )

    ;

  27. }
  28. }

Sample run:

Enter a positive integer: 320

The sum of the digits = 5
320 is not divisble by 3
320 is not divisble by 9

This program works as follows:

The statement System

.out

.print

(

"Enter a positive integer: "

)

;

prompts the user to enter a positive integer.

The statement num =

console.nextInt

(

)

;

store the next number in the variable num.

The statement temp =

num;

temp is equals to variable num.

The statement sum =

0

;

sets the value of variable sum to 0.

The statement sum =

sum +

num %

10

;

extracts the last digit and add it to sum.

The statement num =

num /

10

;

remove the last digit.

The statement
  1. do
  2. {
  3. sum =

    sum +

    num %

    10

    ;

  4. num =

    num /

    10

    ;

  5. }

    while

    (

    num >

    0

    )

    ;<

    java>

    is the beginning and the end of the do

    ..while

    loop.

The statements
  1. if

    (

    sum %

    3

    ==

    0

    )
  2. System

    .out

    .println

    (

    temp +

    " is divisble by 3"

    )

    ;
  3. else
  4. System

    .out

    .println

    (

    temp +

    " is not divisble by 3"

    )

    ;
  5. if

    (

    sum %

    3

    ==

    0

    )

  6. System

    .out

    .println

    (

    temp +

    " is divisble by 9"

    )

    ;
  7. else
  8. System

    .out

    .println

    (

    temp +

    " is not divisble by 9"

    )

    ;

Is the if…else condition where outputs the program if it is divisible by 3 or not, and divisible by 9 or not.

 

452,496

343,109

343,117

Top