Easy
Programming in C++ / Nell Dale,Chip Weems.ISBN : 0-7637-3234-6(pbk.)
page : 231
Question : write a code segment using a while loop that outputs the numbers from -5 to 5.
แปล : เขียนโค้ดโดยใช้ while loop แล้วให้ แสดงผลเป็นตัวเลขจาก -5 ถึง 5.
code
int a= -6;
int b= 5;
while(a<b){ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ a<b
a=a+1; ให้ a มีค่าเพิ่ม อีกหนึ่ง เพื่อใช้ในการเช็คเงื่อนไขในการวนลูป และเพื่อไม่ให้เป็น infinite loop
println(+a); //แสดงค่า a
}
result
-5
-4
-3
-2
-1
0
1
2
3
4
5
Medium
Programming in C++ / Nell Dale,Chip Weems.
ISBN : 0-7637-3234-6(pbk.)
page : 231
Question : write a code segment using a while loop that sums the integer, counting up 1,and stops when the sum is greater 15 printing out the integer.
แปล : เขียนโค้ดโดยใช้while loop แล้ว ผลรวมของตัวเลขนับเพิ่มขึ้นที่ละ 1 และหยุดเมื่อผลรวมสูงกว่า 15 ให้แสดงผลออกมาจำนวนเต็ม
code
int count =1;
int sum =0;
while(sum<= 14){ ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ sum น้อยกว่าเท่ากับ 14
count =1; ให้ count เท่ากับ 1
while(count<=(12-sum)/2){ ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ เงื่อนไขที่กำหนดเป็นจริง เป็น ลูปซ้อนลูป
count=count+1 ; ถ้าเงื่อนไขเป็นจริง ก็จะ ให้ count เพิ่มขึ้นอีก 1
}
count=1;
while(count<=sum){
count=count+1 ;
}
sum=sum+1; ให้ sum เพิ่มขึ้น อีก 1
println(sum); แสดงค่า sum
}
result
ISBN : 0-7637-3234-6(pbk.)
page : 231
Question : write a code segment using a while loop that sums the integer, counting up 1,and stops when the sum is greater 15 printing out the integer.
แปล : เขียนโค้ดโดยใช้while loop แล้ว ผลรวมของตัวเลขนับเพิ่มขึ้นที่ละ 1 และหยุดเมื่อผลรวมสูงกว่า 15 ให้แสดงผลออกมาจำนวนเต็ม
code
int count =1;
int sum =0;
while(sum<= 14){ ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ sum น้อยกว่าเท่ากับ 14
count =1; ให้ count เท่ากับ 1
while(count<=(12-sum)/2){ ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ เงื่อนไขที่กำหนดเป็นจริง เป็น ลูปซ้อนลูป
count=count+1 ; ถ้าเงื่อนไขเป็นจริง ก็จะ ให้ count เพิ่มขึ้นอีก 1
}
count=1;
while(count<=sum){
count=count+1 ;
}
sum=sum+1; ให้ sum เพิ่มขึ้น อีก 1
println(sum); แสดงค่า sum
}
result
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Hard
Programming in C++ / Nell Dale,Chip Weems.
ISBN : 0-7637-3234-6(pbk.)
page : 232
Question : write a code segment using a while loop that outputs the numbers for example.
แปล : เขียนโค้ดโดยใช้ while loop แล้วให้ แสดงผลจากตัวอย่าง
1
2 4
3
4
5
6
code
int a=0;
int c=0;
int s=50;
int n=90;
void setup() {
background(2);
size(200, 200);
while (c<=5) { ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ c น้อยกว่าเท่ากับ 5
a =1; ให้ a เท่ากับ 1
if (a<=c) { ใช้เงื่อนไขให้ a น้อยกว่าเท่่ากับ c ก็จะทำใน statement
a=a+1 ; ให้ a เพิ่มขึ้นอีก 1
}
if ((a/2)-c==0) {
a=a+2;
textSize(20);
text(+a, n, s); ให้แสดง ผลของค่า a บนหน้าจอ
}
c=c+1; ให้ c เพิ่มขึ้นอีก 1 พื่อใช้ในการวนลูป และเพื่อไม่ให้เป็น infinite loop
textSize(20);
text(+c, 50, s);
s=s+20;
}
}
result
ISBN : 0-7637-3234-6(pbk.)
page : 232
Question : write a code segment using a while loop that outputs the numbers for example.
แปล : เขียนโค้ดโดยใช้ while loop แล้วให้ แสดงผลจากตัวอย่าง
1
2 4
3
4
5
6
code
int a=0;
int c=0;
int s=50;
int n=90;
void setup() {
background(2);
size(200, 200);
while (c<=5) { ใช้ whilp loop ในการวนลูป คือ จะทำใน statement เมื่อ c น้อยกว่าเท่ากับ 5
a =1; ให้ a เท่ากับ 1
if (a<=c) { ใช้เงื่อนไขให้ a น้อยกว่าเท่่ากับ c ก็จะทำใน statement
a=a+1 ; ให้ a เพิ่มขึ้นอีก 1
}
if ((a/2)-c==0) {
a=a+2;
textSize(20);
text(+a, n, s); ให้แสดง ผลของค่า a บนหน้าจอ
}
c=c+1; ให้ c เพิ่มขึ้นอีก 1 พื่อใช้ในการวนลูป และเพื่อไม่ให้เป็น infinite loop
textSize(20);
text(+c, 50, s);
s=s+20;
}
}
result
ไม่มีความคิดเห็น:
แสดงความคิดเห็น