According to mine teacher, a for-loop constantly executes at least once, also if the condition is no met.

You are watching: Which loop structure always executes at least once?

Example (like I recognize it from C++):

for (int i=6; i According come her, this loop would execute at least once, yet it go not, or to be I missing something? Is there any type of case, no matter what language, whereby this would certainly execute once? To eliminate the assumed in advance: yes, the was around for loops, not do-while-loops.

Edit:

Thanks for every those fast answers, ns guess this situation is currently closed. Have actually a nice day/night.


c++ loops for-loop language-agnostic
share
enhance this inquiry
follow
edited Jul 5 \"17 in ~ 17:44
Klaus9090
request Jul 5 \"17 in ~ 17:16
\"*\"

Klaus9090Klaus9090
13311 gold badge11 silver- badge55 bronze title
17
| present 12 an ext comments

4 answer 4


active oldest Votes
19
You might say a for-loop is always evaluated at least once.

But if a for-loop\"s condition is no met, its block will never ever execute.

Because friend didn\"t ask about other loops, i won\"t resolve those.


re-publishing
boost this answer
monitor
edited Feb 23 \"19 at 21:20
answered Jul 5 \"17 in ~ 17:21
\"*\"

NonCreature0714NonCreature0714
4,62077 gold badges2828 silver- badges5050 bronze badges
1
add a comment |
6
A loop will only execute if its problem is true. Due to the fact that a because that loop and a when loop both inspect the condition before the body is executed they will never execute if the condition is false.

The only loop that will certainly is a execute while loop. With a do while loop the problem is no evaluated till the end of the loop. Since of that a do while loop will always execute at the very least once.


re-publishing
enhance this answer
follow
edited Feb 23 \"19 at 17:16
\"*\"

Stephen Kennedy
17.6k2121 yellow badges8686 silver- badges104104 bronze badges
answered Jul 5 \"17 in ~ 17:22
\"*\"

NathanOliverNathanOliver
155k2727 gold badges243243 silver badges342342 bronze title
add a comment |
1
A for-loop always makes sure the problem is true prior to running the program. Whereas, a do-loop runs the program at least once and also then checks the condition.


re-superstructure
enhance this prize
follow
reply Jul 6 \"17 in ~ 0:09
\"*\"

EeshaEesha
1933 bronze badges
include a comment |
0
An entry regulated loop will never ever execute if the condition is false, however, exit controlled loop will certainly execute at the very least once.


re-superstructure
improve this price
monitor
reply Jul 5 \"17 in ~ 17:25
Rajeev SinghRajeev singh
3,23222 yellow badges1616 silver badges2828 bronze title
include a comment |

your Answer


Thanks because that contributing an answer to ridge Overflow!

Please be certain to answer the question. Provide details and also share her research!

But avoid

Asking because that help, clarification, or responding to other answers.Making statements based on opinion; earlier them up with referrals or an individual experience.

To discover more, see our advice on writing an excellent answers.

See more: How Much Does A Monster Truck Cost To Build A Monster Truck, 10 Fast Facts To Rev You Up For Monster Jam


Draft saved
Draft discarded

Sign up or log in in


sign up using Google
authorize up utilizing Facebook
authorize up utilizing Email and Password
send

Post together a guest


surname
email Required, but never shown


Post together a guest


name
email

Required, yet never shown


write-up Your prize Discard

By clicking “Post your Answer”, girlfriend agree come our regards to service, privacy policy and also cookie policy


Not the answer you're looking for? Browse various other questions tagged c++ loops for-loop language-agnostic or ask your very own question.


The Overflow Blog
Upcoming occasions
Featured top top Meta
Linked
1
Can I find in an north array?
connected
1963
Why is using \"for...in\" for array iteration a bad idea?
4345
Accessing the table of contents in 'for' loops?
3257
how do i loop with or enumerate a JavaScript object?
1711
just how to loop v a level JavaScript object through the objects together members
905
A 'for' loop to iterate end an enum in Java
3562
Loop through range in JavaScript
5155
For-each over range in JavaScript
2155
how does PHP 'foreach' in reality work?
1771
picture Processing: Algorithm advancement for 'Coca-Cola Can' recognition
1527
replacing a 32-bit loop respond to with 64-bit introduce crazy performance deviations through _mm_popcnt_u64 ~ above Intel CPUs
hot Network questions an ext hot questions

concern feed
i ordered it to RSS
concern feed To subscribe to this RSS feed, copy and also paste this URL into your RSS reader.


lang-cpp
ridge Overflow
assets
company
ridge Exchange Network
site architecture / logo © 2021 ridge Exchange Inc; user contributions licensed under cc by-sa. Rev2021.10.12.40432


Stack Overflow works best with JavaScript permitted
\"*\"

your privacy

By clicking “Accept all cookies”, you agree stack Exchange have the right to store cookie on your maker and disclose details in accordance v our Cookie Policy.