New to C++, I have a question about for loops that I need some help with. I understand the condition of using the for loop and how it increments/decreases but I'm wondering whether using a for loop using the i-- condition is good programing practice or just sloppy?
For example the for loop I'm using is i++ which means it loops the amount of entries I have in a dynamic array/struct which means it repeats asking what entry I want to delete according to how many entries I have inputted. The only way I've been able to get it to ask only once is by using i--. Is it OK to use i-- in a for loop to make the condition only run once or is this bad practice?
Decrementing instead of incrementing to make the index access consistent while erasing or moving data is fine.
Is it OK to use i-- in a for loop to make the condition only run once
Huh? If you're only running something once, why use a loop?
Anyway, the answer to your question is "it's OK". If it makes your code easier to understand, while still being correct, then I don't see a downside. If you gave more concrete examples, people could provide better feedback.
it sounds like you have a logic problem or a misunderstanding.
I can't think of any reason your code needs to run backwards to prevent some sort of user prompt, but if you can show it, maybe we can give you a ++ version or something.
from what you said...
1 2 3 4 5 6 7 8
cout "what to delete?" //prompt once only!
cin >> data;
for(int i = 0; i < yourmax; i++)
{
if(array[i].data == data)
//delete and break or delete multiple(?) keep going
}
it also sounds like you may want to delete just the last element (??) or last few elements (??).
in that case, if using a vector, pop back what you need covers it.
more generally, no, its not bad practice to iterate what you need in a for loop. Its what they do, even if the loop has a wonky 'increment' like *=3 or +=42 or ^=0xAAC. The 'good practice' is that when doing something other than ++ or --, explain it in a comment nearby.
for(int i = 0; i < 5; i++) //Does: 0 1 2 3 4
{ ... }
However, converting this to an i-- format can come in many forms:
1 2 3
//Will loop through a different set of numbers:
for(int i = 5; i > 0; i--) //Does: 5 4 3 2 1 - STILL LOOPS 5 TIMES
{ ... }
OR:
1 2 3
//Now loops through the same set of numbers, still looping 5 times
for(int i = 4; i >= 0; i--) //Does: 4 3 2 1 0 - Notice the i >= 0 instead of i > 0
{ ... }
But no, there's nothing wrong with i++ vs i--. There are valid reasons to want to increment forward or backwards.
However, needing to use i-- to prevent your code from "repeating" something twice seems like a logical error that i-- HAPPENS to solve, and you'd benefit more from solving the real issue rather than simply decrementing.
#include <iostream>
#include <iterator>
#include <ranges> // for std::views::reverse
int main()
{
constint SIZE { 5 };
// a regular array
int arr[SIZE] { 5, 10, 15, 20, 25 };
// old school reverse for loop
for (size_t i { SIZE }; i > 0; --i)
{
std::cout << arr[i - 1] << ' '; // notice the indexing!
}
std::cout << '\n';
// reverse for loop with const iterators
for (auto itr { std::crbegin(arr) }; itr != std::crend(arr); ++itr)
{
std::cout << *itr << ' ';
}
std::cout << '\n';
// https://www.fluentcpp.com/2020/02/11/reverse-for-loops-in-cpp/
// C++20
for (constauto& itr : arr | std::views::reverse)
{
std::cout << itr << ' ';
}
std::cout << '\n';
}
25 20 15 10 5
25 20 15 10 5
25 20 15 10 5
Using an iterator for loop or a range-based for loop on regular arrays "falls apart" when passing the array into a function, it devolves to a pointer. Using a C++ container, say a vector, doesn't suffer this problem. That is one of the reasons why C++ code should use a C++ container whenever possible.
As seeplus showed there are easier ways to manipulate a C++ container's contents using iterators or a range-based for loop, yet another reason to use C++ containers in C++ code.