Top Banner
CONTROL FLOW IN C+ + Satish Mishra PGT CS KV Trimulgherry
29
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

CONTROL FLOW IN C++Satish Mishra

PGT CSKV Trimulgherry

Page 2: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

INTRODUCTION

Page 3: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

#include <iostream.h> int main ()

{ cout << "Hello World!";cout <<“Good Morning”; return 0;

}

TYPICAL C++ PROGRAM

Page 4: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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.

Page 5: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

Fortunately, C++ provides control flow statements

Allows the programmer to change the CPU’s path through the program. 

Page 6: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

HALT JUMPS CONDITIONAL LOOPS EXCEPTIONS

TYPES OF CONTROL FLOW STATEMENTS

Page 7: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 8: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

 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

Page 9: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 10: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

 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.

Page 11: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 12: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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.

Page 13: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 14: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 15: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 16: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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.

Page 17: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

#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:

Page 18: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

#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; }

Page 19: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 20: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 21: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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.

Page 22: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 23: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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.

Page 24: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

 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

Page 25: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 26: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

int i = 0; while (i < 10)    {   cout << i<< " ";    i++;    } cout << "done!"; This outputs:

0 1 2 3 4 5 6 7 8 9 done!

Page 27: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 28: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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

Page 29: CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.

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”; }

ASSIGNMENT