Loops - PowerPoint PPT Presentation

About This Presentation
Title:

Loops

Description:

... and Decrement. C has special operators for incrementing or decrementing an object ... Increment and Decrement. What is the difference between k and k? ... – PowerPoint PPT presentation

Number of Views:14
Avg rating:3.0/5.0
Slides: 11
Provided by: andrew184
Category:
Tags: decrement | loops

less

Transcript and Presenter's Notes

Title: Loops


1
Programming
  • Loops

2
Shortcut Assignment
  • C has a set of operators for applying an
    operation to a variable and then storing the
    result back into the variable
  • Shortcut assignments , /, , -,
  • Examples
  • int i 3
  • i 4 // i i 4
  • cout ltlt i ltlt endl // i is now 7
  • double a 3.2
  • a 2.0 // a a 2.0
  • cout ltlt a ltlt endl // a is now 6.4
  • int change 1265
  • change 100 // change change 100
  • cout ltlt change ltlt endl // change is now 65

3
Increment and Decrement
  • C has special operators for incrementing or
    decrementing an object by one
  • Examples
  • int k 4
  • k // kk1 k is 5
  • k // kk1 k is 6
  • cout ltlt k ltlt endl
  • int i k // i is 6, k is 7
  • cout ltlt i ltlt " " ltlt k ltlt endl
  • int j k // j is 8, k is 8
  • cout ltlt j ltlt " " ltlt k ltlt endl

4
Increment and Decrement
  • What is the difference between k and k?
  • k increments first, and the incremented value
    is
  • used in the expression
  • k uses the initial value of k in the
    expression,
  • and increments afterwards
  • Examples
  • int a, b, c, d, k
  • k 3
  • a k // k4, a4
  • b --a // a3, b3
  • c b // c3, b4
  • d c-- // d3, c2

5
Iterative Constructs
  • Provide
  • Ability to control how many times a statement
    list is executed
  • Three constructs
  • while statement
  • for statement
  • do-while statement

6
The while Statement
  • Syntax
  • while (Expression)
  • Action
  • How it works
  • If Expression is true then execute Action
  • Repeat this process until Expression evaluates to
    false
  • Action is either a single statement or a group of
    statements within braces

Expression
true
false
Action
7
N! (while)
  • int number, factorial, n
  • cout ltlt "Enter number "
  • cin gtgt number
  • factorial 1
  • n 1
  • while(n lt number)
  • factorial n
  • n
  • cout ltlt "The factorial of " ltlt number
  • ltlt " is " ltlt factorial ltlt endl

8
2N (while)
  • int number, result, n
  • cout ltlt "Enter number "
  • cin gtgt number
  • result 1
  • n 1
  • while(n lt number)
  • result 2
  • n
  • cout ltlt "Two raised to the " ltlt number
  • ltlt " power is " ltlt result ltlt endl

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

10
  • include ltiostreamgt
  • using namespace std
  • int main()
  • int listSize 0
  • int value
  • double sum 0
  • double average
  • cout ltlt "Provide a list of numbers (CTRL-D to
    stop) " ltlt endl
  • while (cin gtgt value)
  • sum value
  • listSize
  • if(listSize gt 0)
  • average sum / listSize
  • cout ltlt "Average " ltlt average ltlt endl
  • else
  • cout ltlt "No list to average" ltlt endl

The value of the input operation corresponds to
true only if a successful extraction was made
Write a Comment
User Comments (0)
About PowerShow.com