Introduction:-
Why Is Repetition Needed?
Repetition allows you to efficiently use variables. Can input, add, and average multiple numbers using a limited number of variables. For example, to add five numbers: Declare a variable for each number, input the numbers and add the variables together. Create a loop that reads a number into a variable and adds it to a variable that contains the sum of the numbers.
While Loop(Repetition) Structure:-The general form of the while statement is:
while is a reserved word.
Statement can be simple or compound.
Expression acts as a decision maker and is usually a logical expression.
While is a pre-test loop.
Statement is called the body of the loop.
The parentheses are part of the syntax .
Infinite loop: continues to execute endlessly. Avoided by including statements in loop body that assure exit condition is eventually false.
Case 1: Counter-Controlled while Loops
If you know exactly how many pieces of data need to be read, while loop becomes a counter-controlled loop with loop control variable (LCV)
#include<iostream.h>
int main()
{
int total, //sum of grades
gradeCounter, //number of grades entered
grade, //one grade
average; //average of grades
//initialization phase
total = 0; //clear total
gradeCounter = 1; //prepare to loop
//processing phase
while (gradeCounter <= 10) //loop 10 times
{
cout << "Enter grade: "; //prompt for input
cin >> grade; //input grade
total = total + grade; //add grade total
gradeCounter = gradeCounter +1; //increment counter
}
//termination phase
average = total/10; //integer division
cout << "Class average is " << average <<endl;
return 0; //indicate program ended successfully
}
Case 2: Sentinel-Controlled while LoopsSentinel variable is tested in the condition
Loop ends when sentinel is encountered
while(score!=-1)
{
cout<<"Good Bye "<<endl;
cin>>score;
}
Case 3: Flag-Controlled while loops A flag-controlled while loop uses a bool variable to control the loop.
The flag-controlled while loop takes the form:
boolean done=false;
while(!done)
{
//read some input
if(condition)
done=true;
else
{
//process input
}
}
do...while Looping (Repetition) Structure
General form of a do...while:
do
{
statements;
}
while(condition)
The statement executes first, and then the expression is evaluated.
To avoid an infinite loop, body must contain a statement that makes the expression false.
The statement can be simple or compound.
This loop is post-test loop.
Loop always iterates at least once.
For Any Problem Contact us on my mail apnapunjab7432@gmail.com For(Loops)
for Looping (Repetition) Structure: -
The general form of the for statement is:
for(initialization;test condition;increment)
{
//Body of loop
}
Example:-
for(int x=0;x<=10;x++)
{
cout<<x;
}
The initial statement,loop condition, and update statement are called for loop control statements.
Initial statement usually initializes a variable (called the for loop control, or indexed, variable)
In C++, for is a reserved word. If test is false the first time it is evaluated, the body of the loop will not be executed.The any update expression can increment or decrements by amount.Variables used in the initialization section should not be modified in the body of the loop.
int sum = 0, num;
for (num = 1; num <= 10; num++)
{
sum += num;
cout << "Sum of numbers 1 – 10 is ";
cout<< sum << endl;
}
For Loops related Problems..Click on below link
Click Here
int sum = 0, num;
for (num = 1; num <= 10; num++)
{
sum += num;
cout << "Sum of numbers 1 – 10 is ";
cout<< sum << endl;
}
For Loops related Problems..Click on below link
Click Here
No comments:
Post a Comment