What is the out put #include using namespace std; void main() { int i; for(i=1;i<10;i++) { cout<<i<<endl; } cout<<endl<<i<<endl; }

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

Computer Science 1620 Loops.
Introduction to working with Loops  2000 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Computers and Programming.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
1 Lecture 10:Control Structures II (Repetition) Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
Chapter 5: Control Structures II (Repetition)
CHAPTER 5 CONTROL STRUCTURES II (Repetition). In this chapter, you will:  Learn about repetition (looping) control structures  Explore how to construct.
Chapter 5: Control Structures II (Repetition)
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 5: Control Structures II (Repetition)
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.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
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.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
Chapter 5: Control Structures II (Repetition). Objectives In this chapter, you will: – Learn about repetition (looping) control structures – Learn how.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
Loop.  While Loop  Do-while Loop  For Loop Continue Statement Conclusion Loop Loop.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
GAME102 - INTRO WHILE LOOPS G. MacKay. Fundamental Control Structures  STRAIGHT LINE  CONDITIONAL  LOOPS.
Chapter 4: Control Structures II
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.
 2003 Prentice Hall, Inc. All rights reserved. Outline 1 fig02_07.cpp (1 of 2) 1 // Fig. 2.7: fig02_07.cpp 2 // Class average program with counter-controlled.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 5 Looping.
Overview Go over parts of quiz? Another iteration structure for loop.
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.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 5: Looping.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 5: Control Structures II (Repetition)
Copyright © 2012 Pearson Education, Inc. Chapter 5: Loops.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Think First, Code Second Understand the problem Work out step by step procedure for solving the problem (algorithm) top down design and stepwise refinement.
Copyright 2006 Addison-Wesley Brief Version of Starting Out with C++ Chapter 5 Looping.
Chapter Looping 5. The Increment and Decrement Operators 5.1.
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
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.
Chapter five exercises. a. false; b. true; c. false; d. true; e. true; f. true; g. true; h. false.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
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.
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
Control Structures Sequential execution Transfer of control
REPETITION CONTROL STRUCTURE
while Repetition Structure
Chapter 5: Control Structures II (Repetition)
C++ Programming: CS150 For.
Controlling execution - iteration
Week 4 – Repetition Structures / Loops
Repetition-Sentinel,Flag Loop/Do_While
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.
Chapter 5: Looping Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Lecture 4B More Repetition Richard Gesick
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.
Iteration with While You can say that again.
Chapter 2.2 Control Structures (Iteration)
Let’s all Repeat Together
2.6 The if/else Selection Structure
Chapter 4 Repetition Structures
Programming Fundamental
Presentation transcript:

What is the out put #include using namespace std; void main() { int i; for(i=1;i<10;i++) { cout<<i<<endl; } cout<<endl<<i<<endl; }

What is the out put #include using namespace std; void main() { int i; for(i=1;i<10;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }

What is the out put #include using namespace std; void main() { int i; for(;i<10;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }

Infinite loop

What is the out put #include using namespace std; void main() { int i; for(i=1;;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }

Infinite loop

What is the out put #include using namespace std; void main() { int i; for(i=1;i<10;) { cout<<i<<endl; } cout<<endl<<i<<endl; }

Infinite loop

What is the out put #include using namespace std; void main() { int i; for(;;) { cout<<i<<endl; } cout<<endl<<i<<endl; }

Infinite loop

What is the out put #include using namespace std; void main() { int i; for(i=1;i<10;++i); { cout<<i<<endl; } cout<<endl<<i<<endl; }

The out put

x to the y power // raise x to the y power #include using namespace std; int main() { int x, y, i, power; i = 1; power = 1; cout << "Enter base as an integer: "; cin >> x; cout << "Enter exponent as an integer: "; cin >> y; while ( i <= y ) { power *= x; ++i; } cout << power << endl; return 0; }

// Class average program with counter- controlled repetition (Example) #include using namespace std; int main() { int total, // sum of grades gradeCounter, // number of grades entered grade, // one grade average; // average of grades // initialization phase total = 0; // clear total gradeCounter = 1; // prepare to loop // processing phase while ( gradeCounter <= 5 ) { // loop 10 times cout << "Enter grade: "; // prompt for input cin >> grade; // input grade total = total + grade; // add grade to total gradeCounter = gradeCounter + 1; // increment counter } // termination phase average = total / 5; // integer division cout << "Class average is " << average << endl; return 0; // indicate program ended successfully }

Example // Counter-controlled repetition #include using namespace std; int main() { int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition cout << counter << endl; ++counter; // increment } return 0; }

