TAILIEUCHUNG - C++ Lab 5 Iteration (Looping) Dr. John Abraham

Suppose you want to display numbers 1 to 100 on the screen. Based on what we studies so far, you will have to write one hundred cout statements. If you think about it, all you are doing is adding one to the previous number and displaying it over and over again until 100 has been displayed. Let us write the steps to do it. Number gets 1. Display the number Add one to it Repeat these two statements. Stop when 100 has been displayed. Let us rework it with some numbers. Number = 1 Do the following statements (in brackets). | Lab 5 Iteration Looping Dr. John Abraham Suppose you want to display numbers 1 to 100 on the screen. Based on what we studies so far you will have to write one hundred cout statements. If you think about it all you are doing is adding one to the previous number and displaying it over and over again until 100 has been displayed. Let us write the steps to do it. Number gets 1. Display the number Add one to it Repeat these two statements. Stop when 100 has been displayed. Let us rework it with some numbers. Number 1 Do the following statements in brackets while number is less than or equal to 100. display number add one to number Here are the steps 1. Initialize the variable this variable is also called the loop control variable or LCV because this variable controls the loop . In this example the LCV is number. 2. Check for the condition to enter the loop. The condition should yield a True to enter the loop. If the condition yields a false the loop is not entered. 3. Set up the body of the loop. You may have one or multiple things to do within the loop body. The body here appears within the brackets. 4. Change the value of the LCV within the body. In this example the number is changed by adding a one to it. These steps will work in all programming languages. Let us write this program in c . See program 4-1. Program 4-1 Display 1 to 100 on the screen Teaching objective - while loop By Dr. John Abraham Created for 1370 students include iostream using namespace std int main int number number 1 while number 100 cout number number getchar return 0 display number and put some spaces or use setw increment number by one Program Run 4-1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 This while loop is a count controlled loop since we .

TỪ KHÓA LIÊN QUAN
TAILIEUCHUNG - Chia sẻ tài liệu không giới hạn
Địa chỉ : 444 Hoang Hoa Tham, Hanoi, Viet Nam
Website : tailieuchung.com
Email : tailieuchung20@gmail.com
Tailieuchung.com là thư viện tài liệu trực tuyến, nơi chia sẽ trao đổi hàng triệu tài liệu như luận văn đồ án, sách, giáo trình, đề thi.
Chúng tôi không chịu trách nhiệm liên quan đến các vấn đề bản quyền nội dung tài liệu được thành viên tự nguyện đăng tải lên, nếu phát hiện thấy tài liệu xấu hoặc tài liệu có bản quyền xin hãy email cho chúng tôi.
Đã phát hiện trình chặn quảng cáo AdBlock
Trang web này phụ thuộc vào doanh thu từ số lần hiển thị quảng cáo để tồn tại. Vui lòng tắt trình chặn quảng cáo của bạn hoặc tạm dừng tính năng chặn quảng cáo cho trang web này.