SlideShare uma empresa Scribd logo
1 de 44
What is the out put
#include<iostream>
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<iostream>
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<iostream>
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<iostream>
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<iostream>
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<iostream>
using namespace std;
void main()
{
int i;
for(;;)
{
cout<<i<<endl;
}
cout<<endl<<i<<endl;
}
Infinite loop
What is the out put
#include<iostream>
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 <iostream>
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 <iostream>
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 <iostream>
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 <iostream>
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 <iostream>
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
// Using the continue statement in a for structure
#include <iostream>
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 <iostream>
using namespace std;
int main()
{
int count = 1;
while ( count <= 10 ) {
cout << (count % 2 ? "****" : "++++++++")
<< endl;
++count;
}
return 0;
}
Nested Control Structures
#include <iostream>
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
Nested Control Structures
#include <iostream>
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
Nested Control Structures
#include <iostream>
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<iostream>
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 <iostream>
#include <cstdlib>
#include <ctime>
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 <iostream>
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 <iostream>
using namespace std;
int main()
{
int i=1;
do
{
cout<<i<<endl;
i++;
}
while(i<=10);
cout<<endl<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i=1;
do
cout<<i;
{
cout<<endl;
i++;
}
while(i<=10);
cout<<endl<<i<<endl;
return 0;
}
#include <iostream>
using namespace std;
int main()
{
int i=1;
do
{
cout<<i<<endl;
i++;
}
while(i<=10)
cout<<endl<<i;
return 0;
}
ch5_additional.ppt

Mais conteúdo relacionado

Semelhante a ch5_additional.ppt

Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentationSourabhGour9
 
how to reuse code
how to reuse codehow to reuse code
how to reuse codejleed1
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfshreeaadithyaacellso
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solvingSyed Umair
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd StudyChris Ohk
 
ch6_additional.ppt
ch6_additional.pptch6_additional.ppt
ch6_additional.pptLokeshK66
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4sajidpk92
 

Semelhante a ch5_additional.ppt (20)

Project in programming
Project in programmingProject in programming
Project in programming
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
C++ practical
C++ practicalC++ practical
C++ practical
 
Oop object oriented programing topics
Oop object oriented programing topicsOop object oriented programing topics
Oop object oriented programing topics
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf4th_Ed_Ch03.pdf
4th_Ed_Ch03.pdf
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
I have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdfI have written the code but cannot complete the assignment please help.pdf
I have written the code but cannot complete the assignment please help.pdf
 
3 rd animation
3 rd animation3 rd animation
3 rd animation
 
Assignement of programming & problem solving
Assignement of programming & problem solvingAssignement of programming & problem solving
Assignement of programming & problem solving
 
10 template code program
10 template code program10 template code program
10 template code program
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
ch6_additional.ppt
ch6_additional.pptch6_additional.ppt
ch6_additional.ppt
 
C++ TUTORIAL 4
C++ TUTORIAL 4C++ TUTORIAL 4
C++ TUTORIAL 4
 
c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 

Mais de LokeshK66

Iot application in smart cities .pptx
Iot    application in  smart  cities .pptxIot    application in  smart  cities .pptx
Iot application in smart cities .pptxLokeshK66
 
building mat.pptx
building mat.pptxbuilding mat.pptx
building mat.pptxLokeshK66
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.pptLokeshK66
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.pptLokeshK66
 
9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.pptLokeshK66
 
ch4_additional.ppt
ch4_additional.pptch4_additional.ppt
ch4_additional.pptLokeshK66
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.pptLokeshK66
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.pptLokeshK66
 

Mais de LokeshK66 (8)

Iot application in smart cities .pptx
Iot    application in  smart  cities .pptxIot    application in  smart  cities .pptx
Iot application in smart cities .pptx
 
building mat.pptx
building mat.pptxbuilding mat.pptx
building mat.pptx
 
9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt9781423902096_PPT_ch07.ppt
9781423902096_PPT_ch07.ppt
 
9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt9781423902096_PPT_ch08.ppt
9781423902096_PPT_ch08.ppt
 
9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt9781423902096_PPT_ch09.ppt
9781423902096_PPT_ch09.ppt
 
ch4_additional.ppt
ch4_additional.pptch4_additional.ppt
ch4_additional.ppt
 
ch9_additional.ppt
ch9_additional.pptch9_additional.ppt
ch9_additional.ppt
 
