control flow in c++ satish mishra pgt cs kv trimulgherry
TRANSCRIPT
In computer science, control flow refers to the order in which the individual statements, instructions of a program are executed .
INTRODUCTION
#include <iostream.h> int main ()
{ cout << "Hello World!";cout <<“Good Morning”; return 0;
}
TYPICAL C++ PROGRAM
When a program is run, the CPU begins execution at the top of main(), executes some number of statements, and then terminates at the end of main(). Most of the programs you have seen so far have been straight-line programs. Straight-line programs have sequential flow.
However, often this is not what we desire.
Fortunately, C++ provides control flow statements
Allows the programmer to change the CPU’s path through the program.
Most basic control flow statement is the halt, which tells the program to quit running immediately.
#include <cstdlib.h> #include <iostream.h> int main() { cout << 1; exit(0); // terminate and return 0 to operating system // The following statements never execute cout << 2; return 0; }
HALT
A jump unconditionally causes the CPU to jump to another statement. The goto, break, and continue keywords all cause different types of jumps — we will discuss the difference between these afterwards .
JUMPS
a statement that causes the program to change the path of execution based on the value of an expression. The most basic conditional branch is an if statement.
int main() { // do A if (bCondition) // do B else // do C // do D }
Conditional branches/Selection
If bCondition is true, the program will execute A, B, and D. If bCondition is false, the program will execute A, C, and D. As you can see, this program is no longer a straight-line program — it’s path of execution depends on the value of bCondition.
A loop causes the program to repeatedly execute a series of statements until a given condition is false.
int main() { // do A // loop on B // do C }
Loops
This program might execute as ABC, ABBC, ABBBC, ABBBBC, or even AC. Again, you can see that this program is no longer a straight-line program — it’s path of execution depends on how many times (if any) the looped portion executes.
Exceptions provide a way to react to exceptional circumstances (like runtime errors) in our program by transferring control to special functions called handlers.
Exception handling is a fairly advanced feature of C++, and is the only type of control flow statement that we won’t be discussing.
Exceptions
The most basic kind of conditional branch in C++ is the if statement.
Syntax if (expression) statement ; or
if (expression) statement ; else statement2;
If -else
If the expression evalutes to true (non-zero), the statement executes. If the expression evaluates to false, the else statement is executed if it exists.
#include <iostream> int main() { using namespace std; cout << "Enter a number: "; int nX; cin >> nX; if (nX > 10) cout << nX << "is greater than 10" << endl; else cout << nX << "is not greater than 10" << endl; return 0; } Note that the if statement only executes a single statement if the expression is true, and the else only
executes a single statement if the expression is false. In order to execute multiple statements, we can use a block:
#include <iostream> int main() { using namespace std; cout << "Enter a number: "; int nX; cin >> nX; if (nX > 10) { // both statements will be executed if nX > 10 cout << "You entered " << nX << endl; cout << nX << "is greater than 10" << endl; } else { // both statements will be executed if nX <= 10 cout << "You entered " << nX << endl; cout << nX << "is not greater than 10" << endl; } return 0; }
It is also possible to nest if statements within other if statements: #include <iostream> int main() { using namespace std; cout << "Enter a number: "; int nX; cin >> nX; if (nX > 10) // it is bad coding style to nest if statements this way if (nX < 20) cout << nX << "is between 10 and 20" << endl; // who does this else belong to? else cout << nX << "is greater than 20" << endl; return 0; }
Is the else statement in the previous program matched up with the outer or inner if statement?
The answer is that an else statement is paired up with the last unmatched if statement in the same block. Thus, in the program above, the else is matched up with the inner if statement.
dangling else problem
To avoid such ambiguities when nesting complex statements, it is generally a good idea to enclose the statement within a block. Here is the above program written without ambiguity:
#include <iostream> int main() { using namespace std; cout << "Enter a number: "; int nX; cin >> nX; if (nX > 10) { if (nX < 20) cout << nX << "is between 10 and 20" << endl; else // attached to inner if statement cout << nX << "is greater than 20" << endl; } return 0; } Now it is much clearer that the else statement belongs to the inner if statement.
The goto statement is a control flow statement that causes the CPU to jump to another spot in the code. This spot is identified through use of a statement label.
Goto statements
The following is an example of a goto statement and statement label: #include <iostream> #include <cmath> int main() { using namespace std; tryAgain: // this is a statement label cout << "Enter a non-negative number"; double dX; cin >> dX; if (dX < 0.0) goto tryAgain; // this is the goto statement cout << "The sqrt of " << dX << " is " << sqrt(dX) << endl; } In this program, the user is asked to enter a non-negative number. However, if a negative number is entered, the
program utilizes a goto statement to jump back to the tryAgain label. The user is then asked again to enter a new number. In this way, we can continually ask the user for input until he or she enters something valid.
use of goto is shunned in C++ (and most other high level languages as well).
Almost any program written using a goto statement can be more clearly written using loops.
Rule: Avoid use of goto unless necessary
The while statement is the simplest of the three loops that C++ provides.
while (expression) statement;
A while statement is declared using the while keyword. When a while statement is executed, the expression is evaluated. If the expression evaluates to true (non-zero), the statement executes.
However, unlike an if statement, once the statement has finished executing, control returns to the top of the while statement and the process is repeated.
While statements
int i = 0; while (i < 10) { cout << i<< " "; i++; } cout << "done!"; This outputs:
0 1 2 3 4 5 6 7 8 9 done!
It is possible that a while statement executes 0 times. Consider the following program:
int iii = 15; while (iii < 10) { cout << iii << " "; i++; } cout << "done!"; The condition 15 < 10 evaluates to false, so the while
statement is skipped. The only thing this program prints is done!.
if the expression always evaluates to true, the while loop will execute forever. This is called aninfinite loop. Here is an example of an infinite loop:
int iii = 0;while (iii < 10) cout << iii << " ";Because iii is never incremented in this program, iii < 10 will always be true. Consequently, the loop will never terminate, and the program will hang.
Infinite loop