do-while Loops - PowerPoint PPT Presentation

1 / 6
About This Presentation
Title:

do-while Loops

Description:

do-while Loops The do-while Statement Syntax do action while (condition) How it works: execute action if condition is true then execute action again repeat this ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 7
Provided by: AndrewH164
Category:
Tags: loops

less

Transcript and Presenter's Notes

Title: do-while Loops


1
Programming
  • do-while Loops

2
The do-while Statement
  • Syntax
  • do action
  • while (condition)
  • How it works
  • execute action
  • if condition is true then execute action again
  • repeat this process until condition evaluates to
    false.
  • action is either a single statement or a group of
    statements within braces.

action
condition
true
false
3
N!
  • int number, factorial, counter
  • cout ltlt "Enter a positive integer" cin gtgt
    number
  • factorial 1
  • counter 1
  • do
  • factorial counter
  • counter while(counter lt number)
  • cout ltlt "The factorial of " ltlt number
  • ltlt " is " ltlt factorial ltlt endl

4
2N
  • int number, result, counter
  • cout ltlt "Enter a positive integer" cin gtgt
    number
  • result 1
  • counter 1
  • do
  • result 2
  • counter while (counter lt number)
  • cout ltlt "Two raised to the " ltlt number
  • ltlt " power is " ltlt result ltlt endl

5
Maximum
  • int value // input value
  • int max0 // maximum value
  • do
  • cout ltlt "Enter a positive number
  • ltlt "(-1 to stop)" cin gtgt
    value
  • if(value gt max)
  • max value
  • while(value!-1)
  • cout ltlt "The maximum value found is " ltlt max
    ltlt endl

6
Waiting for a Reply
  • char reply
  • do
  • // do something
  • cout ltlt "Continue(y/n) "
  • cin gtgt reply
  • while(reply!'n')
Write a Comment
User Comments (0)
About PowerShow.com