Introduction to Programming: Do-while Loops and For Loops, Slides of Computer Programming

An introduction to do-while and for loops in programming. It includes the syntax, examples, and flowcharts for both types of loops. The document also covers relational operators and short hand operators for incrementing and decrementing variables, as well as compound assignment operators.

Typology: Slides

2011/2012

Uploaded on 11/06/2012

somo
somo 🇮🇳

4.8

(4)

70 documents

1 / 28

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture 7
Introduction to Programming
Docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c

Partial preview of the text

Download Introduction to Programming: Do-while Loops and For Loops and more Slides Computer Programming in PDF only on Docsity!

Lecture 7

Introduction to Programming

while loop

while (condition)

:^ statements;

statements; Docsity.com

do-while

Do while loop execute on

or more times

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“ ;
} else^ tryNum = 6 ;
} while ( tryNum <= 5 ) ;^ tryNum^ = tryNum + 1 ;

Flow chart for do-while loop

Do-while
condition
Process
true^ false^ Exit

for Loop

For loop

for { statem ent ( s ) ; ( initialization condition ; term ination condition ; increm ent condition )

Table for 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 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“ ;*

Flow chart for the ‘Table’ example

counter=1 While

Print 2*counter

counter <=10?

Stop
Start
No Exit

counter + 1^ Counter =

yes

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

Increment operator ++counter ++ ; same ascounter = counter + 1;

Decrement operator --

counter -- ; same ascounter = counter - 1