Link to home
Create AccountLog in
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.
Avatar of David Johnson, CD
David Johnson, CD
Flag of Canada image

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

Open in new window

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
Avatar of Qlemo
Qlemo
Flag of Germany image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of phoffric
phoffric

@David,
Could you take a look at your if statement again. If both terms are 0, then the for  loop is still performed.
it was supposed to be NOT equal rather than Equal. Qlemo fixed it.
Avatar of Ingo Förster

ASKER

Many thanks for the solution. Special to Qlemo.