C++ course(fifth section 1)
Fifth section(counter control structures 1)
Why do we use repetition ?
while(expression)
statement1;
statement2;
while is a reserved word
Statement1 can be simple or compound
The expression acts as a decision maker and is usually a logical expression
Statement1 is called the body of the loop
The parentheses are part of the syntax
Statement2 is executed in any case.
while (x <= 5)
{ cout<<x<<“\t”; x++;}
using namespace std;
int main()
{
Sentinel-Controlled while Loops:
Fifth section(counter control structures 1)
Why do we use repetition ?
Repetition allows you to efficiently use variables
Can input, add, and average multiple numbers using a limited number of variables
For example, you can add five numbers together by:
declaring a variable for each number, inputting the numbers and adding the variables together
creating a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers and looping until all numbers are read.
Can input, add, and average multiple numbers using a limited number of variables
For example, you can add five numbers together by:
declaring a variable for each number, inputting the numbers and adding the variables together
creating a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers and looping until all numbers are read.
1)The (while) loop:
The general form of the while statement is: while(expression)
statement1;
statement2;
while is a reserved word
Statement1 can be simple or compound
The expression acts as a decision maker and is usually a logical expression
Statement1 is called the body of the loop
The parentheses are part of the syntax
Statement2 is executed in any case.
The expression provides an entry condition
The statement executes if the expression initially evaluates to true
The loop condition is then reevaluated
If it is still true, the statement executes again
The statement continues to execute until the expression is no longer true
An infinite loop continues to execute endlessly and can be avoided by making sure that the loop’s body contains statement(s) that assure that the exit condition will eventually be false
Examples:
int x=1; The statement executes if the expression initially evaluates to true
The loop condition is then reevaluated
If it is still true, the statement executes again
The statement continues to execute until the expression is no longer true
An infinite loop continues to execute endlessly and can be avoided by making sure that the loop’s body contains statement(s) that assure that the exit condition will eventually be false
Examples:
while (x <= 5)
{ cout<<x<<“\t”; x++;}
The output is: 1 2 3 4 5
- Programming example:
using namespace std;
int main()
{
int number; // variable to store the number
int sum = 0; // variable to store the sum
int count = 1; // variable to store the total number read
while(count <= 5)
{
int sum = 0; // variable to store the sum
int count = 1; // variable to store the total number read
while(count <= 5)
{
cout<<"enter integer: ";
cin>>number; sum = sum + number;
cin>>number; sum = sum + number;
count++;
}
count--;
cout<<"Line 7: The sum of "<<count<<" numbers is "<<sum<<endl;
}
count--;
cout<<"Line 7: The sum of "<<count<<" numbers is "<<sum<<endl;
double average= static_cast<double> (sum) / count;
cout<<"average = "<<average<<endl;
return 0;
}
cout<<"average = "<<average<<endl;
return 0;
}
*Note: this statement (static_cast<double> ) converts integer values to the identifier betwen < >.
i.e we have float variable and we want to convert it to int we type
static_cast <int> (variable name)
The output of the past example is:
enter integer: 4
enter integer: 3
enter integer: 5
enter integer: 7
enter integer: 8
Line 7: The sum of 5 numbers is 27
average = 5.4
A sentinel variable is tested in the condition and the loop ends when the sentinel value is encountered
The syntax is:
cin>>variable;
while(variable != sentinel)
{
.
cin>> variable;
.
}
Flag-Controlled while LoopsThe syntax is:
cin>>variable;
while(variable != sentinel)
{
.
cin>> variable;
.
}
A flag-controlled while loop uses a Boolean variable to control the loop.
It is suitable for searching Arrays.
The flag-controlled while loop takes the form:
found = false;
while(!found)
{
.
if(expression)
found = true;
.
}
It is suitable for searching Arrays.
The flag-controlled while loop takes the form:
found = false;
while(!found)
{
.
if(expression)
found = true;
.
}
0 comments:
Post a Comment