Iteration (loops)

THE LOOP-WHILE (PRE-TEST) IN C++: THE WHILE STATEMENT

-remember it's best for loops executing 0 or more (unknown in advance) iterations

-C++ syntax:

```	while (condition)
statement
```

-while is keyword

-condition must be in ( )'s

-statement is singular - must be compound (insde { }'s) if there are multiple statements

EXAMPLES:

```x = 1;
while (x <= 3)
{
cout << x << endl;
x++;			<==don't forget!
}

x = 1;
while (x <=3)
{
x++;
cout << x << endl;
}

x = 1;
while (x < 3)
{
cout << x << endl;
x++;
}

x = 1;
while (x < 0)
{
cout << x << endl;
x++;
}
```

watch out:

```x = 1;
while (x <=3)
cout << x << endl;
x++;

x = 1;
while (x <=3)
{
cout << x << endl;
x--;
}

x = 1;
while (x <= 3);		<===infinite!!!
{
<some statments>
}
```

EXERCISES:

-print numbers from 1 to 100

-print even #s from 1 to 100

-print squares of odd numbers from 1 to 100

-read in numbers from user and add them up until sum is > 100. Report how many #'s user entered

-read in exactly 99 numbers from user, and report average

-Sentinel-controlled while loop:

-Remember the structure:

```	READ 1st data value
LOOP WHILE value != sentinel
process value
ENDLOOP
```

//read in and average series of test scores:

```sum = count = 0;
cout << "Enter 1st score, negative to stop.\n";
cin >> score;
while (score >= 0)
{
sum += score;
count++;
cout << "Enter next score, negative to stop.\n");
cin >> score;
}
if (count)
cout << sum/count << endl;
else
cout << "No scores entered.  Average not computed.\n"

```

- But sometimes, input data has no valid sentinel, such as temperatures, which can be positive, negative or 0

-In these cases, we can use the END-OF-FILE CONTROLLED WHILE LOOP, which uses a special character, control-z, as the loop terminator value.

-This is the ASCII character used to mark the end of Windows files on a disk, and C++ is set up to look for it using the cin object's member function eof( )

- The eof( ) function is true if the last value read in is the ASCII value of control-z, otherwise it's false

- Basic structure of loop is like a sentinel-controlled loop, with prime read, etc.

-EXAMPLE:

//read in and average series of temperaturs:

```sum = count = 0;
cout << "Enter 1st temperature, control-z to stop.\n";
cin >> temp;
while (!cin.eof())
{
sum += temp;
count++;
cout << "Enter next temperature, control-z to stop.\n");
cin >> temp;
}
if (count)
cout << sum/count << endl;
else
cout << "No temperatures entered.  Average not computed.\n"

```

THE LOOP-WHILE (POST-TEST) IN C++: THE DO-WHILE STATEMENT

-remember it's best for loops executing 1 or more (unknown in advance) iterations

-C++ syntax:

```	do
{
statement1;
statement2;
...
statementN;
}
while (condition );
```

-do is keyword

-must be { }'s surrounding body, even if only one statement inside

-while is keyword

-condition must be in ( )'s

EXAMPLES:

```x = 1;
do
{
cout << x << endl;
x++;
}
```

while (x <=3);

```x = 1;
do
{
cout << x << endl;
x++;
}
while (x < 0);
```

(contrast this with pre-test while using same condition)

EXERCISES:

(same as while)

-do-while is often used as a QUERY-CONTROLLED loop:

(ie "do you want to go again" -type loops)

-structure:

```do
{
prompt for data value
process data value
query user if they want to go again/have more data
}
while (Response != 'N') && (Response != 'n');
```

-This is often used as body of main function, to contol repeated executions of program

-Helpful to use a function to query/read response as condition for termination:

```int MoreData(void)
{

cout << "Do you have more data to process (y or n)? ";

}
```

-This function makes structure less cluttered:

```do
{
prompt for data value
process data value
}
while (MoreData());		//this is the call to query function

```

IMPLEMENTING FOR LOOP IN C++:

-used for known number of iterations

-syntax:

```for ( InitialValueExpression; Condition; StepValueExpression)
statement
```

-for is a keyword

-InitialValueExpression sets the starting value for the LOOP CONTROL VARIABLE

-Condition is the stopping limit for loop control variable

-StepValueExpression is the increment value for LCV (count by 1's, 2's, etc)

-statement is singular! - if >1 statement in body, make it compound by { }'s

-note ;'s separating items in ( )'s list

-How It Works:

1. InitialValueExpression is evaluated, assigning LCV its initial value

2. Condition is evaluated

3. If Condition is true,

a. Statement is executed

b. StepValueExpression is executed

else (condition is false)

-program execution continues at first statement following end of for statement

-Remember: changing LCV value inside loop body will alter number of iterations, and defeats purpose of for loop! NEVER, EVER do this!! If it may be necessary to "jump out" of a loop early, use a while, not a for!

EXAMPLES:

```for (x=1; x <=3; x++)
cout << x << endl;

for (x=1; x<3; x++)
cout << x << endl;

for (x=10; x >=1; x--)
cout << x << endl;

for (x=1; x<=100; x+=2)
cout << x << endl;

for (x=1; x<0; x++)
cout << x <<endl;

for (x=1; x<=100; x++)
cout << x*x << endl;

for (x=0.0; x <=1.0; x+=0.1)
cout << x << endl;
```

//the summing algorithm:

```sum = 0;
for (x=1; x<=N; x++);
sum+= x;
cout << sum;
```

NESTED LOOPS!!