Chapter 4 Loops Case Studies

Slides:



Advertisements
Similar presentations
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Advertisements

 2003 Prentice Hall, Inc. All rights reserved. 1 Functions and Recursion Outline some useful problems.
Dr. Yang, Qingxiong (with slides borrowed from Dr. Yuen, Joe) LT4: Control Flow - Loop CS2311 Computer Programming.
Chapter 4 Loops Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X.
Chapter 5 Functions.
1 Engineering Problem Solving With C++ An Object Based Approach Chapter 3 Control Structures.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
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 4 Summation.
Chapter 4 Loops Liang, Introduction to Java Programming, Fifth Edition, (c) 2005 Pearson Education, Inc. All rights reserved
1 10/9/06CS150 Introduction to Computer Science 1 for Loops.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5: Control Structures II (Repetition)
What is the out put #include using namespace std; void main() { int i; for(i=1;i
Programming is instructing a computer to perform a task for you with the help of a programming language.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Chapter 4 Loops Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.1 Chapter 5 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loops 1. Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved while Loop Flow.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
© Copyright 2013 by Pearson Education, Inc. All Rights Reserved.1 Chapter 5 Loops.
Objective: Students will be able to: Declare and use variables Input integers.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 4 Loops.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Multidimensional.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 4 Loops.
Loop. 3.1Introduction to Repetition Structures Loop – a block of code that, under certain conditions will be executed repeatedly. Do Prompt for and input.
Infinite for Loop If you omit the test condition, the value is assumed to be TRUE so the loop will continue indefinitely unless you provide some other.
Chapter 4 Repetition Structures
Introduction to Computer Programming
Chapter 4: Looping Structures LECTURER : MRS ROHANI HASSAN
Review 1.
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
C++ Programming: CS150 For.
Intro to Programming Week # 6 Repetition Structure Lecture # 10
Chapter 2 Assignment and Interactive Input
Engineering Problem Solving with C++, Etter/Ingber
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.
Chapter 4 Repetition Structures
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.
Chapter 4 Control structures and Loops
Chapter 4 Repetition Structures
While Loop Design ENGI 1020 Fall 2018.
Starting Out with C++: From Control Structures through Objects
Chapter 5 Loops Liang, Introduction to Java Programming, Tenth Edition, (c) 2015 Pearson Education, Inc. All rights reserved.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4 Loops Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Control Structures I (Selection)
Let’s all Repeat Together
2.6 The if/else Selection Structure
Statements and flow control
CS149D Elements of Computer Science
COMS 261 Computer Science I
Introduction to Algorithms and Programming COMP151
Chapter 1 Introduction to Algebra: Integers
Chapter 4: Loops and Iteration
Presentation transcript:

Chapter 4 Loops Case Studies Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved. 0136097200

Using break and continue Two keywords, break and continue, can be used in loop statements to provide the loop with additional control. Break: immediately ends the innermost loop that contains it. In other words, break breaks out of a loop. continue: only ends the current iteration. Program control goes to the end of the loop body. In other words, continue breaks out of an iteration.

Example: TestBreak TestBreak.cpp Write a program that adds the integers from 1 to 20 in this order to sum until sum is greater than or equal to 100. #include <iostream> using namespace std; int main() { int sum = 0; int number = 0; while (number < 20) { number++; sum += number; if (sum >= 100) break; } cout << "The number is " << number << endl; cout << "The sum is " << sum << endl; return 0; }

Example: TestContinue TestContinue.cpp Write a program that adds all the integers from 1 to 20 except 10 and 11 to sum. #include <iostream> using namespace std; int main() { int sum = 0; int number = 0; while (number < 20) { number++; if (number == 10 || number == 11) continue; sum += number; } cout << "The sum is " << sum<<endl; return 0; } number is not added to sum when it is 10 or 11

Exercise 4.25 : Computing π You can approximate π by using the following series: Write a program that displays the π value for i=100000.

Exercise 4.18 : Print a Pyramid pattern Use nested loops that print the following pattern:

Homework Use nested loops that print the following pattern: (2) Write a program that displays the e value for i=100000.