• Register
1 vote
579 views

Problem :

I am very new to programming world. According to my senior a for loop will always get executed at least once even if a required condition is not met.

Please find below example for your reference:

for (int i=6; i <=5; i++) {
    //inappropriate for this question
}

According to my senior above listed loop will be executed at least once but it does not or let me know if I am I missing something?

8 5 2
3,230 points

Please log in or register to answer this question.

2 Answers

0 votes

Solution :

I can help you in understanding this concept. I can say that the for loop is always evaluated at least once.
But if a for loops condition is not met because of some reason then its block will never be executed. Any loop will only get executed if its condition is true. As far as the for loop and the while loop are considered then I can certainly say that both will check a condition before a body is executed and they will never get executed if a condition is false.

The only loop that will always get executed is the do while loop. As far as the do while loop is considered then the condition is not evaluated until you reach the end of a loop. Because of this nature of it a do while loop will always get executed at least once.

Hope it helps.

 

 

8 4 1
5,680 points
0 votes

The do-while loop is similar to a while loop. The only difference is that the condition occurs at the end of the loop. Having the loop condition at the end of the loop gives the guarantees that the body of the loop always executes at least once.

Test condition:

The test condition must be enclosed in parentheses and followed by a semicolon. Semicolons are also placed at the end of each statement within the blocks. The body of the loop is enclosed in curly braces to indicate the body of the loop. This method also helps to increase the readability of code.

Exit condition loop:

The do-while loop is an exit-condition loop that means the body of the loop will always be executed first. After that, the test condition is evaluated. If the test condition is true the body of the loop is executed. If the loop condition is false, the loop terminates and the program execution continues with the statement following while.

Example:

char ans;

do

{

      cout<< "Do you want to continue (Y/N)?\n";

      cout<< "You must type a 'Y' or an 'N'.\n";

      cin >> ans;

}

while((ans !='Y')&&(ans !='N')&&(ans !='y')&&(ans !='n'));

 

11 5 2
3,890 points

Related questions

0 votes
1 answer 3 views
3 views
Problem: According to my teacher, a for-loop always executes at least once, even if the condition is not met. Example (like I know it from C++): for (int i=6; i <=5; i++) { //irrelevant for this question } According to her, this loop would execute at ... , no matter what language, where this would execute once? To eliminate the thought in advance: yes, it was about for loops, not do-while-loops.
asked 4 days ago anika11 32k points
0 votes
1 answer 5 views
5 views
Problem: According to my teacher, a for-loop always executes at least once, even if the condition is not met. Example (like I know it from C++): for (int i=6; i <=5; i++) { //irrelevant for this question } According to her, this loop would execute at least ... about for loops, not do-while-loops. Edit: Thanks for all those quick answers, I guess this case is already closed. Have a nice day/night.
asked 5 days ago ochena 22.1k points
0 votes
1 answer 6 views
6 views
Problem: I am stuck with this ... Please help me that how to deal with this error? which loop structure always executes at least once?
asked Apr 2 Ifra 34.7k points
0 votes
1 answer 3 views
3 views
Problem: Is a For Loop always executed at least once?
asked 1 day ago Supti 12.7k points
0 votes
1 answer 4 views
4 views
Problem: According to my teacher, a for-loop always executes at least once, even if the condition is not met. Example (like I know it from C++): for (int i=6; i <=5; i++) { //irrelevant for this question } According to her, this loop would execute at ... , no matter what language, where this would execute once? To eliminate the thought in advance: yes, it was about for loops, not do-while-loops.
asked Apr 26 sumaiya simi 43.9k points
0 votes
1 answer 73 views
73 views
Problem: My programming teacher gave us an assignment in which we have to tell which of the three loops in C++ does not have an entry condition? As far as my guess is concerned, the 'for' loop does not need an entry condition. I want you to guide me if I am wrong and tell me the correct answer to this question. Thanks!
asked Dec 17, 2020 Code Learner 9.8k points
0 votes
1 answer 121 views
121 views
Problem: Can I use the same counter variable for a for loop inside of a for loop? Or will the variables affect each other? Should the following code use a different variable for the second loop, such as j, or is i fine? for(int i = 0; i < 10; i++) { for(int i = 0; i < 10; i++) { } }
asked Apr 24 Humaira ahmed 50.7k points
0 votes
1 answer 271 views
271 views
Problem : Looking for solution of following doubt if one for loop is nested within another, then the counter variables for the two loops should be
asked Nov 12, 2019 peterlaw 6.9k points
0 votes
1 answer 8 views
8 views
Problem: Anyone here who know nested loops to print a rectangle. sample output for given program? If yes, then please answer me.
asked Mar 27 niyatiry 3.9k points
0 votes
1 answer 8 views
8 views
Problem: Can anyone help me about this? If anyone know about loop to print each contact and contact email. Then please read my question carefully and answer me.
asked Mar 26 niyatiry 3.9k points