Engineering Problem Solving with C++, Etter/Ingber

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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
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.
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)
Outlines Chapter 3 –Chapter 3 – Loops & Revision –Loops while do … while – revision 1.
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.
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.
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:
Previously Repetition Structures While, Do-While, For.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
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.
Chapter 05 (Part III) Control Statements: Part II.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
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.
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.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
REPETITION STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Tevfik Bultan Lecture 4: Introduction to C: Control Flow.
Chapter 5 – Decision Making. Structured Programming Sequence Selection Repetition yesno yes no.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
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
REPETITION CONTROL STRUCTURE
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 Fundamental Concepts
Ch 7: JavaScript Control Statements I.
Chapter 2.2 Control Structures (Iteration)
Control Statements Kingdom of Saudi Arabia
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.
Programming Fundamentals
JavaScript: Control Statements I
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)
Summary Two basic concepts: variables and assignments Basic types:
By Hector M Lugo-Cordero September 3, 2008
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Control Structures: Selection Statement
Presentation transcript:

Engineering Problem Solving with C++, Etter/Ingber Chapter 3 Control Structures Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Control structures Algorithm Development Conditional Expressions Selection Statements Repetition Statements Structuring Input Loops Engineering Problem Solving with C++, Second Edition, J. Ingber

Algorithm Development Structured Programming Evaluation of Alternative Solutions Algorithm Development Engineering Problem Solving with C++, Second Edition, J. Ingber

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. Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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. Engineering Problem Solving with C++, Second Edition, J. Ingber

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 Engineering Problem Solving with C++, Second Edition, J. Ingber

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. Engineering Problem Solving with C++, Second Edition, J. Ingber

Structured Programming Sequence Selection Repetition ? false true ? true false ? => conditional expression Engineering Problem Solving with C++, Second Edition, J. Ingber

Conditional Expressions Relational operators Logical operators Conditional Expressions Engineering Problem Solving with C++, Second Edition, J. Ingber

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. Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Relational Operators == equality != non equality < less than > greater than <= less than equal to >= greater than equal to Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Logical Operators ! not && and || or Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Logical Operators A B A&&B A||B !A !B 1 Truth table for conditional expressions 0 = Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Operator Precedence < <= > >= == != && || Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Practice! - evaluate (-6<0)&&(12>=10) (3.0 >= 2.0) || (3.0 >= 4.0) (3.0 >= 2.0) && (3.0 >= 4.0) true && true results in true true || false results in true true && false results in false Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber if statement switch statement Selection Statements Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Selection Statements The C++ programming language supports the implementation of selection with: if statements switch statements Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The if statement if(expression) statement; /*single statement executed if expression is true */ // statement block is executed if expression is true. if(expression) { statement1; statement2; … statement n; } Engineering Problem Solving with C++, Second Edition, J. Ingber

The if statement - examples if (x>0) ++k; if(x>0) { x=sqrt(x); } Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The if - else statement if(expression) statement; else if(expression) { statement block } else Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The nested if-else if(x > y) if(y < z) k++; else m++; j++; Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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++; j++; What are the values of j, k and m? Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The switch statement switch(expression) { case constant: statement(s); break; /* default is optional*/ default: } Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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. Engineering Problem Solving with C++, Second Edition, J. Ingber

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 }//end while cout << ecount << ‘,’ << vowels << ‘,’ << other << endl; Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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";    { if (rank==5) cout << "Graduate student \n"; cout << "Invalid rank \n"; } Engineering Problem Solving with C++, Second Edition, J. Ingber

Repetition Statements while statement do while statement for statement Structuring input loops Repetition Statements Engineering Problem Solving with C++, Second Edition, J. Ingber

Repetition Statements The C++ programming language supports the implementation of repetition with: while statements do/while statements for statements Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The while statement ? true false while (expression) statement; { statement block } Engineering Problem Solving with C++, Second Edition, J. Ingber

The do/while statement ? true false do statement; while (expression) { statement block } while (expression) Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber Practice! #include <iostream> 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? Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The for statement initalize ? false increment/ decrement true statement(s) statement(s) Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber The for statement for(initialization; expression; increment/decrement) statement; { } Engineering Problem Solving with C++, Second Edition, J. Ingber

The for statement - examples Alternate solution: //sum integers from //1 to 10 #include<iostream> using namespace std; int main() { int sum=0; for(int i=1;i<=10;i++) sum = sum + i; cout << sum << endl; return 0; } //sum integers from //1 to 10 inclusive #include<iostream> using namespace std; int main() { int sum=0; for(int i=1;i<11;++i) sum = sum + i; } cout << sum << endl; return 0; Engineering Problem Solving with C++, Second Edition, J. Ingber

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

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; cout << "enter non-negative integer: "; cin >> n; for(int i=1;i<=n;++i) if(i%2) sum = sum + i; } cout << sum << endl; return 0; Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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. Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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; } Engineering Problem Solving with C++, Second Edition, J. Ingber

The continue statement 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; } Engineering Problem Solving with C++, Second Edition, J. Ingber

Engineering Problem Solving with C++, Second Edition, J. Ingber 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. Engineering Problem Solving with C++, Second Edition, J. Ingber

Structuring Input Loops Repetition is useful when inputting data from standard input or from a file. Common repetition structures: counter-controlled sentinel-controlled end-of-data controlled Engineering Problem Solving with C++, Second Edition, J. Ingber

Counter-controlled Repetition Structure while i < = counter input data value //Do something with data value increment i end while Engineering Problem Solving with C++, Second Edition, J. Ingber

Sentinel-controlled Repetition Structure input data value while data value ! = sentinel value //Do something with data value input next data value end while Engineering Problem Solving with C++, Second Edition, J. Ingber

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 Engineering Problem Solving with C++, Second Edition, J. Ingber