Avatar of Ingo Förster
Ingo Förster
 asked on

Avoid for loop with 0

I have a beginner question about c++. How I can avoid that the for loops run when FirstSession and LastSession both are 0.

   
 for (int32 i = mi.nFirstSession; i <= mi.nLastSession; i++)
{

Open in new window


Currently it will run if FirstSession and LastSession are 0.
C++

Avatar of undefined
Last Comment
Ingo Förster

8/22/2022 - Mon
David Johnson, CD

if (mi.nFirstSession == 0 || mi.nLastSession == 0)
{
for (int32 i = mi.nFirstSession; i <= mi.nLastSession; i++)
{}
}

Open in new window

Qlemo

Above should be
if (mi.nFirstSession != 0 || mi.nLastSession != 0)
  for (int32 i = mi.nFirstSession; i <= mi.nLastSession; i++)
  {}

Open in new window

ASKER CERTIFIED SOLUTION
Qlemo

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
phoffric

@David,
Could you take a look at your if statement again. If both terms are 0, then the for  loop is still performed.
Your help has saved me hundreds of hours of internet surfing.
fblack61
David Johnson, CD

it was supposed to be NOT equal rather than Equal. Qlemo fixed it.
Ingo Förster

ASKER
Many thanks for the solution. Special to Qlemo.