• Register
1 vote
236 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
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 24 views
24 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 6.3k points
0 votes
1 answer 176 views
176 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 12 views
12 views
In the first loop that runs with ++ of the function it returns 29 and the second that runs with + i returns 30, but both should return 29. What am I doing? #include <iostream> using namespace std; int sumElements (int a [], int size) {      int accum = 0;      int * p;      p = & a [0]; ...      } int main () {      int array [5] = {4,9,6,7,3};      cout << sumElements (array, 5);      return 0; }
asked Aug 14, 2020 game 4.6k points
0 votes
1 answer 44 views
44 views
Problem: I just started Bachelors in Software Engineering. Programming is the main subject of Software Engineering and I have no prior knowledge of programming. My programming teacher gave me an assignment in which I have to write a program in C++ whose output should be as ... a hint: Use nested loop. If someone knows how to do this kindly explain and provide me with the example program. Thanks.
asked Nov 29, 2020 Code Learner 6.3k points
0 votes
1 answer 28 views
28 views
Problem: Hi there! I am new to the world of programming and learning loop constructs in C#. My concept for loops is clear but I just recently encountered a code somewhere in which for loop was initialized as follows: for(;;) { } I tried running that code, but it ... I make some mistake but I want to know more about &lsquo;for&rsquo; loops and all the possible ways in which it can be used. Thanks!
asked Nov 27, 2020 Code Learner 6.3k points
0 votes
1 answer 1 view
1 view
Problem: How to solve : does break break out of all loops HELP! I need help understanding this : does break break out of all loops Can someone please help fix this, I am new in java. Anyone have ideas on what I need to do to understand this? Thanks!
asked 1 day ago Ali 5.7k points
1 vote
1 answer 63 views
63 views
Problem : I am very new to programming. I have recently started learning C programming. Please find below the one way of initializing the array: I have one doubt about my above program : Which of the following is not a correct way to initialize an array? int n[ 5 ] = { 0, 7, 0, 3, 8, 2 }; int m[3] = ... [1] 15};     printf("m[0] = %d  m[1] = %d   m[2] = %d\n", m[0], m[1], m[2]);     return 0; }
asked Apr 27, 2020 stewart 4k points
0 votes
1 answer 58 views
58 views
Problem: I am little bit confused and need help regarding my query? Which of the following expressions will determine whether x is less than or equal to y? A) x > y B) x =< y C) x <= y D) x >= y
asked Feb 22, 2020 maddi86 5.4k points
0 votes
1 answer 46 views
46 views
Problem: Can anybody help me solve this problem? Print numbers 0, 1, 2, ..., userNum as shown, with each number indented by that number of spaces. For each printed line, print the leading spaces, then the number, and then a newline. Hint: Use i and j as loop variables (initialize i and j explicitly). Note: Avoid any other spaces like spaces after the printed number. Ex: userNum = 3 prints:
asked Feb 18 Ali 5.7k points
0 votes
1 answer 6 views
6 views
Problem: Hello! I have got an exercise in which I have to arrange four integers in ascending order, and I can use only loops and conditional statements. I do not know how I can do this as I do not have a lot of hands-on experience. If someone could provide me with the sample program, I would be very thankful.
asked Nov 30, 2020 Code Learner 6.3k points