FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Fundamental of C programming
The "if structure" is used to execute statement(s) only if the given condition is satisfied.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
True or false A variable of type char can hold the value 301. ( F )
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
CP104 Introduction to Programming Selection Structures_3 Lecture 11 __ 1 The Switch Statement The switch statement provides another means to select one.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Computer Science Department Relational Operators And Decisions.
Selection Structures (if & switch statements) (CS1123)
Chapter 4 Selection Structures: Making Decisions.
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
CONTROLLING PROGRAM FLOW
Lecture 3 Decisions (Conditionals). One of the essential features of computer programs is their ability to make decisions. Like a train that changes tracks.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
OBJECTIVES  Illustration of the Concept of Control Flow  Types of Control Flow  Knowledge about conditional and repetitive statements.  Make more.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Chapter 5: Structured Programming
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Basic Control Structures
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
Lesson 6 Selection Structures. Example program //This will convert a numeric grade into a letter grade import TerminalIO.KeyboardReader; public class.
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
1 SELECTION using IF and IF-ELSE Chapter 4. 2 Agenda Background  One Way selection (if) Two Way selection (if-else) Compound Statements Nested if-else.
Copyright Curt Hill The C/C++ switch Statement A multi-path decision statement.
CHAPTER 10 ARRAYS AND FUNCTIONS Prepared by: Lec. Ghader Kurdi.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Statements
What is a function? Functions are nothing but sub-programs of a program. Is a part of a program which performs a particular task as desired by the programmer.
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Session 2 Operators, Decisions and Loops. Objectives Operators Casting data Decision marking structures Loops break, continue, return.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Multi-Selection If-elsif Statement.  In fact, it’s everything in between  Yesterday we learned how to add a control statement that gave us two path.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
Asif Nawaz University Institute of Information Technology, PMAS-AAUR Lecture 05: Object Oriented Programming:2014 Object-Oriented Programming in C++ Exception.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
THE DECISIONS CONTROL STRUCTURE! CHAPTER 2. Transfer of control statement: o The statement of computer program are executed one after the other in the.
Chapter 3 Selection Statements
Decisions Chapter 4.
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Control Statement Examples
Summary Two basic concepts: variables and assignments Basic types:
Program Flow.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
HNDIT11034 More Operators.
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called sequential execution of program. S KIRAN PGT(COMP) KV CLRI STATEMENT 1 STATEMENT 2 STATEMENT 3 3/21/2016

FLOW OF CONTROL However, often this is not what we desire. For example, if we ask the user to make a selection, and the user enters an invalid choice, ideally we’d like to ask the user to make another choice. This is not possible in a sequential execution of program. Fortunately, C++ provides control flow statements (also called flow control statements), which allow the programmer to change the Compiler’s path through the program. There are quite a few different types of control flow statements. S KIRAN PGT(COMP) KV CLRI3/21/2016

FLOW OF CONROL STATEMENT FLOW CONTROL : SELECTION CONSTRUCT Here execution of statements depends upon a condition-test. If a condition test evaluates to true a set of statements is executed else another set of statements is executed. S KIRAN PGT(COMP) KV CLRI3/21/2016

SELECTION CONSTRUCT S KIRAN PGT(COMP) KV CLRI COND ITION STATEMENT 1STATEMENT 2 STATEMENT 1 STATEMENT 2 3/21/2016

SELECTION CONSTRUCT Different C++ Selection Statements are if statement if… else...construct if … else if … else construct switch statement S KIRAN PGT(COMP) KV CLRI3/21/2016

if Statement SYNTAX : if ( condition ) { statement true; } S KIRAN PGT(COMP) KV CLRI3/21/2016

if Statement #include void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } getch(); } S KIRAN PGT(COMP) KV CLRI3/21/2016

if … else… construct if ( condition ) { statement true; } else { statement false; } S KIRAN PGT(COMP) KV CLRI3/21/2016

If … else… construct #include void main() { int x = -1; if ( x > 0 ) { cout << "x is a positive number"; } else { cout << "x is a negative or zero"; } getch(); } S KIRAN PGT(COMP) KV CLRI3/21/2016

if…else if … else if ( condition-1 ) { statement; // condition-1 is true } else if ( condition-2 ) { statement; // condition-2 is true } else if ( condition-3 ) { statement; // condition-3 is true } else { // default case: statement; // all above conditions were false } S KIRAN PGT(COMP) KV CLRI3/21/2016

if…else if … else #include int main() { int x = -1; if ( x > 0 ) { cout << "x is positive"; } else if ( x < 0 ) { cout << "x is negative"; } else { cout << "x is zero"; } return 0; } S KIRAN PGT(COMP) KV CLRI3/21/2016

if…else if … else Example 2 : Program to create the equivalent of four-function calculator. The program requires the user to enter two numbers and an operator. It then carries the specified arithmetical operation: addition, subtraction, multiplication or division of two numbers. Finally display the results S KIRAN PGT(COMP) KV CLRI3/21/2016

Program 2 #include if(ch=='+') #include result = a+b; void main()else if(ch=='-') {result = a-b; char ch;else if(ch=='*') float a, b, result;result = a*b; clrscr();else if(ch=='/') cout<<"Enter two numbers";result = a/b; cin>>a>>b;else cout<<" \n Enter the operator(+,-,/,*); cout<<"Invalid Operator“ cin>>ch; cout<<"\n The Calculated result is = "<<result; } S KIRAN PGT(COMP) KV CLRI3/21/2016

Program Write a program to accept student information like, name, rollno, marks in 3 subjects and calculate the total, average and grade. Grade is calculated as S KIRAN PGT(COMP) KV CLRI AVERAGEGRADE > 90A > 75B > 60C > 40D < 40E 3/21/2016

RECAPITULATION What is sequential Execution of a program? When do we make use of selection statements? What are the different types of selection statements available? S KIRAN PGT(COMP) KV CLRI3/21/2016