Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# Loops sending me for a loop... Could use some help
New Posts  All Forums:Forum Nav:

C# Loops sending me for a loop... Could use some help

post #1 of 59
Thread Starter 
Here is my assignment

319

Here is what I have so far....

What to I use to initialize a loop?

375

Any pointers in the right direction are greatly appreciated
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
post #2 of 59
Because I cannot be bothered typing this morning: biggrin.gif

http://msdn.microsoft.com/en-us/library/2aeyhxcd%28v=vs.100%29.aspx (standard while loop)
http://msdn.microsoft.com/en-us/library/ch45axte.aspx (standard for loop)
http://msdn.microsoft.com/en-us/library/ttw7t8t6%28v=vs.80%29.aspx (foreach, in loop).
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
Ol' Sandy
(28 items)
 
"Zeus"
(12 items)
 
Elite Preview
(6 items)
 
CPUMotherboardGraphicsRAM
Intel Xeon E3-1230v3 Gigabyte GA-Z97X-UD5H-BK MSI Gaming GTX 980 Kingston 32GB (4x8) 
Hard DriveHard DriveHard DriveHard Drive
Plextor PX-256M5S 256GB Samsung EVO 1TB Hitachi HDS721010CLA332 Hitachi HDS723020BLA642 
Hard DriveHard DriveHard DriveOptical Drive
Hitachi HDS723020BLA642 Hitachi HUA722010CLA330 WDC WD10EARS-00Z5B1 TSSTcorp CDDVDW SH-S223B 
CoolingCoolingOSMonitor
Phanteks PH-TC14PE with TY-140's Lamptron FCv5 (x2) Windows 8 Pro 64-bit Dell U2412M 
MonitorMonitorMonitorKeyboard
Dell U2412M Dell U2212HM Dell U2713HM Topre Realforce 87UB | Ducky DK9087 G2 Pro 
PowerCaseMouseMouse Pad
Corsair AX-750 Corsair Obsidian 650D Logitech G700 XTRAC Ripper XXL 
AudioAudioAudioAudio
Beyerdynamic DT-770 Pro 250ohm Schiit Bifrost DAC Schiit Asgard 2 HiVi Swan M50W 2.1 
CPUMotherboardRAMHard Drive
Intel Xeon E5-2620 Super Micro X9SRL-F-B 128GB 1333MHz LSI 9271-8i 
OSPowerCase
VMware ESXi 5.5 SeaSonic SS-400FL2 Fractal Define R3 
CPUMotherboardGraphicsRAM
Intel Core i5-3437U HP EliteBook Folio 9470m  Intel HD Graphics 4000  16GB DDR3 SDRAM 
Hard DriveOS
256GB SSD Windows 10 Insider Preview 
  hide details  
Reply
post #3 of 59
Thread Starter 
But which variable do I initialize?

Initial Population?
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
post #4 of 59
Not sure what you mean. Years will be your loop index.

for (years = 0; years < 16; years++)
{

}

--Alex
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
3570K  GB Z77x-UD5H GTX465GE / 470 Flashed G-Skill  
Hard DriveOSPower
128GB Vertex 4 (x2) Win7 64 PCP&C 750w 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
3570K  GB Z77x-UD5H GTX465GE / 470 Flashed G-Skill  
Hard DriveOSPower
128GB Vertex 4 (x2) Win7 64 PCP&C 750w 
  hide details  
Reply
post #5 of 59
Thread Starter 
We have not learned for loops yet

All we have dealt with are "while"

Can this assignment be done with a while?
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
post #6 of 59
Yea.... Same thing really.

years = 0;

While (years < 16)
{
// Code here
years++;
}


For loops just combine the declaration, while, and increment in one line....
--Alex
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
3570K  GB Z77x-UD5H GTX465GE / 470 Flashed G-Skill  
Hard DriveOSPower
128GB Vertex 4 (x2) Win7 64 PCP&C 750w 
  hide details  
Reply
My System
(13 items)
 
  
CPUMotherboardGraphicsRAM
3570K  GB Z77x-UD5H GTX465GE / 470 Flashed G-Skill  
Hard DriveOSPower
128GB Vertex 4 (x2) Win7 64 PCP&C 750w 
  hide details  
Reply
post #7 of 59
Thread Starter 
Ah! Well how would that work if I wanted to ask the user to input the number of years and it then fill into that loop?

See my code above to see what I have asked to user for in my code already
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
post #8 of 59
loops are easy.

theres basically 3 types:

the for loop (useful for arrays)
Code:
for (/*some variable*/; /*run loop while this condition is true*/; /*do something to the variable on each run of the loop*/)
{
/*do this stuff*/
}

example:
Code:
int y = 10;
for (int x = 0; x < 10; x++)
{
y--;
}

this will declare and set variable x to 0, then run the loop, incrementing x by 1 each time until x reaches 10. each time the loop runs int y will be decreased by 1.

The other two are very similar and even easier to understand:

The while loop:
Code:
while (/*some condition*/)
{
/*do this stuff*/
}

example:
Code:
int x = 0, y = 10;
while (x != y)
{
x++;
}

this loop will check if x is equal to y, if x is NOT equal to y, the loop will run, and x will be incremented by 1. this loop will run until x equals y.

finally you have the do-while loop:
Code:
do
{
/*do this stuff*/
}
while (/*some condition*/);

example:
Code:
int x = 0, y = 10;
do
{
x++;
}
while (x != y);

this is basically what the while loop does, but do while loop will always run at least once before checking the condition. so even if x is equal to y the loop would run once and x would be incremented once before breaking out.

you can also make a loop using goto statements and such, but that's only useful in special situations.
Edited by melterx12 - 2/14/12 at 7:09pm
post #9 of 59
Thread Starter 
Thanks for that breakdown.... definitely going in my notes. But how would I ask a user for input such as years and then plug it into the loop?
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
Zaxbuild Mk II
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel 2500k Asus P67 WS Revolution EVGA GTX 670 SC 4GB 2x4gb G.Skill Rip Jaws CL6 
Hard DriveOptical DriveCoolingOS
Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... Asus 24x CD/DVD-RW XSPC RASA 240 Kit Windows 7 Pro 64bit 
MonitorKeyboardPowerCase
4 x ASUS VN247H-P's in Surround + Accessory Logitech G15 Corsair HX750 NZXT Phantom-White 
MouseMouse PadAudio
Logitech G5 Razer Kabuto Asus Xnonar DX 
  hide details  
Reply
post #10 of 59
Quote:
Originally Posted by Zaxbys View Post

Thanks for that breakdown.... definitely going in my notes. But how would I ask a user for input such as years and then plug it into the loop?

I'm not sure what the syntax is for printing to the console is in c#, so I'll explain with C++ syntax. the concept is the same.

basically something like this:
Code:
int numYears;

cout << "Enter the number of years for the simulation: "; //output message to console asking for number of years.
cin >> numYears; //get number from user and store it into integer numYears.

while (numYears < 0)
{
cout << "Invalid Number! \nEnter the number of years for the simulation: "; //output that number entered is invalid and ask again for number of years.
cin >> numYears; //get number from user again and store it into integer numYears.
}

basically you ask for input before the loop. then once the code for the loop is executed, the loop will check if numYears is < 0 or not. if it is, it will go into the loop and ask you again for a number, and will keep looping forever until you enter a positive number.
If the number was positive right from the start, the loop will be skipped entirely.

EDIT: I beleive for C# you use something like Console.Writeline("Text here"); instead of cout, and something like numYears = Console.Readline(); for cin.
Edited by melterx12 - 2/14/12 at 7:21pm
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C# Loops sending me for a loop... Could use some help