TAILIEUCHUNG - A Complete Guide to Programming in C++ part 13

A Complete Guide to Programming in C++ part 13. This book provides both novice and experienced programmers with a comprehensive resource manual for the C++ programming language. Readers gain experience in all aspects of programming, from elementary language concepts to professional software development, with in depth coverage of all the language elements en route. These elements are carefully ordered to help the reader create useful programs every step of the way. | THE FOR STATEMENT Initializing and Reinitializing A typical loop uses a counter that is initialized tested by the controlling expression and reinitialized at the end of the loop. Example int count 1 Initialization while count 10 Controlling cout count expression . loop endl count Reinitialization In the case of a for statement the elements that control the loop can be found in the loop header. The above example can also be expressed as a for loop Example int count for count 1 count 10 count cout count . loop endl Any expression can be used to initialize and reinitialize the loop. Thus a for loop has the following form Syntax for expression1 expression2 expressions statement expressionl is executed first and only once to initialize the loop. expression2 is the controlling expression which is always evaluated prior to executing the loop body if expression2 is false the loop is terminated if expression2 is true the loop body is executed. Subsequently the loop is reinitialized by executing expressions and expression2 is re-tested. You can also define the loop counter in expressionl. Doing so means that the counter can be used within the loop but not after leaving the loop. Example for int i 0 i 10 cout i As this example illustrates the loop body can be an empty statement. This is always the case if the loop header contains all necessary statements. However to improve readability even the empty statement should occupy a line of its own. 100 CHAPTER 6 CONTROL FLOW THE for STATEMENT CONTINUED Sample program Outputs a table of exchange Euro and US- include iostream include iomanip using namespace std int main long euro maxEuro Amount in Euros double rate Exchange rate Euro - cout n TABLE OF EXCHANGE Euro - US- n n cout nPlease give the rate of exchange one Euro in US- cin rate cout nPlease enter the maximum euro cin maxEuro --- Outputs the table --- Titles of columns cout n setw 12 Euro setw 20 US- t tRate rate endl Formatting US- cout fixed setprecision 2 .

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.