Intro to Programming Week # 6 Repetition Structure Lecture # 10

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
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.
1 Lecture 11:Control Structures II (Repetition) (cont.) Introduction to Computer Science Spring 2006.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 5 Looping.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
1 10/11/06CS150 Introduction to Computer Science 1 do/while and Nested Loops.
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.
Computer Science 1620 Accumulators. Recall the solution to our financial program: #include using namespace std; int main() { double balance = ;
Computer Science 1620 Multi-Dimensional Arrays. we used arrays to store a set of data of the same type e.g. store the assignment grades for a particular.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Summary of Loops Programming. COMP102 Prog Fundamentals I: Summary of Loops /Slide 2 Which Loop to Use? l for loop n for calculations that are repeated.
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.
Computer Science 1620 Lifetime & Scope. Variable Lifetime a variable's lifetime is finite Variable creation: memory is allocated to the variable occurs.
Department of Computer Science and Engineering, HKUST 1 HKUST Summer Programming Course 2008 C++ Control Statements ~ Selection and Iteration.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
Looping II (for statement). CSCE 1062 Outline  for statement  Nested loops  Compound assignment operators  Increment and decrement operators.
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.
First steps Jordi Cortadella Department of Computer Science.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
C++ for Engineers and Scientists, Third Edition1 Objectives In this chapter, you will learn about: Basic loop structures while loops Interactive while.
While Loops Programming. COMP102 Prog Fundamentals I: while Loops/Slide 2 Shortcut Assignments l C++ has a set of shortcut operators for applying an operation.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
+ Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 5: Looping.
Fundamental Programming Fundamental Programming for Loops.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Fundamental Programming Fundamental Programming More on Repetition.
Alternate Version of STARTING OUT WITH C++ 4 th 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.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Functions Jordi Cortadella Department of Computer Science.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
Computer Programming -1-
Introduction to Loop. Introduction to Loops: The while Loop Loop: part of program that may execute > 1 time (i.e., it repeats) while loop format: while.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
Introduction to Computer Programming
Review 1.
Introduction to C++ Programming Language
Unit-1 Introduction to Java
C++ Programming: CS150 For.
Engineering Problem Solving with C++, Etter/Ingber
CS150 Introduction to Computer Science 1
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.
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Chapter 4 Loops Case Studies
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.
Repetition Statements
Intro to Programming Week # 3 If-else Statement Lecture # 6
Intro to Programming Week # 4 Control Structure Lecture # 8
Starting Out with C++: From Control Structures through Objects
Pointers & Functions.
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
Repetition Control Structure
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Looping III (do … while statement)
By Hector M Lugo-Cordero September 3, 2008
2.6 The if/else Selection Structure
Introduction To Programming
do/while Selection Structure
Arrays Arrays A few types Structures of related data items
CS150 Introduction to Computer Science 1
CS149D Elements of Computer Science
Pointers & Functions.
Presentation transcript:

Intro to Programming Week # 6 Repetition Structure Lecture # 10 Department of Computer Science & Engineering Air University Intro to Programming Week # 6 Repetition Structure Lecture # 10 By: Saqib Rasheed

. Nested loops

Nested Loops A loop can be nested inside of another loop. C++ allows at least 256 levels of nesting When working with nested loops, the outer loop changes only after the inner loop is completely finished

The syntax for a nested for loop statement in C++ for ( init; condition; increment ) { statement(s); } statement(s); // you can put more statements.

The syntax for a nested while loop statement in C++ while(condition) { statement(s); } statement(s); // you can put more statements.

The syntax for a nested do...while loop statement in C++ do { statement(s); // you can put more statements. statement(s); } while( condition );

For loop nesting

Nested loops (loop in loop) b ************* cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) cout << “*”; } cout << endl; a

Nested loops (2) b * ** *** a **** int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) if (j > i) break; cout << “*”; } cout << endl;

Nested loops (3) b * ** *** a **** int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b && j < i; j++) { cout << “*”; } cout << endl; if (j > i) break; j <= i;

Nested loops (4) b ************* ************ *********** a ********** int a,b; cin >> a >> b; for (int i = 0; i < a; i++) { for (int j=0; j<b; j++) if (j < i) cout << “ ”; else cout << “*”; } cout << endl;

Write a program in C++ that prints a tables Starting from 1 12. 1 2 3 4 5 6 7 8 9 10 11 12 2 4 6 8 10 12 14 16 18 20 22 24 3 6 9 12 15 18 21 24 27 30 33 36 4 8 12 16 20 24 28 32 36 40 44 48 5 10 15 20 25 30 35 40 45 50 55 60 6 12 18 24 30 36 42 48 54 60 66 72 7 14 21 28 35 42 49 56 63 70 77 84 8 16 24 32 40 48 56 64 72 80 88 96 9 18 27 36 45 54 63 72 81 90 99 108 10 20 30 40 50 60 70 80 90 100 110 120 11 22 33 44 55 66 77 88 99 110 121 132 12 24 36 48 60 72 84 96 108 120 132 144 Air University

#include <iomanip> // defines setw() #include <iostream> // defines cout using namespace std; int main() { for (int x=1; x <= 12; x++) for (int y=1; y <= 12; y++) cout << setw(4) << x*y; cout << endl; } return 0; } . Air University

Assignment Due next Class Copied Assignments will be marked zero Late Assignment not accepted Hard Copies only Write your name roll #, Section

Nested loops (5) * *** ***** ******* ********* ***********

Develop a code in C++ that generate the following series Develop a code in C++ that generate the following series .Use nested while loop! . Series No. 1 No. 2 No. 3 1 2 2 3 3 3 4 4 4 4 1 2 3 4 5 6 7 8 9 10 1 1 2 1 2 3 1 2 3 4 Air University