1、以下代码的结果是什么?
begin
当前数:=1;
While 当前数<8 then
当前数:=当前数+2;
结果:=2*当前数+3;
End loop;
rentun 结果;
end;2、以下代码执行循环的次数是多少?
LoopCounts
begin
结果:=0;
当前数:=1;
For 当前数 in (1...10) loop
结果:=结果+当前数;
If 结果>17 then
退出循环;
End if;
end loop;
end;
begin
当前数:=1;
While 当前数<8 then
当前数:=当前数+2;
结果:=2*当前数+3;
End loop;
rentun 结果;
end;2、以下代码执行循环的次数是多少?
LoopCounts
begin
结果:=0;
当前数:=1;
For 当前数 in (1...10) loop
结果:=结果+当前数;
If 结果>17 then
退出循环;
End if;
end loop;
end;
public class Test
{
public int get()
{
int i = 1;
int result = 0;
while (i < 8)
{
i += 2;
result = 2 * i + 3;
}
return result;
}
public static void main(String[] args)
{
Test test = new Test();
int result = test.get();
System.out.println(result);
}
}第一个:输出结果是:21
public int get(){
int result = 0;
for (int i = 0; i <= 10; i++){
result += i;
if (result > 17){
break;
}
}
return result;
}
public static void main(String[] args){
Test test = new Test();
int result = test.get();
System.out.print(result);
}
}
第二个:结果是21 循环次数:6
public class Test
{
public int get()
{
int i = 1;
int result = 0;
do
{
i += 2;
result = 2 * i + 3;
}
while (i < 8);
return result;
}
public static void main(String[] args)
{
Test test = new Test();
int result = test.get();
System.out.println(result);
}
}
那你看下这个程序呢?
区别是
直接while循环:第一次循环也需要判断是否为真
do while:第一次循环无视条件,从第2次开始需要判断条件是否为真当i=7的时候,
i+=2;
这时,i已经等于9了,然后i*2+3,就等于9*2+3=21