programming n.
Download
Skip this Video
Download Presentation
Programming

Loading in 2 Seconds...

play fullscreen
1 / 13

Programming - PowerPoint PPT Presentation


  • 76 Views
  • Uploaded on

Programming. for Loops. The for Statement. initialization. condition. false. true. action. update. The for Statement. Syntax for ( initialization ; condition ; update ) action How it works: execute initialization statement while condition is true execute action

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Programming' - forrest


Download Now An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
programming

Programming

for Loops

the for statement
The for Statement

initialization

condition

false

true

action

update

the for statement1
The for Statement
  • Syntax

for (initialization; condition; update)

action

  • How it works:
    • execute initialization statement
    • while condition is true
      • execute action
      • execute update
  • Example:

int i;

for(i=1; i<=20; i++) cout << "i is " << i << endl;

slide4
N!

int number, factorial, n;

cout << "Enter positive integer:"; cin >> number;

factorial = 1;

for(n=1; n<=number; n++)

factorial *= n;

cout << "The factorial of " << number

<< " is " << factorial << endl;

slide5
2N

int number, result, n;

cout << "Enter positive integer:"; cin >> number;

result = 1;

for(n=1; n<=number; n++)

result *= 2;

cout << "Two raised to the "

<< number << " power is "

<< result << endl;

slide6
int main() {int listSize, n;int value;double sum = 0;double average; cout << "How many input numbers? " << endl;

cin >> listSize; cout << " Please enter the " << listSize

<< " input numbers: " << endl;

for (n=listSize; n > 0; n--) {

cin >> value; sum += value; }

if (listSize >0) {

average = sum / listSize;

cout << "Average: " << average << endl;

}return 0;}

loops iteration
Loops (Iteration)
  • Make sure there is a statement that will eventually stop the loop.
    • INFINITE LOOP == loop that never stops
  • Make sure to initialize loop counters correctly.
    • OFF-BY-ONE == the number of times that a loop is executed is one too many or one too few.
  • Have a clear purpose for the loop.
increment and decrement
Increment and Decrement
  • C++ has special operators for incrementing (++) and decrementing (--) an integer by one.
  • The ++ operator functions as follows:
    • ++k: increments the value of k by one and the incremented value is used in the expression.
    • Example:

int k = 1, j;

j =++k;//First: k increased by 1, becomes 2,

// Second: j is assigned to 2k = 3;cout << ++k;

// First: k is increased by 1, becomes 4,

// Second: 4 outputs to the screen

increment and decrement1
Increment and Decrement

k++ uses the initial value of k in the expression and increments afterwards.

Example:

int k = 1, j;

j = k++; // First: j is assigned to 1

//Second: k is increased by 1, becomes 2

k = 3;

cout << k++;

//First:3 printed on the screen

// Second:k is increased by 1, becomes 4,

increment and decrement2
Increment and Decrement

The -- operator functions as follows:

  • --k decrements the value of k by one and the decremented value is used in the expression.

Example:

int k = 1, j;

j =--k; // First: k decreased by 1, become 0,

// Second: j is assigned to 0

k = 3; cout << --k;

// First: k is decreased by 1, becomes 2,

// Second: 2 is output to the screen

increment and decrement3
Increment and Decrement

k-- uses the initial value of k in the expression and decrements afterwards.

Example:

int k = 1, j;

j = k--; // first: j is assigned to 1

// second: k decreased by 1, becomes 0,

k = 3;

cout << k--;

//First: 3 output to the screen

//Second: k is decreased by 1, becomes 2

increment and decrement4
Increment and Decrement
  • Examples:

int k = 4;

cout << k << endl;cout << ++k << endl; // k=k+1 : k is 5

cout << k++ << endl; // k=k+1 : k is 6

int i = k++; // i is 6, k is 7cout << i << " " << k << endl;

int j = ++k; // j is 8, k is 8cout << j << " " << k << endl;

increment and decrement5
Increment and Decrement
  • Examples:

int k = 4;

cout << k << endl;cout << --k << endl; // k=k-1 : k is 3

cout << k-- << endl; // k=k-1 : k is 2

int i = k--; // i is 2, k is 1cout << i << " " << k << endl;

int j = --k; // j is 0, k is 0cout << j << " " << k << endl;