Control Statements
Until this point in the book, statements have been executed in sequence. The flow of control in a C++ program is linear; it may pass to a function and back, but it always passes through each statement. Often you need to make decisions and you need to repeat actions. These are the basic reasons for control statements.
The if-else Statement
Programs often need to make decisions. For example, if a student scores less than a certain required minimum, action has to be taken; if an account is overdrawn more than a prescribed number of days, bills need to be sent out and interest rates raised. The C++ if-else statement handles these types of decisions:
;> int mark = 49; ;> if (mark >= 50) cout << "pass\n"; ;> else cout << "fail\n"; ...
Get C++ By Example: UnderC Learning Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.