Académique Documents
Professionnel Documents
Culture Documents
Function
Expression is evaluated the same as an if-else
expression
Statement following expression executed repeatedly as long
as expression has nonzero value
Sample problems
Display all even numbers from 2-20.
Write a program to display numbers
from 1-10 together with its square.
#include<iostream>
#include<iomanip>
using namespace std;
int main()
{
int num, count;
num=1;
cout <<"Enter number of counts: ";
cin >> count;
cout<< "NUMBER" << " " <<"SQUARE"<<endl;
cout<< "======" << " " <<"======"<<endl;
while (num <=count)
{
cout<<num << <<num*num<<endl;
num++;
}
return 0;
}
13
#include<iostream>
using namespace std;
int main()
{
int num, count, total;
total=0;
num=1;
cout <<"Enter number of counts: ";
cin >> count;
cout<< "NUMBER" << " " <<"SQUARE"<<endl;
cout<< "======" << " " <<"======"<<endl;
while (num <=count)
{
cout<<num << <<num*num<<endl;
total=total + num;
num++;
}
cout <<The sum of the numbers is: << total;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int num, count, total, grade;
grade=0;
total=0;
num=1;
cout <<"Enter number of grades: ";
cin >> count;
cout<< "======" << " " <<"======"<<endl;
while (num <=count)
{
cout<<Enter grades: << <<grade<<endl;
grade=grade + num;
num++;
}
cout <<The sum of the numbers is: << total;
return 0;
}
Sample problem
1. Write a program that will ask for a
number of times the user will input a
grade. The program will then calculate
the average of the user-entered grades.
2. Input number of students. Compute for
the final grade of the student by getting
the average of his/her midterm and final
grade.
The do Statement
A repetition statement that evaluates an
expression at the end of the statement
Allows some statements to be executed before
an expression is evaluated
for and while evaluate an expression at the
beginning of the statement
Format:
do
statement;
while
(expression);//
final ;
dont
forget
20
21
22
23
24
25
NUMBER
_ _ _ _ _
1
2
3
4
5
6
7
8
9
10
SQUARE
CUBE
_ _ _ _ _ _ _ _ _ _
1
1
4
8
9
27
16
64
25
125
36
216
49
343
64
512
81
729
100
1000
26
27
28
29
30
31
Exercises
1. Write a program to display numbers from 1-10
with its square root.
2. Write a program to calculate the average of the
user-entered numbers.
32
Nested Loops
A loop contained within another loop
Example:
for(i = 1; i <= 5; i++)
// start of outer loop
{
cout << "\ni is now " << i << endl;
for(j = 1; j <= 4; j++)
{
cout << " j = " << j;
}
}
33
Rules:
For each single trip through outer loop, inner loop
runs through its entire sequence
Different variable to control each loop
Inner loop statements contained within outer loop
A First Book of C++: From Here To There, Third Edition
34
35
#include<iostream>
using namespace std;
int main()
{
int x, y;
for (x=0 ; x<=4; x++)
{
for (y=0; y<=10-x; y++)
cout <<y;
cout <<"\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()
{
int x, y;
for(x =0; x<5; x++)
{
for (y=0; y<10; y++)
cout<<"*";
cout<<"\n";
}
return 0;
}
#include<iostream>
using namespace std;
int main()7
{
char outer, inner;
for (outer ='F'; outer>='A'; outer--)
{
for (inner ='A'; inner<=outer; inner++)
cout<<inner;
cout<<"\n";
}
return 0;
}
Validity Checks
Provided by do statement through filtering of
user-entered input
Example:
do
{
40
41
42
43
Summary
while, for and do statements create loops
These statements evaluate an expression
On the basis of the expression value, either
terminate the loop or continue with it
44
Summary (continued)
The for statement: fixed-count loops
Included in parentheses at top of loop:
Initializing expressions
Tested expression
Expressions that affect the tested expression
Other loop statements can also be included as part
of the altering list
45
Summary (continued)
The do statement checks its expression at the
end of the loop
Body of the loop must execute at least once
do loop must contain statement(s) that either:
Alter the tested expressions value or
Force a break from the loop
46
Break statements
break: forces immediate exit from structures:
Use in switch statements:
The desired case has been detected and
processed
Format:
break;
47
48