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.

Slides:



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

Computer Science 1620 Loops.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
 2002 Prentice Hall. All rights reserved. 1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure.
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.
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
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.
Presented by Joaquin Vila Prepared by Sally Scott ACS 168 Problem Solving Using the Computer Week 12 Boolean Expressions, Switches, For-Loops Chapter 7.
High-Level Programming Languages: C++
Chapter 5: Control Structures II (Repetition)
CHAPTER 5: CONTROL STRUCTURES II INSTRUCTOR: MOHAMMAD MOJADDAM.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
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.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Chapter 4 Selection Structures: Making Decisions.
Programming in Java Unit 4. Learning outcome:  LO2: Be able to design Java solutions  LO3: Be able to implement Java solutions Assessment criteria:
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
C++ Programming Lecture 6 Control Structure II (Repetition) By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
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,
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 Outline 4.1 Introduction 4.2 Algorithms 4.3 Pseudocode 4.4 Control Structures 4.5 The if Selection Structure 4.6 The if / else Selection Structure 4.7.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
REPETITION STATEMENTS - Part2 Structuring Input Loops Counter-Controlled Repetition Structure Sentinel-Controlled Repetition Structure eof()-Controlled.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 5: Control Structures II (Repetition)
Chapter 4 Repetition Statements Program Development and Design Using C++, Third Edition.
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.
CHAPTER 2.2 CONTROL STRUCTURES (ITERATION) Dr. Shady Yehia Elmashad.
Introduction to Computer Programming
Chapter 3 Control Statements
Control Structures and Data Files
Engineering Problem Solving with C++, Etter/Ingber
Engineering Problem Solving with C Fundamental Concepts
Ch 7: JavaScript Control Statements I.
Chapter 2.2 Control Structures (Iteration)
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.
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.
Control Structures: Selection Statement
3 Control Statements:.
Repetition Control Structure
Chapter 2.2 Control Structures (Iteration)
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Presentation transcript:

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 hand example. 4. Develop a solution. 5. Test your solution. 2

Control structures  Algorithm Development  Conditional Expressions  Selection Statements  Repetition Statements  Structuring Input Loops 3

Algorithm Development  An algorithm is a sequence of steps for solving a problem.  Engineering problem solutions to real world problems require complex algorithms.  Development of a good algorithm increases the quality and maintainability of a solution, and reduces the overall time required to implement a correct solution. 4

Top-Down Design  Top-down design begins with a "big picture" description of a problem solution in sequential steps.  The sequential steps are refined until the steps are detailed enough to translate to language statements.  The refined steps, or algorithm, can be described using pseudo code or flowcharts. 5

Evaluation of Alternative Solutions  Most problems have more than one solution.  There may not be a single best solution, but some solutions are better than others.  Elements that contribute to a good solution: –correctness –reliability –readability –maintainability –execution speed –memory considerations –user interface 6

Structured Programming  A structured program is written using simple control structures, including: –Sequence – steps are performed one after another. –Selection – one set of statements is executed if a given condition is true, a different set of statements, or no statements at all, is executed if the condition is false. –Repetition –A set of statements is executed repeatedly as long as a given condition is true. 7

8 Structured Programming  Sequence  Selection  Repetition ? truefalse ? true false ? => conditional expression

Conditional Expressions  A conditional expression is a Boolean expression that evaluates to true or false.  Selection structures and repetition structures rely on conditional expressions.  Relational operators and logical operators are used to form conditional expressions. 9

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

11 Logical Operators !not !not &&and &&and ||or ||or

Logical Operators ABA&&BA||B!A!B Truth table for conditional expressions 0 = false 1=true

13 Operator Precedence 1. >= 2. == != 3. && 4. ||

14  (-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

15 Selection Statements  The C++ programming language supports the implementation of selection with: –if statements –switch statements

16 The if statement if(expression) statement; /*single statement executed if expression is true */ if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; }

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

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

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

20 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?

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

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

23 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 << ‘,’ << vowels << ‘,’ << other << endl;

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

REPETITION STATEMENTS while statement do while statement for statement Structuring input loops 25

26 Repetition Statements  The C++ programming language supports the implementation of repetition with: –while statements –do/while statements –for statements

27 The while statement while (expression) statement; { statement block } ? true false

28 The do/while statement dostatement; while (expression) do{ statement block } while (expression) ? true false

29 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; } Program Trace: Output?

30 The for statement initalize ? increment/ decrement true statement(s) false

31 The for statement for(initialization; expression; increment/decrement) statement; for(initialization; expression; increment/decrement) { statement; }

32 The for statement - examples //sum integers from //1 to 10 inclusive #include using namespace std; int main() { int sum=0; for(int i=1;i<11;++i) { sum = sum + i; } cout << sum << endl; return 0; } Alternate solution: //sum integers from //1 to 10 #include using namespace std; int main() { int sum=0; for(int i=1;i<=10;i++) sum = sum + i; cout << sum << endl; return 0; }

The for statement - example //sum odd integers from //1 to n inclusive #include<iostream> using namespace std; int main() { int sum=0, n; int sum=0, n; cout << "enter non-negative integer: "; cout << "enter non-negative integer: "; cin >> n; cin >> n; for(int i=1;i<=n;i+=2) for(int i=1;i<=n;i+=2) sum = sum + i sum = sum + i cout << sum << endl; cout << sum << endl; return 0; return 0;} 33

The for statement - example //sum odd integers from //1 to n inclusive //Alternate Solution #include<iostream> using namespace std; int main() { int sum=0, n; int sum=0, n; cout << "enter non-negative integer: "; cout << "enter non-negative integer: "; cin >> n; cin >> n; for(int i=1;i<=n;++i) for(int i=1;i<=n;++i) { if(i%2) sum = sum + i; if(i%2) sum = sum + i; } cout << sum << endl; cout << sum << endl; return 0; return 0;} 34

35 Practice!  Write a program solution to print all integer values between 1 and n that are multiples of 5 (ie evenly divisible by 5).  Compare your solution with another person's solution.

36 The break statement  break; –terminates loop –execution continues with the first statement following the loop Example: What is the output? for(int i-0; i<=10; ++i) { if(i%2) break; cout << i << endl; }

37 The continue statement  continue; –forces next iteration of the loop, skipping any remaining statements in the loop Example: What is the output? for(int i-0; i<=10; ++i) { if(i%2) continue; cout << i << endl; }

38 Practice ! //This while loop calculates n! int nfact=1, n; cout << "enter positive integer "; cin >> n; while(n > 1) { nfact = nfact*n; n--; } cout << n << "! = " << nfact << endl; //What is the output for n=5? //Write an alternate solution.

Structuring Input Loops 39  Repetition is useful when inputting data from standard input or from a file.  Common repetition structures:  counter-controlled  sentinel-controlled  end-of-data controlled

Counter-controlled Repetition Structure i 0 while i < = counter input data value //Do something with data value increment i end while 40

Sentinel-controlled Repetition Structure input data value while data value ! = sentinel value //Do something with data value input next data value end while 41

eof()-controlled Repetition Structure input data value while end-of-file is not true //Do something with input data input next data value end while 42