-18

Consider this Python for loop using a range:

for i in range(n-2, -1, -1):

As the loop iterates, i takes on the values n-2, n-3, until 0.

What is the equivalent code in C++?


This question is copied almost verbatim from another deleted question.

TheMaster
  • 37,620
  • 6
  • 43
  • 68
cigien
  • 55,661
  • 11
  • 60
  • 99
  • 6
    I can't see how this will be useful to other readers, its title is too generic to really show up in any searches and then a for loop itself is a pretty basic construct for c++ – Sayse Nov 24 '20 at 14:14
  • 1
    @Sayse Fair point. I edited the title to be more specific. Is that better? And questions asking about basic constructs are actually very useful to future readers. – cigien Nov 24 '20 at 14:15
  • 2
    I still don't see how this is any different to [What's the best way to do a backwards loop in C/C#/C++?](https://stackoverflow.com/a/276056/1324033) – Sayse Nov 24 '20 at 14:16
  • @Sayse That question constrains the problem to certain C-family languages. The only idioms that can be used to answer that are classic `for` and `while` loops. – cigien Nov 24 '20 at 14:20
  • @chepner Yes I do. Do you feel that the question needs a demonstration of the behavior? – cigien Nov 24 '20 at 14:30
  • 12
    Your question is too broad: do you need help identifying what the Python does? Do you not know how to write a C++ loop? Both questions on their own indicate a significant lack of research on your part, as they are both very basic questions in their respective languages. – chepner Nov 24 '20 at 14:31
  • 12
    @chepner I disagree that the question is too broad. I'm asking for a translation of a single line of python to C++, which is narrowly scoped. I think it's implied that I understand what the python loop does, but I'm happy to clarify that in the question. I completely agree that the question shows a lack of research effort. In that case, please down-vote the question. However, note that lack of research, or the fact that the question is basic, are not close reasons. – cigien Nov 24 '20 at 14:38
  • 10
    This question is being discussed in [the meta](https://meta.stackoverflow.com/questions/403116) – TheMaster Nov 24 '20 at 21:59
  • 2
    @chepner I disagree that this question indicates a lack of research. StackOverflow isn't just for professionals, and the very most useful questions are often from someone new to the area they're asking about. It's not helpful to say "Do you not know how to write a C++ loop?" when it's quite obvious that advice about loops and/or range constructs is exactly what the question is asking for. – Ken Williams Nov 25 '20 at 17:37
  • 1
    @KenWilliams It was *not* obvious at all, in the original question, if the OP knew what the Python code did and lacked the C++ knowledge to translate, or if they didn't know what the PYthon code did in the first place. – chepner Nov 25 '20 at 17:48
  • According to my search engine https://www.bing.com/search?q=c+for+loop+counting+down+to+zero (which I generally favor over Disney and Amazon for programming questions) the answer is https://stackoverflow.com/questions/804777/counting-down-in-for-loops (I understand that sample shows counting down from X-1 and not X-2 and uses different variable names... but I hope that should be ok). – Alexei Levenkov Nov 25 '20 at 21:23
  • 1
    @AlexeiLevenkov There is an answer on that question that could be used to answer this one. However, the question itself is different. It's asking about the performance impact of iterating in the forward/reverse direction when there is an array that needs be transformed. There is also the concern of the loop counter being `unsigned` which is not relevant here. While there are 2 answers to that question that "fix" the reverse iteration code the OP never even showed, the remaining 12 answers all address the performance aspect of the question. – cigien Nov 25 '20 at 23:00
  • This is currently copied almost verbatim from another question that *isn't* deleted... – jonrsharpe Nov 27 '20 at 13:41
  • 1
    @jonrsharpe Not anymore. At the time this was posted the other post was deleted voluntarily by its author. It was then forcefully un-deleted, and has now been re-deleted. – Scratte Nov 27 '20 at 16:24
  • @jonrsharpe and Scratte It is deleted. – TheMaster Nov 27 '20 at 17:41

2 Answers2

6

While the classic for(;;) loop can be used for any iteration that you can imagine, your particular loop can be written more readably like this:

namespace sv = std::views;

for (int i : sv::iota(0, n-1) | sv::reverse)
{
  // ...
}

Here's a demo.

For completeness, here's what a classic for(;;) loop would look like:

for (int i = n-2; i > -1; --i)
{
  // ...
}

In my opinion, this is more cognitive effort, precisely because I have to run the iteration in my head by reading the syntax. I find the first version reads more naturally.

cigien
  • 55,661
  • 11
  • 60
  • 99
  • 8
    For old dinosaurs once fed with C code, the second version looks much more simple, natural and readable. But it is just a matter of taste... – Serge Ballesta Nov 24 '20 at 14:16
  • @SergeBallesta Yes, I agree completely. For a python programmer not familiar with the classic version, this version is possibly easier to read. – cigien Nov 24 '20 at 14:17
5

The equivalent code in C++ :

for(int i = n-2 ; i > -1 ; i--) {
    ...
}
Vatsal kesarwani
  • 587
  • 6
  • 19