CS201- Introduction to Programming- Lecture 07

28
Lecture 7 Lecture 7 Introduction to Programmi Introduction to Programmi

description

Virtual University Course CS201- Introduction to Programming Lecture No 07 Instructor's Name: Dr. Naveed A. Malik Course Email: [email protected]

Transcript of CS201- Introduction to Programming- Lecture 07

Page 1: CS201- Introduction to Programming- Lecture 07

Lecture 7Lecture 7

Introduction to Introduction to ProgrammingProgramming

Page 2: CS201- Introduction to Programming- Lecture 07

while loopwhile loop

while (condition)while (condition)

{{

statements;statements;::

}}

statements;statements;

Page 3: CS201- Introduction to Programming- Lecture 07

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?

Page 4: CS201- Introduction to Programming- Lecture 07

do-whiledo-while

Page 5: CS201- Introduction to Programming- Lecture 07

Do while loop execute Do while loop execute onon

or more timesor more times

Page 6: CS201- Introduction to Programming- Lecture 07

Syntax of do-while Syntax of do-while looploop

dodo{{

statements ;statements ;

}}while ( condition ) ; while ( condition ) ;

Page 7: CS201- Introduction to Programming- Lecture 07

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 ) ;

Page 8: CS201- Introduction to Programming- Lecture 07

Flow chart for do-while Flow chart for do-while looploop

Do-while

condition

Process

Exit

true

false

Page 9: CS201- Introduction to Programming- Lecture 07

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 ; }}

Page 10: CS201- Introduction to Programming- Lecture 07

for Loopfor Loop

Page 11: CS201- Introduction to Programming- Lecture 07

For loopFor loop

forfor ( ( initialization condition initialization condition ; ; termination condition termination condition ; ; increment conditionincrement condition ) ) { {

statement ( s ) ;statement ( s ) ;}}

Page 12: CS201- Introduction to Programming- Lecture 07

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

Page 13: CS201- Introduction to Programming- Lecture 07

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

Page 14: CS201- Introduction to Programming- Lecture 07

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“ ;

}}

}}

Page 15: CS201- Introduction to Programming- Lecture 07

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

Page 16: CS201- Introduction to Programming- Lecture 07

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

Page 17: CS201- Introduction to Programming- Lecture 07

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“ ; }}}}

Page 18: CS201- Introduction to Programming- Lecture 07

Always think re-useAlways think re-use Don’t use explicit Don’t use explicit

constantsconstants

Page 19: CS201- Introduction to Programming- Lecture 07

Increment Increment operatoroperator

++++

counter ++ ; counter ++ ;

same as same as counter = counter + 1;counter = counter + 1;

Page 20: CS201- Introduction to Programming- Lecture 07

Decrement Decrement operatoroperator

----

counter -- ;counter -- ;

same assame as counter = counter - 1counter = counter - 1

Page 21: CS201- Introduction to Programming- Lecture 07

+=+= counter += 3 ; counter += 3 ;

same assame as counter = counter + 3 ;counter = counter + 3 ;

Page 22: CS201- Introduction to Programming- Lecture 07

-=-= counter -= 5 ; counter -= 5 ;

same assame as counter = counter – 5 ;counter = counter – 5 ;

Page 23: CS201- Introduction to Programming- Lecture 07

*=*=x*=2x*=2

x = x * 2x = x * 2

Page 24: CS201- Introduction to Programming- Lecture 07

/=/=

x /= 2x /= 2

x = x / 2 x = x / 2

Page 25: CS201- Introduction to Programming- Lecture 07

Compound Assignment Compound Assignment OperatorsOperators

operatoroperator==

Page 26: CS201- Introduction to Programming- Lecture 07

%=%= x %= 2 ; x %= 2 ;

same as same as x = x % 2 ;x = x % 2 ;

Page 27: CS201- Introduction to Programming- Lecture 07

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

Page 28: CS201- Introduction to Programming- Lecture 07

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