// Summation with for example #include using namespace std; int main() { int sum = 0; for ( int number = 1; number <= 10; number += 2 ) { cout<<number<<endl; sum += number; } cout <<endl<< "Sum is " << sum << endl<<endl; return 0; }

Summation with for example

Using the break statement in a for structure // Using the break statement in a for structure #include using namespace std; int main() { // x declared here so it can be used after the loop int x; for ( x = 1; x <= 10; x++ ) { if ( x == 5 ) break; // break loop only if x is 5 cout << x << " "; } cout << "\nBroke out of loop at x of " << x << endl; return 0; }

// Using the continue statement in a for structure #include using namespace std; int main() { for ( int x = 1; x <= 10; x++ ) { if ( x == 5 ) continue; // skip remaining code in loop // only if x is 5 cout << x << " "; } cout << "\nUsed continue to skip printing the value 5" << endl; return 0; }

// Using the continue statement in a for structure

example #include using namespace std; int main() { int count = 1; while ( count <= 10 ) { cout << (count % 2 ? "****" : " ") << endl; ++count; } return 0; }

Nested Control Structures #include using namespace std; int main () { int i,j; for (i = 1; i <= 5 ; i++) { for (j = 1; j <= i; j++) cout << "*"; cout << endl; } return 0; }

Nested Control Structures

#include using namespace std; int main () { int i,j; for (i = 1; i <= 5 ; i++) { for (j = i; j <= 5; j++) cout << "*"; cout << endl; } return 0; }

Nested Control Structures

#include using namespace std; int main () { int i,j; for (i = 1; i <= 5 ; i++) { for (j = 1; j <= 5; j++) cout << "*"; cout << endl; } return 0; }

Nested Control Structures

Count Control #include using namespace std; int main() { int limit; //variable to store the number of items //in the list int number; //variable to store the number int sum; //variable to store the sum int counter; //loop control variable cout << "Line 1: Enter number of data for processing" << endl; //Line 1 cin >> limit; //Line 2 sum = 0; //Line 3 counter = 0; //Line 4 while (counter < limit) //Line 5 { cout<<"the number is:"; cin >> number; //Line 6 sum = sum + number; //Line 7 counter++; //Line 8 } cout << "Line 9: The sum of the " << limit << " numbers = " << sum << endl; //Line 9 if (counter != 0) //Line 10 cout << "Line 11: The average = " << sum / counter << endl; //Line 11 else //Line 12 cout << "Line 13: No input." << endl; //Line 13 return 0; }

//Flag-controlled while loop. //Number guessing game. #include using namespace std; int main() { //declare the variables int num; //variable to store the random //number int guess; //variable to store the number //guessed by the user bool done; //boolean variable to control //the loop num = (rand() + time(0)) % 100; //Line 1 done = false; //Line 2 while (!done) //Line 3 { //Line 4 cout << "Enter an integer greater" << " than or equal to 0 and "<< "less than 100: "; //Line 5 cin >> guess; //Line 6 cout << endl; //Line 7 if (guess == num) //Line 8 { //Line 9 cout << "You guessed the correct " << "number." << endl; //Line 10 done = true; //Line 11 } //Line 12 else //Line 13 if (guess < num) //Line 14 cout << "Your guess is lower " << "than the number.\n" << "Guess again!" << endl; //Line 15 else //Line 16 cout << "Your guess is higher " << "than the number.\n" << "Guess again!" << endl; //Line 17 } //end while //Line 18 return 0; }

Sentinel Control //Program: AVG2 #include using namespace std; const int SENTINEL = -999; int main() { int number; //variable to store the number int sum = 0; //variable to store the sum int count = 0; //variable to store the total //numbers read cout << "Line 1: Enter integers ending with " << SENTINEL << endl; //Line 1 cin >> number; //Line 2 while ( number != SENTINEL ) //Line 3 { sum = sum + number; //Line 4 count++; //Line 5 cin >> number; //Line 6 } cout << "Line 7: The sum of the " << count << " numbers is " << sum << endl; //Line 7 if (count != 0) //Line 8 cout << "Line 9: The average is " << sum / count << endl; //Line 9 else //Line 10 cout << "Line 11: No input." << endl; //Line 11 return 0; }

The do … while Loop #include using namespace std; int main() { int i=1; do { cout<<i<<endl; i++; } while(i<=10); cout<<endl<<i<<endl; return 0; }

#include using namespace std; int main() { int i=1; do cout<<i; { cout<<endl; i++; } while(i<=10); cout<<endl<<i<<endl; return 0; }

#include using namespace std; int main() { int i=1; do { cout<<i<<endl; i++; } while(i<=10) cout<<endl<<i; return 0; }