1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.

Slides:



Advertisements
Similar presentations
INTEC CS160 - Jeanine Ingber Engineering Problem Solving with C Fundamental Concepts Chapter 3 Control Structures and Data Files.
Advertisements

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.
1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
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.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Chapter 4 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4 Selection Structures: Making Decisions.
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
Previously Repetition Structures While, Do-While, For.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
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.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
Chapter 05 (Part III) Control Statements: Part II.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
1 ELEC 206 Chapter 3 Control Structures 5-Step Problem Solving Methodology 1. State the problem clearly. 2. Describe the input and output. 3. Work a.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Control Structures and Data Files
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Engineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C Fundamental Concepts
Chapter 2.2 Control Structures (Iteration)
Control Structures: Selection Statement
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
Summary Two basic concepts: variables and assignments Basic types:
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
REPETITION Why Repetition?
Presentation transcript:

1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures

2 Structured Programming Sequence Selection Repetition yesno yes no

3 Boolean Expressions

4 Relational Operators ==equality !=non equality <less than >greater than <=less than equal to >=greater than equal to

5 Logical Operators !not &&and ||or

6 Operator Precedence 1. >=(relational operators) 2.== !=(logical operators) 3.&&(…) 4.||(…)

7  (-6 =10)  (3.0 >= 2.0) || (3.0 >= 4.0)  (3.0 >= 2.0) && (3.0 >= 4.0) true && true results in true Practice! - evaluate true || false results in true true && false results in false

8 Selection Statements

9 if if else switch

10 The if statement if(expression) statement; //single statement executed //if expression is true if(expression) { //statements inside {} are //executed if expression is true statement1; statement2; … statement n; }

11 The if statement - examples if (x>0) k++; if(x>0) { x=sqrt(x); k++; }

12 The if - else statement if(expression) statement; else statement; if(expression) { statement block } else { statement block }

13 The nested if-else if(x > y) if(y < z) k++; else m++; else j++;

14 Practice! int x=9, y=7, z=2, k=0, m=0, j=0; if(x > y) if(y >z && y>k) k++; else m++; else j++; What are the values of j, k and m? j is 0, k is 1, m is 0

15 The switch statement switch(expression) { case constant: statement(s); break; case constant: statement(s); break; /* default is optional*/ default: statement(s); }

16 The switch statement Expression must be of type integer or character The keyword case must be followed by a constant break statement is required unless you want all subsequent statements to be executed.

17 switch statement example char ch; int ecount=0, vowels=0, other=0; cin.get(ch); while(!cin.eof()) { switch(ch) {case ‘e’: ecount++; case ‘a’: case ‘i’: case ‘o’: case ‘u’: vowels++; break; default: other++; }//end switch cin.get(ch); }//end while cout << ecount << “ e’s, ” << vowels << “ vowels, and ” << other << “ other characters. “ << endl;

18 Practice! Convert these nested if/else statements to a switch statement : if (rank==1 || rank==2) cout << "Lower division \n"; else { if (rank==3 || rank==4) cout << "Upper division \n"; else { if (rank==5) cout << "Graduate student \n"; else cout << "Invalid rank \n"; }

19 Practice Solution! switch(rank) { case 1: case 2: cout << "Lower division \n"; break; case 3: case 4: cout << "Upper division \n"; break; case 5: cout << "Graduate student \n"; break; default: cout << "Invalid rank \n"; }//end switch

20 Repetition Statements

21 The while statement while (condition) statement; while (condition) { statement block } yes no

22 The do/while statement do statement; while (condition) do { statement block } while (condition) yes no

23 Practice! #include using namespace std; int main() { int n=4; while(n>0) { cout << n << endl; n--; } cout << “value of n outside while is “ << n << endl; return 0; } Output? Program Trace

24 The for statement initalize test increment/ decrement true statement(s)

25 The for statement for(initialization; test; increment/decrement) statement; for(initialization; test; increment/decrement) { statement; }

26 The for statement - examples //sum integers from 1 to 10 int sum =0; for(int I=1;I<=10;I++) sum = sum + I; //sum odd integers from 1 to 10 int sum =0; for(int I=1;I<=10;I+=2) sum = sum + I;

27 Practice! Determine the number of times that each of the following for loops is executed. for (k=3; k<=10; k++) { statements; } for (k=3; k<=10; ++k) { statements; } for (count=-2; count<=10; count++) { statements; }

28 The break statement break; –terminates loop –execution continues with the first statement following the loop

29 The continue statement continue; –forces next iteration of the loop, skipping any remaining statements in the loop

30 Practice! What is the output? #include using namespace std; int main() { for(int i=0; i<10; i++) { if(i%2) { continue; }//end if cout << i << endl; }//end for return 0; }//end main

31 Practice! What is the output? #include using namespace std; int main() { for(int i=0; i<10; i++) { if(i%2) { break; }//end if cout << i << endl; }//end for return 0; }//end main 0

32 Practice! //This while loop calculates n! int nfact=1, n; cout << “enter positive integer “; cin >> n; while(n > 0) { nfact = nfact*n; n--; } cout << n “! = “ << nfact;.. Write a for loop to replace the while loop