Author Topic: c programming for loop increment <solved>  (Read 4168 times)

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
c programming for loop increment <solved>
« on: February 26, 2011, 11:41:23 AM »
for (initialize variable; test; increment)

Code: [Select]
for (i=0; i<=10; i++)I understand this will make "i" increment by counts of 1 until the loop is satisfied. However, how do I make it increment by 3?
Obviously this is wrong buy my intent is:
Code: [Select]
for (i=0; i<=12; i+3)

Also, can you call a function as the increment? I couldn't get that to work either.
Obviously this is wrong buy my intent is:
Code: [Select]
for (i=0; i<=12; somefunction(i))
printf ("%i\n", i);

return 0;
}


int somefunction (int x)
{
    return (x + 3);
}



« Last Edit: February 26, 2011, 01:01:59 PM by docnascar »
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: c programming for loop increment
« Reply #1 on: February 26, 2011, 11:51:50 AM »
I got the increment +3 part... (found it as a c++ command that works for c  :D)
Code: [Select]
for (i=0; i<=10; i+=3)
My question still stands on the function.

My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p

Offline AS

  • Hero Member
  • *****
  • Posts: 4098
  • Have a nice ... night!
Re: c programming for loop increment
« Reply #2 on: February 26, 2011, 12:01:33 PM »

Obviously this is wrong buy my intent is:
Code: [Select]
for (i=0; i<=12; somefunction(i))
printf ("%i\n", i);

return 0;
}


int somefunction (int x)
{
    return (x + 3);
}


Your intent probably is:
for (i=0; i<=12; i = somefunction(i))

AS

Offline docnascar

  • Sr. Member
  • ****
  • Posts: 465
Re: c programming for loop increment
« Reply #3 on: February 26, 2011, 01:01:17 PM »
Nice! Thanks!
My main PCLINUXOS PC:
KDE Mini
AMD FX-6300 (3.5G / 6 core)
MSI 970A-G46 AM3+ MOBO
G.SKILL Sniper Series 8GB (2 x 4GB) 1866 (PC3 14900)
ECS GeForce GT 440 (Fermi) 512MB 128-bit GDDR5
Seagate Barracuda ST1000DM003 1TB 7200 RPM SATA
SAMSUNG DVD Burner SATA Model SH-224BB
POWERUP PU-550 (550W) p