Can anyone help in loops basic description inc++? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Can anyone help in loops basic description inc++?

I'm new learner

7th Mar 2018, 1:34 PM
Faraz khan
Faraz khan - avatar
4 Answers
+ 6
while loop Syntax: while(loopCondition) { // action <- Can be placed here // instructions // action <- Can also be placed here } Where: <loopCondition> defines a condition whether or not the loop should continue, this condition is checked before the loop begins, that means, if you set a condition that evaluates to false, the loop may not even start. A while loop starts only if it finds the condition evaluates to true, and after executing the loop body and <action>, the loop will go back and check again, whether the condition still evaluates to true, if not, the loop must stop. Example: int a = 1; while(a <= 10) // <- loopCondition { std::cout << "value of a = " << a; a++; // <- action } <action> is one or more instructions you put inside the loop body that may change the loopCondition to evaluate to false, where the loop should stop. If you omit this <action> part, you risk turning the loop into an infinite loop, pay attention carefully, this part IS important. NOTE: The <action> can be placed before or after other instructions, depending on situation and requirements. Hth, cmiiw
10th Mar 2018, 9:53 AM
Ipang
+ 5
for loop: Syntax: for(initiations; loopCondition; actions) { // loop body } Where: <initiations> is where you declare initial values, this is executed before the loop body. You can initialize multiple values here, separate multiple value initializations with comma. NOTE: Variables you declare in <initiations> are block scope variables, meaning, they are only known inside the loop declaration and body. If you want the variable to be recognized and usable outside the loop block, you need to declare the variable outside, not as <initiations> part. Example: for(int a = 1, b = 2 ; a <= 5 ; a++, b = i * 2) { // codes here } - Here you initialize a to value 1, and b to value 2. <initiations> here is "a = 1, b = 2". <loopCondition> defines a condition whether or not the loop should continue, the loop must stop when this condition evaluates to false. Example: for(int a = 1 ; a <= 10 ; a++) { // codes here } - Here this loop continues while value of a is less than or equal to 10. <loopCondition> here is "a <= 10". <actions> are the instructions you put in that can change the <loopCondition> to evaluate to true, if you omit this part, you risk of creating an infinite loop (a loop that never stops). Example: for(int a = 1 ; a <= 10 ; a++) { // codes here } - Here, on each loop iteration, you increment value of a by one, so it eventually becomes greater than 10, when it happens, the loop will stop. actions here is "a++".
10th Mar 2018, 9:53 AM
Ipang
+ 3
Which loop are you referring to here? the for loop, while or do..while loop?
7th Mar 2018, 2:10 PM
Ipang
0
for ,while loop
8th Mar 2018, 7:30 PM
Faraz khan
Faraz khan - avatar