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

Slides:



Advertisements
Similar presentations
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Advertisements

True or false A variable of type char can hold the value 301. ( F )
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
Computer Science 1620 Loops.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
Control Flow C and Data Structures Baojian Hua
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
1 9/28/07CS150 Introduction to Computer Science 1 Loops section 5.2, 5.4, 5.7.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Computer Science 1620 Programming & Problem Solving.
The If/Else Statement, Boolean Flags, and Menus Page 180
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Chapter 4 Program Control Statements
CPS120 Introduction to Computer Science Iteration (Looping)
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CPS120: Introduction to Computer Science Decision Making in Programs.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CMP-MX21: Lecture 5 Repetitions Steve Hordley. Overview 1. Repetition using the do-while construct 2. Repetition using the while construct 3. Repetition.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Decision Making and Branching
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
01/05/100 1 Loops/Iteration Used to repeat an action Must have a STOP condition Three flavors - for, while, do/while.
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
Branching statements.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Programming Fundamentals
Outline Altering flow of control Boolean expressions
Repetition Control Structure
Control Structures Part 1
Let’s all Repeat Together
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Branching statements Kingdom of Saudi Arabia
CSC215 Lecture Control Flow.
Presentation transcript:

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

 #include 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.

 HALT  JUMPS  CONDITIONAL  LOOPS  EXCEPTIONS TYPES OF CONTROL FLOW STATEMENTS

 Most basic control flow statement is the halt, which tells the program to quit running immediately.  #include   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.

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

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

 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 void main() { int I; i=3; if (i=3) cout<<“I is equal to 3”; else cout<<“I is not equal to 3”; } ASSIGNMENT