0
i am finding very hard to understand loop...
can someone plz describe hw this loop thing works? or can someone help me to understand the loop thing? Thanks for ur modest co-operation...☺
5 Réponses
+ 4
//top of my loop
10 push-ups
10 sit-ups
10 jumping jacks
//bottom of loop
we did 1 set of each exercise. if i want to do 3 sets of each then i make a counter. i start the counter to 0 and every time i get to the bottom of the loop i add 1 to loop.
loop = 0
//top of my loop
if loop == 3 stop exercising.
10 push-ups
10 sit-ups
10 jumping jacks
Loop ++
//bottom of loop
we went through the loop 3 times, that means 3 sets or 30 of each exercise.
there are different types of loops with different ways of counting. you can even put loops inside other loops. a while loop doesnt count loops; it checks a condition and keeps looping until the condition is FALSE or 0;
//top of my loop
while , Still have energy? TRUE, keep going.
10 push-ups
10 sit-ups
10 jumping jacks
FALSE? STOP
//bottom of loop
understand?
+ 3
Loops:
- These are iterative constructs
- They help in managing iterative tasks.
- They run a block of code for either a fixed no. of times or till exit condition is met.
- Loops can be classified into two categories
a) Counter Loops b) Dynamic Loops
> Counter Loops: The block of code is executed for a fixed and finite no of times, usually no. of iterations are known in advance. The counter loop consists of
-> Start Counter
-> End Counter
-> Change
-> Loop body
SYN:
for(start; exit-condition; change){
//loop content
}
- Counter should be an integer variable.
- Counter should have a finite end value
- Proper change should be applied to the counter so that end value could
be achieved
for(int i=0;i<10;i++){
cout<<i;
}
- i variable is a counter of type int with start value of 0
- loop will terminate on reaching at value 10
- i++ increases the value by 1
>Dynamic Loops: The block of code is executed till loop condition is true
The loop consists of
-> Loop Condition
-> Loop termination logic
-> Loop body
There are two types of dynamic loops
1. while loop: First it checks for the loop condition and if it true,
loop body is executed, otherwise it does not executes the code.
SYN
while(loop-condition){
//loop contents
}
int i=0;
while (i<10){ //Loop body will execute
cout<<i;
i++;
}
int i=10;
while (i<10){ //loop body will not execute
cout<<i;
i++;
}
out of the loop
2. do-while loop: The loop body is executed and loop condition is verified
afterwards. That is the loop body will be executed at least once before
SYN
do{
//loop contents
}while(loop-condition);
int i=10;
do{
cout<<i;
i++;
}while(i<10);
+ 2
while loop:
As long as the condition is true the while loop will run the code over and over again.
while(<condition>){
<code>
}
Example:
int i = 1;
while(i <= 5){
cout << i << " ";
i = i + 1;
}
As i is 1 when we reach the while loop (i <= 5) will be true, so the code inside the loop will run. Every time the code inside the loop runs it will cout i and then increase i by 1. Once i is 6 (i <= 5) will be false and the loop will stop. The output will be: "1 2 3 4 5"
+ 1
loop is like a common day.. you do your stuff every day like brushing teeth or eating breakfast then... so loop is repeating some statement in basic view until some condition is false.. for ex. you ll eat and eat until you are full...
0
thanku