0% found this document useful (0 votes)
100 views28 pages

Programming Loops: While, Do-While, For

This document provides an introduction to various programming concepts including while loops, do-while loops, for loops, relational operators, and increment/decrement operators. It includes syntax examples and sample code to calculate multiplication tables using for loops. Key points covered are: - While loops execute zero or more times, do-while loops execute one or more times - For loops allow initialization, termination, and increment conditions to be specified - Examples show using for loops to output multiplication tables - Increment/decrement, compound assignment, and remainder operators are introduced

Uploaded by

api-3701035
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
100 views28 pages

Programming Loops: While, Do-While, For

This document provides an introduction to various programming concepts including while loops, do-while loops, for loops, relational operators, and increment/decrement operators. It includes syntax examples and sample code to calculate multiplication tables using for loops. Key points covered are: - While loops execute zero or more times, do-while loops execute one or more times - For loops allow initialization, termination, and increment conditions to be specified - Examples show using for loops to output multiplication tables - Increment/decrement, compound assignment, and remainder operators are introduced

Uploaded by

api-3701035
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd

Introduction to Programming

while loop
while (condition)
{
statements;
:
}
statements;
While loop executes zero
or more times. What if we
want the loop to execute
at least one time?
do-while
Do while loop execute on
or more times
Syntax of do-while loop
do
{
statements ;

}
while ( condition ) ;
Example-Guessing game
char c ;
int tryNum = 1 ;
do
{
cout << "Please enter your guess by pressing a character key from a to z “ ;
cin >> c ;
if ( c == 'z‘ )
{
cout << "Congratulations! you guessed the right answer“ ;
tryNum = 6 ;
}
else
tryNum = tryNum + 1 ;
} while ( tryNum <= 5 ) ;
Flow chart for do-while loop

Do-while

Process

false
condition Exit

true
Relational Operators
char c ;
int tryNum , maxTries ;
tryNum = 1 ;
maxTries = 5 ;
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) )
{
cout << "Guess the alphabet between a to z “ ;
cin >> c ;
tryNum = tryNum + 1 ;
}
for Loop
For loop

for ( initialization condition ; termination condition ; increment condition )


{
statement ( s ) ;
}
Example
int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )
cout << counter;

Output
0123456789
Table for 2
2x1=2
2x2=4
2x3=6
:
:
2 x 10 = 20
Example - Calculate Table for 2

#include <iostream.h>
main ( )
{
int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}
}
Output
2 x1 = 2
2x2=4
2x3=6
:
:
2 x 10 = 20
Flow chart for the ‘Table’ example
Start

counter=1

While

No Exit
counter <=10?

yes

Print 2*counter

Counter =
counter + 1

Stop
Example: Calculate Table- Enhanced

#include <iostream.h>
main ( )
{
int number ;
int maxMultiplier ;
int counter ;
maxMultiplier = 10 ;
cout << " Please enter the number for which you wish to construct the table “ ;
cin >> number ;
for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )
{
cout << number <<" x " << counter<< " = " << number * counter << "\n“ ;
}
}
Always think re-use
Don’t use explicit constants
Increment operator
++
counter ++ ;
same as
counter = counter + 1;
Decrement operator
--

counter -- ;
same as
counter = counter - 1
+=
counter += 3 ;
same as
counter = counter + 3 ;
-=
counter -= 5 ;
same as
counter = counter – 5 ;
*=
x*=2
x=x*2
/=
x /= 2

x=x/2
Compound Assignment
Operators

operator=
%=
x %= 2 ;
same as
x=x%2;
Comments
Write comment at the top program
to show what it does
Write comments that mean some
thing
In today’s lecture
Do - while
– Executes the code at least ones
For loop
– Executes at least zero times
Short hand operators
– Incrementing
– Decrementing
Compound assignment operator

You might also like