15 กันยายน 2556

..# Loop #..

..# Loop #..


Easy   P.351 /1
Assume a program declares an int  named evenNum and initializes it to 2. Write a C++ while loop that uses the evenNum variable to display the even integers between 1 and 9
Answer
          int evenNum = 2;  // ประกาศและกำหนดค่าตัวแปร evenNum เริ่มที่ 2
                   while (evenNum < 9) {  // สร้าง Loop ที่ evenNum น้อยกว่า 9
                        println (evenNum);  // ปริ๊นค่า evenNum ในบรรทัดใหม่
                        evenNum = evenNum + 2;  // เพิ่มค่า evenNum 2 หน่วย เพื่อที่จะเป็นจำนวนคู่
                   }  // end while


Middle   P.390 /2
Assume a program declares two int variable named firstLoop and secondLoop. Both variable are initialized to the number 1. Write the C++ code that uses these variables to display the number 1 and 3 on four lines, as follows.
1        2        3
1        2        3
1        2        3
1        2        3
Answer
          int firstLoop = 1;  // ประกาศและกำหนดค่าตัวแปร firstLoop เท่ากับ 1
                   int secondLoop = 1;  // ประกาศและกำหนดค่าตัวแปร secondLoop เท่ากับ 1
                   while (firstLoop <=4) {  // สร้าง Loop ที่ค่า firstLoop น้อยกว่าหรือเท่ากับ 4
                        while (secondLoop <= 3) {  // สร้าง Loop ที่ค่า secondLoop น้อยกว่าหรือเท่ากับ 3
                             print(secondLoop);  // ปริ๊นค่า secondLoop  ในบรรทัดเดียวเดิม
                             print("   ");  // ปริ๊น ช่องว่าง ในบรรทัดเดียวกัน
                             secondLoop = secondLoop + 1; 
                            
// เพิ่มค่า secondLoop 1 หน่วย แล้วกลับไปพิจารณาเงื่อนไขการวน loop
                        }  // end while secondLoop
                        println();  // ปริ๊นเริ่มบรรทัดใหม่
                        firstLoop = firstLoop + 1; 
                       
// ให้ firstLoop เพิ่ม 1 หน่วย กลับไปพิจารณาเงื่อนไขการวนค่า loop
                         secondLoop = 1;  // รีเซ็ตค่า secondLoop เพื่อเริ่มการวน loop ที่สองใหม่
                   }  // end while  firstLoop


Hard   P.394 /6:B
Complete the program by entering the instructions to display the following pattern of asterisks ( 9 asterisks, 8 asterisks, 7 asterisks, 6 asterisks, 5 asterisks, 4 asterisks, 3 asterisks, 2 asterisks, 1 asterisks ).
* * * * * * * * *
* * * * * * * *
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
Answer
          void setup() {
                        int i = 9;
                        while (i > 0) {
                             printV(i);  // เรียกใช้ฟังก์ชัน printV และใส่ค่า parameter ให้เท่ากับค่า i
                             println();  // ปริ๊นบรรทัดใหม่
                             i = i - 1;  // ให้ค่า I ลดลง 1 หน่วย
                        }  // end loop
                   }   // end function
                   void printV(int n) {  // สร้างฟังก์ชัน printV มี parameter n
                        int count = 0;  // ประกาศและกำหนดค่าตัวแปร count = 0
                        while (count < n) {  // สร้าง loop ให้ค่า count น้อยกว่าค่า ตัวแปร n
                             print("* ");  // ปริ๊น  “* ” ในบรรทัดเดียวกัน
                             count = count+1;  // เพิ่มค่า count 1 หน่วย แล้วกลับไปดูเงื่อนไขการวน loop
                        }  // end loop
                   }  // end function printV

ไม่มีความคิดเห็น:

แสดงความคิดเห็น