2. Loops: While And For.
The while Statement:
Tests a condition and repeats a block of one or
more statements as long as the condition is
true.
EXAMPLE
While (expression) x=0; //initial statement, x starts from 0
{ while (x<2) //test expression, It
{ //shall continue only till x<2
statement(s);
cout<<x<<endl; //output
} x++; //increament expression
}
3. Lets Look At This Example…..
The number we are using is in
#include <iostream> the form of integers and we
need to find out the sqaure and
using namespace std;
cube and make 3 columns in the
void main() output. “n” means a new line
{
The number is starting from 1
int number;
and continues till 10 as the
cout << "NUMBER SQUARE CUBEn" condition is that the number
<< "------ ------ ----n"; lasts as long as its less than 11
number = 1;
while (number < 11) The output is that we first have
number than square and at the
{
end cube with a space so that it
cout << number fits with the categories above!
<< " "<< number * number << " “
<< number * number * number << endl;
The increment is +1 which means
number++; // increment num
that the next no will be +1 to the
} previous no eg. if the previous
system ("pause"); number is 2 then the next
number will be 2+1=3 and so on.
}
4. Lets Look At Another Example
• Write a C++ program that converts feet to meters.
The program should
display feet from 3 to 30 in 3-foot increments and
the corresponding meter equivalents.
Use the relationship that 1 meter = 3.28 feet.
Things that can be determined by looking at the question are the
following:
1-We need to convert feet to meters and for that the formulae is
M=3.28*no (in feet)
2-We need to make 2 columns named FEET and METERS.
3-The no should state from 3 (n=3) and the while statement should
continue till 30 (n<30) with a 3-foot increment (no=no+3) which
means that if the previous number is 9 the next number will be
9+3=12 and so on…
NOW LETS SOLVE THIS PROBLEM TOGATHER
5. Solution: We are using int as we will
be dealing with numbers
#include <iostream> only
using namespace std; We need to make 2
void main() columns of feet and
{ meter.
int no, m;
cout<<"FEET METERSn"
<<"---- ------n"; The number is starting
from 3 till 27 as its less
no=3; than 30 and then we find
while (no<30) m (meters).
{
m=3.28*no;
cout<<no<<" "<<m<<endl; At the end we simply output
no=no+3; the no and m and update the
} counter as +3 so that we get
numbers like 3,6,9… and their
system ("pause"); corresponding values in m
}
6. • Home Work:
1.Write a Program to print the numbers 2 to 10
in increments of two. The output of your
program should be the following: 2 4 6 8 10
2. Write a C++ program that converts gallons to
liters. The program should
display gallons from 10 to 20 in one-gallon
increments and the corresponding liter
equivalents. Use the relationship that 1 gallon =
3.785 liters.
7. For Loop:
The for Statement:
In C++, a for loop is constructed by using a for
statement. This statement performs the
same functions as the while statement but uses
a different form.
- It enables count controlled loops.
for (initial statement; test expression; increment
expression)
{ Example:
statement(s); for(i=1; i<5; i++)
} cout<<i<<endl;
8. Lets Look On This Example
-Use a counter named i that has an initial value
of 1, a final value of 20, and an increment of 1.
Solution:
#include<iostream>
using namespace std;
void main ()
{
int i;
for (i=1; i<20; i++)
cout<<i<<endl;
system ("pause");
}
9. Main Difference Between For And
While Loop
• While loop - used for looping until a condition
is satisfied and when it is unsure how many
times the code should be in loop.
• For loop - used for looping until a condition is
satisfied but it is used when you know how
many times the code needs to be in loop.
10. • Homework
1-Use a counter named j that has an initial value of
one, a final value of 100, and an
increment of 5.
2-Write a C++ program to convert kilometers/hr to
miles/hr. The program should produce a table of 10
conversions, starting at 60 km/hr and incremented
by 5 km/hr. (no=60; no<=110); no=no+5)
The display should have appropriate headings and
list each km/hr and its equivalent miles/hr
value. Use the relationship that 1 kilometer =0.6241
miles.