SlideShare a Scribd company logo
1 of 10
• C++ Programming
        Club
      Lecture 3
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
                                }
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.
}
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
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
}
• 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.
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;
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");
}
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.
• 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.

More Related Content

What's hot

What's hot (20)

C++ quik notes
C++ quik notesC++ quik notes
C++ quik notes
 
Practiceproblems(1)
Practiceproblems(1)Practiceproblems(1)
Practiceproblems(1)
 
2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units2Bytesprog2 course_2014_c8_units
2Bytesprog2 course_2014_c8_units
 
Control structures
Control structuresControl structures
Control structures
 
Lecture 12 - Recursion
Lecture 12 - Recursion Lecture 12 - Recursion
Lecture 12 - Recursion
 
C++ programming
C++ programmingC++ programming
C++ programming
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Reconstruction
ReconstructionReconstruction
Reconstruction
 
Programming C Part 03
Programming C Part 03Programming C Part 03
Programming C Part 03
 
String .h
String .hString .h
String .h
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...Write a C function that returns the length of a string. Function name: strinq...
Write a C function that returns the length of a string. Function name: strinq...
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Programming Fundamentals
Programming FundamentalsProgramming Fundamentals
Programming Fundamentals
 
C++loop statements
C++loop statementsC++loop statements
C++loop statements
 
c++
c++c++
c++
 
Lập trình C
Lập trình CLập trình C
Lập trình C
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Chap 6 c++
Chap 6 c++Chap 6 c++
Chap 6 c++
 

Similar to C++ Programming Club-Lecture 3

C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
Warui Maina
 

Similar to C++ Programming Club-Lecture 3 (20)

Lecture 4
Lecture 4Lecture 4
Lecture 4
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Lec7 - Loops updated.pptx
Lec7 - Loops updated.pptxLec7 - Loops updated.pptx
Lec7 - Loops updated.pptx
 
L06
L06L06
L06
 
C++ loop
C++ loop C++ loop
C++ loop
 
Control Statement.ppt
Control Statement.pptControl Statement.ppt
Control Statement.ppt
 
Introduction to python programming
Introduction to python programmingIntroduction to python programming
Introduction to python programming
 
C++ Language
C++ LanguageC++ Language
C++ Language
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Pointer
PointerPointer
Pointer
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Chapter 3 Control structures.ppt
Chapter 3 Control structures.pptChapter 3 Control structures.ppt
Chapter 3 Control structures.ppt
 
Python for Machine Learning
Python for Machine LearningPython for Machine Learning
Python for Machine Learning
 
C++ TUTORIAL 3
C++ TUTORIAL 3C++ TUTORIAL 3
C++ TUTORIAL 3
 
Unit2 algorithmic problem_solving
Unit2 algorithmic problem_solvingUnit2 algorithmic problem_solving
Unit2 algorithmic problem_solving
 
Solved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ ExamsSolved Practice Problems For the C++ Exams
Solved Practice Problems For the C++ Exams
 
Lec16.pptx problem specifications computer
Lec16.pptx problem specifications computerLec16.pptx problem specifications computer
Lec16.pptx problem specifications computer
 
algorithm
algorithmalgorithm
algorithm
 
2.overview of c++ ________lecture2
2.overview of c++  ________lecture22.overview of c++  ________lecture2
2.overview of c++ ________lecture2
 
ICP - Lecture 9
ICP - Lecture 9ICP - Lecture 9
ICP - Lecture 9
 

More from Ammara Javed

More from Ammara Javed (15)

Counters
CountersCounters
Counters
 
Registers
RegistersRegisters
Registers
 
Flip flops
Flip flopsFlip flops
Flip flops
 
Logic Gates
Logic GatesLogic Gates
Logic Gates
 
Programmable logic devices
Programmable logic devicesProgrammable logic devices
Programmable logic devices
 
Maxterms
MaxtermsMaxterms
Maxterms
 
Karnaugh Maps
Karnaugh MapsKarnaugh Maps
Karnaugh Maps
 
Demultiplexers
DemultiplexersDemultiplexers
Demultiplexers
 
Minterms
MintermsMinterms
Minterms
 
Multiplexer
MultiplexerMultiplexer
Multiplexer
 
Boolean algebra
Boolean algebraBoolean algebra
Boolean algebra
 
Encoders
EncodersEncoders
Encoders
 
Decoders
DecodersDecoders
Decoders
 
Circuit Analysis-
Circuit Analysis-Circuit Analysis-
Circuit Analysis-
 
The Impact Of Holy Quran In Our Everyday Life
The Impact Of Holy Quran In Our Everyday LifeThe Impact Of Holy Quran In Our Everyday Life
The Impact Of Holy Quran In Our Everyday Life
 

C++ Programming Club-Lecture 3

  • 1. • C++ Programming Club Lecture 3
  • 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.