0
I need to clear my concepts in the following area with a well explained example please :)
While loop Do-while loop For loop Break and continue Where to use them? How to use them and how will the out out of each change?
7 Réponses
0
"While" loop:
1. Check the condition (actually can be anything, for example, just a number);
2. If it is true or evaluated as true (for non-Booleans), then execute some statements and go to point 1;
3. Otherwise go to the statement after that loop.
- It is better to use it when you just need to do something while something meets some criterion/criteria.
"Do … while" loop:
1. Do some commands;
2. Check the condition;
3. If it is true or evaluated to true, then go to point 1;
4. Otherwise go to the statement after that loop.
- It is better to use it when you need the same effect as "while" loop, but the code inside the loop must be executed at least one time.
"For" loop:
1. Execute statement 1;
2. Check statement 2;
3. If it is true or evaluated as true, then do something, do statement 3 and go to point 2;
4. Otherwise go to the statement after that loop.
[…]
0
[…]
- It is better to use it when you need something simple to do before the loop and do the same as "while" loop with simple statement executed at the end of the iteration (process of doing that something in the loop).
- It is easy to rewrite it into "while" loop: `for (statement 1; statement 2; statement 3) { something; }` (in some languages like C and C++ you can omit the brackets between the loop body (what I call "something" here) if something is one statement) = `statement 1; while (statement 2) { something; statement 3; }`.
"break" statement is used to break the loop and go to the next statement.
- It is better to use it if you want to stop doing something at the middle/start of it, when you should use some variables declared as it is needed only inside the loop body, if there the loop needs to be "broken" inside the complex code that to use it like this is better or the only way to do it.
"continue" statement is used to immediately go to statement checking.
[…]
0
[…]
- It is better to use it when you need to skip one iteration; it could be better and/or more readable than to put the loop body inside the "if" statement.
If you want someone to provide examples, specify the language, because the examples may vary.
Hope it helps you.
You can also read how it is explained in Wikipedia (with examples): https://en.wikipedia.org/wiki/While_loop , https://en.wikipedia.org/wiki/Do_while_loop , https://en.m.wikipedia.org/wiki/For_loop , https://en.m.wikipedia.org/wiki/Control_flow#Early_exit_from_loops (different types of loops, "break" and "continue" statements).
0
Riya Soni ,
1. If you meant if the "if" statement is put in "while"/"for" loop, then it will print either "two digit numbers" or "three digit numbers" for each iteration.
2. If you meant if to make an "if … else" statement a "while" or "for" loop, then:
• Note that loops do not support "else" part like "if" statement (do not be confused with Python's "else" part of loops that executes when no "break" statement was executed); though, there is an alternative: if the loop break, then the statement is false, so you can write the "else" part's body right after the loop: `while (a < 100) printf("two digit numbers"); printf("three digit numbers");` (if "a"'s value is less than 100, it will infinitely output "two digit numbers", otherwise it will output "three digit numbers" once);
[…]
0
Riya Soni, […]
• It is better to use "while" loop in this case as there is no simple statement before the loop; though, you can omit it like this: `for (; a < 100; ) printf("two digit numbers"); printf("three digit numbers");` or `for (; a < 100; printf("two digit numbers")); printf("three digit numbers");`.
If you meant something else, tell me.
- 1
I still need help with for loop. I’m kind of unclear and also break and continue
- 1
Ok this helped a lot but one more thing, say the condition statement is
if (a<100)
printf(“two digit numbers”)
Else
Printf(“three digit numbers”)
In while loop, if i put this it will check if the condition executes true, if it does then it will print 2 digit numbers and what if it executes false? And same with the for loop??