โจทย์
X
= 50 Y=5
|
||
X<=100
50
<=100
|
(T)
X = 50 + 5 * 2
= 50
+10
Y = 5 + 1
|
X=60
Y=6
|
X<=100
60
<=100
|
(T)
X = 60 + 6 * 2
= 60
+12
Y = 6 + 1
|
X=72
Y=7
|
X<=100
72
<=100
|
(T)
X = 72 + 7 * 2
= 72
+14
Y = 7 + 1
|
X=86
Y=8
|
X<=100
86
<=100
|
(T)
X = 86 + 8 * 2
= 86
+16
Y = 8 + 1
|
X=102
Y=9
|
X<=100
102
<=100
|
(F)
|
X=102
Y=9
|
2.1.)
จงหาผลลัพธ์ Y = 9 2.2.) จงหาผลลัพธ์ X = 102
2.3.)
จงหาว่ามีการวนซ้ำ (looping) กี่ครั้ง 4
|
import java.util.*;
import java.lang.Math;
class JavaApplication {
public static void main(String[] args) {
int x;
int y;
x = 50;
y = 5;
int i;
i = 0;
while (x <= 100) {
x = x + y * 2;
y = y + 1;
i = i + 1;
}
System.out.println(" x = " + x);
System.out.println(" y = " + y);
System.out.println(" i = " + i);
}
}
|
<?php
# Main
$x = 50;
$y = 5;
$i = 0;
while ($x <= 100) {
$x = $x + $y * 2;
$y = $y + 1;
$i = $i + 1;
}
echo " x = " . $x . PHP_EOL;
echo " y = " . $y . PHP_EOL;
echo " i = " . $i . PHP_EOL;
?>
|