Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › For loops in C#
Featured Sponsors
Recent Reviews
-
Picked these up for my TRON Legacy theme build. Got the optional light bar upgrade kit(s). Note, for a quad channel package you will need two light bar kits as each covers two sticks. No issues...
-
I honestly love this cooler, but I think I'm gonna move on to another one soon, it just looks... A bit to bland compared to other flaming red (literally) coolers on the market.
-
Metro 2033 is one of the greatest FPS games ever made. Although not usually recognized as such, due to the horrible state the gaming industry is in, it succeeds largely on its storytelling,...
-
This is an update of the original Fractal Design Arc Midi. The solid left panel has been replaced by a panel with a plexiglass window and the top USB ports are now USB-3.0. Let me state at the...
-
I've used this headset extensively for MAG on PS3. The sounds were wonderful, the mic was crisp, and the ability to adjust peoples voice volume is absolutely invaluable. For $100 ( or less ) this...
For loops in C#
post #2 of 15
2/19/12 at 3:25pm
- appleg33k85
- Overclocker
-




- Joined: Sep 2010
- Location: Long Island, NY
- Posts: 4,600
- Rep: 655 (Unique: 585)
- Trader Rating: 41
- Select All Posts By This User
Format:
{for (initialize; booleanExpression; increment)
{
Statement(s)tobeexecuted
}
i.e.
for (ctr = 1; ctr <= 10; ctr++)
{for (initialize; booleanExpression; increment)
{
Statement(s)tobeexecuted
}
i.e.
for (ctr = 1; ctr <= 10; ctr++)
The Mini Beast!
(13 items) |
The Laptop
(6 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Core i5 3570k | ASRock Z77E-ITX | Asus GTX 480 | Samsung DDR3 1600MHz |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate Barracuda 500GB | Crucial M4 128GB | Noctua NH-L12 | Windows 7 Pro 64Bit |
| Monitor | Keyboard | Power | Case |
| Dell U3011 | Microsoft Generic in Black! | SILVERSTONE ST50F-P 500W | Lian Li PC-Q25B |
| Mouse | |||
| CM Sentinal | |||
| View all | |||
| CPU | Graphics | RAM | Hard Drive |
|---|---|---|---|
| Intel i7 2860QM | AMD Radeon HD 6770M | Corsair Vengence 1600MHz | 750GB 7200rpm |
| OS | Monitor | ||
| Mac OS X 10.8 | 1680 x 1050 anti-glare high resolution | ||
| View all | |||
The Mini Beast!
(13 items) |
The Laptop
(6 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Core i5 3570k | ASRock Z77E-ITX | Asus GTX 480 | Samsung DDR3 1600MHz |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate Barracuda 500GB | Crucial M4 128GB | Noctua NH-L12 | Windows 7 Pro 64Bit |
| Monitor | Keyboard | Power | Case |
| Dell U3011 | Microsoft Generic in Black! | SILVERSTONE ST50F-P 500W | Lian Li PC-Q25B |
| Mouse | |||
| CM Sentinal | |||
| View all | |||
| CPU | Graphics | RAM | Hard Drive |
|---|---|---|---|
| Intel i7 2860QM | AMD Radeon HD 6770M | Corsair Vengence 1600MHz | 750GB 7200rpm |
| OS | Monitor | ||
| Mac OS X 10.8 | 1680 x 1050 anti-glare high resolution | ||
| View all | |||
- Joined: Apr 2008
- Location: GA, USA
- Posts: 935
- Rep: 45 (Unique: 44)
- Trader Rating: 18
- Select All Posts By This User
So far here is my code:
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
for(int i = 1; i < 8; i++)
}
}
}
Zaxbuild Mk II
(15 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel 2500k | Asus P67 WS Revolution | EVGA GTX 670 SC 4GB | 2x4gb G.Skill Rip Jaws CL6 |
| Hard Drive | Optical Drive | Cooling | OS |
| Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... | Asus 24x CD/DVD-RW | XSPC RASA 240 Kit | Windows 7 Pro 64bit |
| Monitor | Keyboard | Power | Case |
| Triple Samsung S24B300EL's | Logitech G15 | Corsair HX750 | NZXT Phantom-White |
| Mouse | Mouse Pad | Audio | |
| Logitech G5 | Razer Kabuto | Asus Xnonar DX | |
| View all | |||
Zaxbuild Mk II
(15 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel 2500k | Asus P67 WS Revolution | EVGA GTX 670 SC 4GB | 2x4gb G.Skill Rip Jaws CL6 |
| Hard Drive | Optical Drive | Cooling | OS |
| Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... | Asus 24x CD/DVD-RW | XSPC RASA 240 Kit | Windows 7 Pro 64bit |
| Monitor | Keyboard | Power | Case |
| Triple Samsung S24B300EL's | Logitech G15 | Corsair HX750 | NZXT Phantom-White |
| Mouse | Mouse Pad | Audio | |
| Logitech G5 | Razer Kabuto | Asus Xnonar DX | |
| View all | |||
post #4 of 15
2/19/12 at 3:31pm
- l1o2l
- Intel Overclocker
- Joined: Feb 2011
- Posts: 145
- Rep: 5 (Unique: 5)
- Select All Posts By This User
Inside the for loop, use modulus to determine if its an odd or even number.
For example, if(i%2 !=0), not even. i%2 == 0 is even
For example, if(i%2 !=0), not even. i%2 == 0 is even
Compooter
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel i7 2600k | ASUS P8P67 Deluxe | Sapphire HD 6970 | G.Skill DDR3 16 GB |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate 1 TB | Seagate 1 TB | Thermalright HR01-Plus | Windows 8 Professional |
| Monitor | Power | Case | Mouse |
| Acer G245HQ | Antec BP550Plus | CORSAIR CARBIDE 200R | Logitech G400 |
| Audio | |||
| JVC HA-RX500 | |||
| View all | |||
Compooter
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel i7 2600k | ASUS P8P67 Deluxe | Sapphire HD 6970 | G.Skill DDR3 16 GB |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate 1 TB | Seagate 1 TB | Thermalright HR01-Plus | Windows 8 Professional |
| Monitor | Power | Case | Mouse |
| Acer G245HQ | Antec BP550Plus | CORSAIR CARBIDE 200R | Logitech G400 |
| Audio | |||
| JVC HA-RX500 | |||
| View all | |||
- Joined: Apr 2008
- Location: GA, USA
- Posts: 935
- Rep: 45 (Unique: 44)
- Trader Rating: 18
- Select All Posts By This User
Quote:
Whats a modulus?
Zaxbuild Mk II
(15 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel 2500k | Asus P67 WS Revolution | EVGA GTX 670 SC 4GB | 2x4gb G.Skill Rip Jaws CL6 |
| Hard Drive | Optical Drive | Cooling | OS |
| Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... | Asus 24x CD/DVD-RW | XSPC RASA 240 Kit | Windows 7 Pro 64bit |
| Monitor | Keyboard | Power | Case |
| Triple Samsung S24B300EL's | Logitech G15 | Corsair HX750 | NZXT Phantom-White |
| Mouse | Mouse Pad | Audio | |
| Logitech G5 | Razer Kabuto | Asus Xnonar DX | |
| View all | |||
Zaxbuild Mk II
(15 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel 2500k | Asus P67 WS Revolution | EVGA GTX 670 SC 4GB | 2x4gb G.Skill Rip Jaws CL6 |
| Hard Drive | Optical Drive | Cooling | OS |
| Crucial M4 128gb & Crucial C300 60gb SSD & 2x 1... | Asus 24x CD/DVD-RW | XSPC RASA 240 Kit | Windows 7 Pro 64bit |
| Monitor | Keyboard | Power | Case |
| Triple Samsung S24B300EL's | Logitech G15 | Corsair HX750 | NZXT Phantom-White |
| Mouse | Mouse Pad | Audio | |
| Logitech G5 | Razer Kabuto | Asus Xnonar DX | |
| View all | |||
post #6 of 15
2/19/12 at 3:36pm
- finger00
- Programmer
- Joined: Sep 2011
- Location: Poland
- Posts: 311
- Rep: 17 (Unique: 17)
- Select All Posts By This User
Then use another variable like 'product' to store your product. You need to assign it a value of 1 before you enter the loop though. It could go like this (inside the for loop):
{
if (i%2=1) then{
product = i * product
}
}
Pardon my syntax if it is wrong, I'm mostly used to VB
EDIT: Modulus is the rest you get from dividing two numbers
{
if (i%2=1) then{
product = i * product
}
}
Pardon my syntax if it is wrong, I'm mostly used to VB
EDIT: Modulus is the rest you get from dividing two numbers
Gaming Rig
(14 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| i7 2600k @4,2GHz | MSI P67-GD65 | MSI Radeon HD6950 2GB + Sapphire Radeon HD6950 2GB | 8GB Kingston HyperX 1600 |
| Hard Drive | Optical Drive | OS | Monitor |
| Crucial C300 64GB + 3 x 1,5TB HDD | LG DVD-RW | Windows 7 Professional x64 | Samsung SyncMaster T240HD 24'' |
| Keyboard | Power | Case | Mouse |
| Razer BlackWidow | Corsair TX850M | Lian Li A10B | Corsair M90 |
| Mouse Pad | Audio | ||
| SteelSeries QcK mini | Logitech G930 | ||
| View all | |||
Gaming Rig
(14 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| i7 2600k @4,2GHz | MSI P67-GD65 | MSI Radeon HD6950 2GB + Sapphire Radeon HD6950 2GB | 8GB Kingston HyperX 1600 |
| Hard Drive | Optical Drive | OS | Monitor |
| Crucial C300 64GB + 3 x 1,5TB HDD | LG DVD-RW | Windows 7 Professional x64 | Samsung SyncMaster T240HD 24'' |
| Keyboard | Power | Case | Mouse |
| Razer BlackWidow | Corsair TX850M | Lian Li A10B | Corsair M90 |
| Mouse Pad | Audio | ||
| SteelSeries QcK mini | Logitech G930 | ||
| View all | |||
post #7 of 15
2/19/12 at 3:39pm
- lloyd mcclendon
- Programmer
-

- Joined: Oct 2008
- Posts: 963
- Rep: 93 (Unique: 68)
- Trader Rating: 5
- Select All Posts By This User
Code:
using System;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int product = 1;
for(int i = 1; i < 8; i += 2)
{
product *= i;
}
}
Console.write(product);
}
}
i don't have a c# compiler to test it (thank god) but i think that will do the trick.
untitled
(30 items) |
| CPU | Motherboard | Graphics | Graphics |
|---|---|---|---|
| 2x E5-2687W | ASUS Z9PED8-WS | EVGA GTX 570 (Linux host) | ASUS 7870HD (win7 VM) |
| RAM | Hard Drive | Hard Drive | Cooling |
| 64GB G.SKILL 2133 | 2x Crucial M4 256GB raid1 | 2x (2x 1TB raid1) | 2x Apogee HD |
| Cooling | Cooling | Cooling | Cooling |
| 2x RX 480 | 2x MCP 655 | RP-452x2 rev1 (deprecated.) | 16x AP-29 |
| Cooling | OS | OS | OS |
| EK FC 580 | Gentoo | + around 14 other VMs | + windows 7 gaming VM |
| Monitor | Monitor | Keyboard | Power |
| Viewsonic 23" 1080P | Viewsonic 19" | Logitech G510 | Corsair AX 1200 |
| Case | Mouse | Audio | Audio |
| Case Labs TH10 | Logitech G500 | Onboard (Linux host) | Usb sound adapter (win7 VM) |
| Audio | Other | Other | Other |
| 2 sets of Logitech speakers (Linux host / win7 VM) | 2x Lamptron FC-5 | IOGEAR 2 way HDMI KVM Switch | Sonnet USB2 controller (win7 VM) |
| View all | |||
untitled
(30 items) |
| CPU | Motherboard | Graphics | Graphics |
|---|---|---|---|
| 2x E5-2687W | ASUS Z9PED8-WS | EVGA GTX 570 (Linux host) | ASUS 7870HD (win7 VM) |
| RAM | Hard Drive | Hard Drive | Cooling |
| 64GB G.SKILL 2133 | 2x Crucial M4 256GB raid1 | 2x (2x 1TB raid1) | 2x Apogee HD |
| Cooling | Cooling | Cooling | Cooling |
| 2x RX 480 | 2x MCP 655 | RP-452x2 rev1 (deprecated.) | 16x AP-29 |
| Cooling | OS | OS | OS |
| EK FC 580 | Gentoo | + around 14 other VMs | + windows 7 gaming VM |
| Monitor | Monitor | Keyboard | Power |
| Viewsonic 23" 1080P | Viewsonic 19" | Logitech G510 | Corsair AX 1200 |
| Case | Mouse | Audio | Audio |
| Case Labs TH10 | Logitech G500 | Onboard (Linux host) | Usb sound adapter (win7 VM) |
| Audio | Other | Other | Other |
| 2 sets of Logitech speakers (Linux host / win7 VM) | 2x Lamptron FC-5 | IOGEAR 2 way HDMI KVM Switch | Sonnet USB2 controller (win7 VM) |
| View all | |||
post #8 of 15
2/19/12 at 3:40pm
- l1o2l
- Intel Overclocker
- Joined: Feb 2011
- Posts: 145
- Rep: 5 (Unique: 5)
- Select All Posts By This User
Modulus gives you the remainder when you divide 2 numbers.
When you divide two even numbers, the remainder becomes 0.
When you divide an odd number with an even number, the remainder is not zero.
int product =1;
for (int i = 1; i < 8; i++)
{
if(i%2 !=0)
{
product = i *product;
}
}
You want to initialize product to 1 also, otherwise it might have a really weird number.
When you divide two even numbers, the remainder becomes 0.
When you divide an odd number with an even number, the remainder is not zero.
int product =1;
for (int i = 1; i < 8; i++)
{
if(i%2 !=0)
{
product = i *product;
}
}
You want to initialize product to 1 also, otherwise it might have a really weird number.
Compooter
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel i7 2600k | ASUS P8P67 Deluxe | Sapphire HD 6970 | G.Skill DDR3 16 GB |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate 1 TB | Seagate 1 TB | Thermalright HR01-Plus | Windows 8 Professional |
| Monitor | Power | Case | Mouse |
| Acer G245HQ | Antec BP550Plus | CORSAIR CARBIDE 200R | Logitech G400 |
| Audio | |||
| JVC HA-RX500 | |||
| View all | |||
Compooter
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Intel i7 2600k | ASUS P8P67 Deluxe | Sapphire HD 6970 | G.Skill DDR3 16 GB |
| Hard Drive | Hard Drive | Cooling | OS |
| Seagate 1 TB | Seagate 1 TB | Thermalright HR01-Plus | Windows 8 Professional |
| Monitor | Power | Case | Mouse |
| Acer G245HQ | Antec BP550Plus | CORSAIR CARBIDE 200R | Logitech G400 |
| Audio | |||
| JVC HA-RX500 | |||
| View all | |||
post #9 of 15
2/19/12 at 3:44pm
- Mullinz
- Programmer
- Joined: May 2011
- Location: Chicago suburbs
- Posts: 143
- Rep: 6 (Unique: 6)
- Select All Posts By This User
For loops are pretty awesome, once you understand them you can do a lot with them!
Basically you are telling the program to do a certain thing for x number of times.
So if you do
int count = 0;
for(int i = 0; i < 5; i++){
count++;
}
The program will go through the for loop 5 times and each time add one to count, at the end of it making count = 5
You can also make use of the variable i and use it within the loop for whatever you might need it for. As others have said you will have to use the modulus operator to find odd numbers.
Hope this helps a little!
Basically you are telling the program to do a certain thing for x number of times.
So if you do
int count = 0;
for(int i = 0; i < 5; i++){
count++;
}
The program will go through the for loop 5 times and each time add one to count, at the end of it making count = 5
You can also make use of the variable i and use it within the loop for whatever you might need it for. As others have said you will have to use the modulus operator to find odd numbers.
Hope this helps a little!
Boba Fett
(13 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| i7 2600k | Asus Maximus Extreme-Z | 3 x Palit GTX 580 3GB | 16g Corsair Vengeance 1600 |
| Hard Drive | OS | Monitor | Keyboard |
| OCZ Agility 3 120g | Windows 7 Ultimate 64 bit | 3 x Dell u2410 | Razer Lycosa |
| Power | Case | Mouse | |
| Corsair AX1200 | Danger Den LDR Tower 29 | Logitech G9X | |
| View all | |||
Boba Fett
(13 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| i7 2600k | Asus Maximus Extreme-Z | 3 x Palit GTX 580 3GB | 16g Corsair Vengeance 1600 |
| Hard Drive | OS | Monitor | Keyboard |
| OCZ Agility 3 120g | Windows 7 Ultimate 64 bit | 3 x Dell u2410 | Razer Lycosa |
| Power | Case | Mouse | |
| Corsair AX1200 | Danger Den LDR Tower 29 | Logitech G9X | |
| View all | |||
post #10 of 15
2/19/12 at 3:46pm
- thelamacmdr
- 4.0 GHz
-

- Joined: Oct 2011
- Posts: 1,068
- Rep: 32 (Unique: 29)
- Select All Posts By This User
I'd do a I+2 instead of modulus for odd numbers just for consistencies sake in using both odd and even numbers whenever you want to. But that's just me, there's usually something wrong with how I do stuff
DeadHost
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Core i5 2500k 4.7 Ghz | ASRock Z68 Extreme4 | MSI GTX 670 PE | 8 GB Crucial Ballistix |
| Hard Drive | Hard Drive | Cooling | OS |
| 1 TB WD Green | Crucial M4 | Antec Kuhler H20 620 | Windows 7 Ultimate x86_64 SP1 |
| Monitor | Monitor | Keyboard | Power |
| HPW23something | Dell U2312HM | CM Storm Quickfire Pro | OCZ 600W |
| Case | Mouse | Mouse Pad | Audio |
| Fractal Design Define R4 | Logitech G400 | Steelseries Qck | Pioneer VSX-821 |
| View all | |||
DeadHost
(16 items) |
| CPU | Motherboard | Graphics | RAM |
|---|---|---|---|
| Core i5 2500k 4.7 Ghz | ASRock Z68 Extreme4 | MSI GTX 670 PE | 8 GB Crucial Ballistix |
| Hard Drive | Hard Drive | Cooling | OS |
| 1 TB WD Green | Crucial M4 | Antec Kuhler H20 620 | Windows 7 Ultimate x86_64 SP1 |
| Monitor | Monitor | Keyboard | Power |
| HPW23something | Dell U2312HM | CM Storm Quickfire Pro | OCZ 600W |
| Case | Mouse | Mouse Pad | Audio |
| Fractal Design Define R4 | Logitech G400 | Steelseries Qck | Pioneer VSX-821 |
| View all | |||
Return Home
Back to Forum: Coding and Programming
- For loops in C#
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › For loops in C#
Currently, there are 1872 Active Users
(674 Members and 1198 Guests)
Recent Discussions
- › Korean QNIX QX2710 Evolution ll 27" 2560x1440 Matte Samsung PLS... 17 seconds ago
- › Nvidia GeForce GTX TITAN Owners' club 35 seconds ago
- › [Geforce.com] 320.18 WHQL available 1 minute ago
- › Keyboard of the Month: May '13 1 minute ago
- › Project "SBD" (Silent But Deadly) 1 minute ago
- › 780 bottleneck 1 minute ago
- › GTX 600 Series Unlocked Voltage Bios' downloads and tools. 2 minutes ago
- › Metro Last Light flickering (NOT shadows, totally different issue) 2 minutes ago
- › FS: EVGA GTX 670 FTW 2GB 2 minutes ago
- › [Build Log] "900CSQ" - 900D Maxed Out - Everything Watercooled -... 2 minutes ago
View: New Posts | All Discussions
Recent Reviews
- › CORSAIR Dominator Platinum 16GB (4 x 4GB) 240-Pin DDR3 SDRAM DDR3... by TLHarrell
- › Cooler Master Hyper 212 EVO CPU Cooler (RR-212E-20PK-R1) by junhawng
- › Metro: Last Light - Standard Edition by boredgunner
- › Fractal Design Arc Midi R2 FD-CA-ARC-R2-BL-W Computer Case With... by Alan G
- › Wireless Stereo Headset by DoomDash
- › GIGABYTE Radeon HD 7870 GHz Edition GV-R787OC-2GD Video Card by VitalShot
- › Crucial Ballistix sport 16GB (2 x 8GB) 240-Pin DDR3 SDRAM DDR3 1600... by Fieldsweeper
- › Creative 70SB150000000 Sound Card by Fieldsweeper
- › ASUS PB Series PB278Q 27"" 5ms (GTG) WQHD Widescreen LED... by R3apR369
- › SilverStone Aluminum Body ATX Full Tower Computer Case TJ07B -... by R3apR369
View: More Reviews
New Articles
- › METRO 2033 STARTUP CRASH FIX WORKS by xiangelo
- › Why are DNS Servers Important? How to make... by exzacklyright
- › Titanium Backup Guide For Newbies by exzacklyright
- › How to install ADB (Android Debug Bridge) by exzacklyright
- › How to take ownership of a file, folder, or... by exzacklyright
- › How to disable the open file security warning... by exzacklyright
- › Clean and Make a USB Bootable Flash Drive for... by exzacklyright
- › Efficiency Trials by dava4444
- › How on EARTH Do I Build a Computer??... by dava4444
- › How to rip a Windows 7/8 Image using WinPE4.0... by RagingCain
View: New Articles | All Articles
Home | Reviews | Forums | Articles | My Profile
About Overclock.net | Join the Community | Advertise | Contact Us | All Staff
© 2013 Shogun Interactive Development Overclock.net is powered by Huddler Tech | FAQ | Support | Privacy | ToS | DMCA | Site Map
About Overclock.net | Join the Community | Advertise | Contact Us | All Staff
© 2013 Shogun Interactive Development Overclock.net is powered by Huddler Tech | FAQ | Support | Privacy | ToS | DMCA | Site Map






