Saturday, 12 October 2013

C++ course (fifth section 1)

C++ course(fifth section 1)

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.

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; 
while (x <= 5)
{ cout<<x<<“\t”; x++;}

The output is:   1      2    3    4     5
  • Programming example:
#include <iostream>
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)
 cout<<"enter integer: ";
cin>>number; sum = sum + number;
 count++;
}
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;
}
*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

Sentinel-Controlled while Loops:
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 Loops
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;
.
}

Unknown

Author & Editor

Has laoreet percipitur ad. Vide interesset in mei, no his legimus verterem. Et nostrum imperdiet appellantur usu, mnesarchum referrentur id vim.

0 comments:

Post a Comment

 
biz.