1 / 29

CONTROL FLOW IN C++

CONTROL FLOW IN C++. Satish Mishra PGT CS KV Trimulgherry. INTRODUCTION . In computer science,  control flow refers to the order in which the individual  statements,  instructions of a  program are executed  . TYPICAL C++ PROGRAM . #include < iostream.h >

dex
Télécharger la présentation

CONTROL FLOW IN C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry

  2. INTRODUCTION In computer science, control flow refers to the order in which the individual statements, instructions of a  program are executed .

  3. TYPICAL C++ PROGRAM #include <iostream.h> intmain () { cout<< "Hello World!"; cout <<“Good Morning”; return 0; }

  4. 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.

  5. Fortunately, C++ provides control flow statements Allows the programmer to change the CPU’s path through the program. 

  6. TYPES OF CONTROL FLOW STATEMENTS HALT JUMPS CONDITIONAL LOOPS EXCEPTIONS

  7. HALT 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; }

  8. JUMPS  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 .

  9. Conditional branches/Selection 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 }

  10.  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.

  11. Loops 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 }

  12. 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.

  13. C++ provides 3 types of loops: while, do while, and for loops.

  14. Exceptions 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.

  15. If -else The most basic kind of conditional branch in C++ is the if statement. Syntax if (expression) statement ; or if (expression) statement ; else statement2;

  16. 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.

  17. #include <iostream> int main() {     using namespace std; cout << "Enter a number: "; intnX; 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:

  18. #include <iostream> int main() {     using namespace std; cout << "Enter a number: "; intnX; 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; }

  19. It is also possible to nest if statements within other if statements: #include <iostream> int main() {     using namespace std; cout << "Enter a number: "; intnX; 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; }

  20. dangling else problem Is the else statement in the previous program matched up with the outer or innerif 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.

  21. 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: "; intnX; 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.

  22. Goto statements 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.

  23. 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) gototryAgain; // 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.

  24.  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

  25. While statements 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.

  26. inti = 0; while (i < 10)     { cout << i<< " "; i++;     } cout<< "done!"; This outputs: 0 1 2 3 4 5 6 7 8 9 done!

  27. 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!.

  28. Infinite loop 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.

  29. ASSIGNMENT Q.1.What is control flow ? Q.2.What different types of control flow are available in C++ Q.3. Find the errors after correcting errors predict the output #include<iostream.h> void main() { int I; i=3; if (i=3) cout<<“I is equal to 3”; else cout<<“I is not equal to 3”; }

More Related