CS201- Introduction to Programming- Lecture 07
-
Upload
bilal-ahmed -
Category
Education
-
view
20 -
download
6
description
Transcript of CS201- Introduction to Programming- Lecture 07
Lecture 7Lecture 7
Introduction to Introduction to ProgrammingProgramming
while loopwhile loop
while (condition)while (condition)
{{
statements;statements;::
}}
statements;statements;
While loop executes zeroWhile loop executes zero
or more times. What if weor more times. What if we
want the loop to executewant the loop to execute
at least one time?at least one time?
do-whiledo-while
Do while loop execute Do while loop execute onon
or more timesor more times
Syntax of do-while Syntax of do-while looploop
dodo{{
statements ;statements ;
}}while ( condition ) ; while ( condition ) ;
Example-Guessing Example-Guessing gamegame
char c ;char c ; int tryNum = 1 ;int tryNum = 1 ; dodo {{
cout << "Please enter your guess by pressing a character key from cout << "Please enter your guess by pressing a character key from a to z “ ;a to z “ ; cin >> c ;cin >> c ; if ( c == 'z‘ )if ( c == 'z‘ )
{{ cout << "Congratulations! you guessed the right answer“ ;cout << "Congratulations! you guessed the right answer“ ; tryNum = 6 ;tryNum = 6 ;
}} elseelse
tryNum = tryNum + 1 ;tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ;} while ( tryNum <= 5 ) ;
Flow chart for do-while Flow chart for do-while looploop
Do-while
condition
Process
Exit
true
false
Relational Relational OperatorsOperators
char c ;char c ; int tryNum , maxTries ;int tryNum , maxTries ; tryNum = 1 ;tryNum = 1 ; maxTries = 5 ;maxTries = 5 ; cout << "Guess the alphabet between a to z “ ;cout << "Guess the alphabet between a to z “ ; cin >> c ;cin >> c ; while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) )while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) {{ cout << "Guess the alphabet cout << "Guess the alphabet
between a to z “ ;between a to z “ ; cin >> c ;cin >> c ; tryNum = tryNum + 1 ;tryNum = tryNum + 1 ; }}
for Loopfor Loop
For loopFor loop
forfor ( ( initialization condition initialization condition ; ; termination condition termination condition ; ; increment conditionincrement condition ) ) { {
statement ( s ) ;statement ( s ) ;}}
ExampleExampleint counter ;int counter ;
for( counter = 0 ; counter < 10 ; counter = counter + 1 )for( counter = 0 ; counter < 10 ; counter = counter + 1 )
cout << counter;cout << counter;
OutputOutput
01234567890123456789
Table for 2Table for 22 x 1 = 22 x 1 = 2
2 x 2 = 42 x 2 = 4
2 x 3 = 62 x 3 = 6
::
::
2 x 10 = 202 x 10 = 20
Example - Calculate Table Example - Calculate Table for 2for 2
#include <iostream.h>#include <iostream.h>
main ( )main ( )
{{
int counter ;int counter ;
for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )for ( counter = 1 ; counter <= 10 ; counter = counter + 1 )
{{
cout << "2 x " << counter << " = " << 2* counter << "\n“ ;cout << "2 x " << counter << " = " << 2* counter << "\n“ ;
}}
}}
OutputOutput2 x1 = 22 x1 = 2
2 x 2 = 42 x 2 = 4
2 x 3 = 62 x 3 = 6
::
::
2 x 10 = 202 x 10 = 20
Flow chart for the ‘Table’ Flow chart for the ‘Table’ example example
counter=1
While
Print 2*counter
counter <=10?
Stop
Start
No Exit
Counter = counter + 1
yes
Example: Calculate Table- Example: Calculate Table- EnhancedEnhanced
#include <iostream.h>#include <iostream.h>main ( )main ( ){{ int number ;int number ; int maxMultiplier ;int maxMultiplier ; int counter ;int counter ; maxMultiplier = 10 ;maxMultiplier = 10 ; cout << " Please enter the number for which you wish to construct the table “ cout << " Please enter the number for which you wish to construct the table “
;; cin >> number ;cin >> number ; for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 )for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) {{ cout << number <<" x " << counter<< " = " << number * counter cout << number <<" x " << counter<< " = " << number * counter
<< "\n“ ;<< "\n“ ; }}}}
Always think re-useAlways think re-use Don’t use explicit Don’t use explicit
constantsconstants
Increment Increment operatoroperator
++++
counter ++ ; counter ++ ;
same as same as counter = counter + 1;counter = counter + 1;
Decrement Decrement operatoroperator
----
counter -- ;counter -- ;
same assame as counter = counter - 1counter = counter - 1
+=+= counter += 3 ; counter += 3 ;
same assame as counter = counter + 3 ;counter = counter + 3 ;
-=-= counter -= 5 ; counter -= 5 ;
same assame as counter = counter – 5 ;counter = counter – 5 ;
*=*=x*=2x*=2
x = x * 2x = x * 2
/=/=
x /= 2x /= 2
x = x / 2 x = x / 2
Compound Assignment Compound Assignment OperatorsOperators
operatoroperator==
%=%= x %= 2 ; x %= 2 ;
same as same as x = x % 2 ;x = x % 2 ;
CommentsComments Write comment at the top Write comment at the top
program to show what it doesprogram to show what it does Write comments that mean Write comments that mean
some thingsome thing
In today’s lectureIn today’s lecture Do - whileDo - while
– Executes the code at least onesExecutes the code at least ones For loopFor loop
– Executes at least zero timesExecutes at least zero times Short hand operatorsShort hand operators
– IncrementingIncrementing– DecrementingDecrementing
Compound assignment operatorCompound assignment operator