php利用循环编写出5_100的奇数之和,跳过33和99(用三种循环结构编写)
1个回答
关注
展开全部
咨询记录 · 回答于2024-01-13
php利用循环编写出5_100的奇数之和,跳过33和99(用三种循环结构编写)
// while循环
$num = 5;
$sum = 0;
while ($num <= 100) {
if ($num != 33 && $num != 99) {
$sum += $num;
}
$num += 2;
echo $sum;
echo "";
// for循环
$sum = 0;
for ($i = 5; $i <= 100; $i += 2) {
if ($i != 33 && $i != 99) {
$sum += $i;
}
echo $sum;
echo "";
// foreach循环
$arr = range(5, 100, 2);
$sum = 0;
foreach ($arr as $v) {
if ($v != 33 && $v != 99) {
$sum += $v;
}
echo $sum;