I found a post where FOR loop is used without CONDITION value. Here is a loop:
for (INITIALIZATION; CONDITION; AFTERTHOUGHT)
{
// Code for the for-loop's body goes here.
}
It's not safe to skip CONDITION value, but if you use if/else
statement it can be done. Please take a look on my for loop: for (int i = 1; ; i++)
and implementation inside. For some reason, I don't get proper logic with if/else
statement.
#include <iostream>
using namespace std;
int main() {
int boxes;
int boxes_for_sale;
cout << "Enter quantity of the boxes in warehouse: > " << flush;
cin >> boxes;
cout << "Enter quantity of the boxes for sale: > " << flush;
cin >> boxes_for_sale;
for (int i = 1;; i++) {
if (boxes < boxes_for_sale) {
cout << "There are not enough boxes in warehouse!" << endl;
cout << "Enter quantity of the boxes for sale: > " << flush;
cin >> boxes_for_sale;
}
else
boxes -= boxes_for_sale;
cout << "Car N:" << i << " is full\n" << "You have " << boxes << "boxes for sale" << endl;
if (boxes == 0)
cout << "Sold out!" << endl;
break;
}
return 0;
}