SQLSecurity.ppt
SQLSecurity.pptSQLSecurity.ppt
SQLSecurity.ppt
 

Último

The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxShobhayan Kirtania
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...Pooja Nehwal
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphThiyagu K
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room servicediscovermytutordmt
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDThiyagu K
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionSafetyChain Software
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajanpragatimahajan3
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformChameera Dedduwage
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...Sapna Thakur
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 

Último (20)

Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Advance Mobile Application Development class 07
Advance Mobile Application Development class 07Advance Mobile Application Development class 07
Advance Mobile Application Development class 07
 
The byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptxThe byproduct of sericulture in different industries.pptx
The byproduct of sericulture in different industries.pptx
 
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...Russian Call Girls in Andheri Airport Mumbai WhatsApp  9167673311 💞 Full Nigh...
Russian Call Girls in Andheri Airport Mumbai WhatsApp 9167673311 💞 Full Nigh...
 
Z Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot GraphZ Score,T Score, Percential Rank and Box Plot Graph
Z Score,T Score, Percential Rank and Box Plot Graph
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
9548086042 for call girls in Indira Nagar with room service
9548086042  for call girls in Indira Nagar  with room service9548086042  for call girls in Indira Nagar  with room service
9548086042 for call girls in Indira Nagar with room service
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Mastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory InspectionMastering the Unannounced Regulatory Inspection
Mastering the Unannounced Regulatory Inspection
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
social pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajansocial pharmacy d-pharm 1st year by Pragati K. Mahajan
social pharmacy d-pharm 1st year by Pragati K. Mahajan
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
A Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy ReformA Critique of the Proposed National Education Policy Reform
A Critique of the Proposed National Education Policy Reform
 
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
BAG TECHNIQUE Bag technique-a tool making use of public health bag through wh...
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 

ch5_additional.ppt

  • 1. What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;i<10;i++) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 2.
  • 3. What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;i<10;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 4.
  • 5. What is the out put #include<iostream> using namespace std; void main() { int i; for(;i<10;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 7. What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;;++i) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 9. What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;i<10;) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 11. What is the out put #include<iostream> using namespace std; void main() { int i; for(;;) { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 13. What is the out put #include<iostream> using namespace std; void main() { int i; for(i=1;i<10;++i); { cout<<i<<endl; } cout<<endl<<i<<endl; }
  • 15. x to the y power // raise x to the y power #include <iostream> 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; }
  • 16.
  • 17.
  • 18. // Class average program with counter- controlled repetition (Example) #include <iostream> 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 }
  • 19.
  • 20. Example // Counter-controlled repetition #include <iostream> using namespace std; int main() { int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition cout << counter << endl; ++counter; // increment } return 0; }
  • 21.
  • 22. // Summation with for example #include <iostream> 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; }
  • 24. Using the break statement in a for structure // Using the break statement in a for structure #include <iostream> 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; }
  • 25.
  • 26. // Using the continue statement in a for structure // Using the continue statement in a for structure #include <iostream> 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; }
  • 27. // Using the continue statement in a for structure
  • 28. example #include <iostream> using namespace std; int main() { int count = 1; while ( count <= 10 ) { cout << (count % 2 ? "****" : "++++++++") << endl; ++count; } return 0; }
  • 29.
  • 30. Nested Control Structures #include <iostream> 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; }
  • 32. Nested Control Structures #include <iostream> 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; }
  • 34. Nested Control Structures #include <iostream> 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; }
  • 36. Count Control #include<iostream> 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; }
  • 37. //Flag-controlled while loop. //Number guessing game. #include <iostream> #include <cstdlib> #include <ctime> 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; }
  • 38. Sentinel Control //Program: AVG2 #include <iostream> 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; }
  • 39. The do…while Loop #include <iostream> using namespace std; int main() { int i=1; do { cout<<i<<endl; i++; } while(i<=10); cout<<endl<<i<<endl; return 0; }
  • 40.
  • 41. #include <iostream> using namespace std; int main() { int i=1; do cout<<i; { cout<<endl; i++; } while(i<=10); cout<<endl<<i<<endl; return 0; }
  • 42.
  • 43. #include <iostream> using namespace std; int main() { int i=1; do { cout<<i<<endl; i++; } while(i<=10) cout<<endl<<i; return 0; }