New Posts  All Forums:Forum Nav:

For loops in C#

post #1 of 15
Thread Starter 
Can someone explain for loops to me. I have been researching a GOOD easy method on how to understand them but I am at a loss

37

Here is the assignment I am struggling with

Thanks ahead of time for any advice

~Zax
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 15
Format:

{for (initialize; booleanExpression; increment)

{
Statement(s)tobeexecuted
}


i.e.

for (ctr = 1; ctr <= 10; ctr++)
The Laptop
(6 items)
 
  
CPUGraphicsRAMHard Drive
Intel i7 2860QM AMD Radeon HD 6770M Corsair Vengence 1600MHz Samsung 840 Pro 256GB 
OSMonitor
Mac OS X 10.8 1680 x 1050 anti-glare high resolution  
  hide details  
Reply
The Laptop
(6 items)
 
  
CPUGraphicsRAMHard Drive
Intel i7 2860QM AMD Radeon HD 6770M Corsair Vengence 1600MHz Samsung 840 Pro 256GB 
OSMonitor
Mac OS X 10.8 1680 x 1050 anti-glare high resolution  
  hide details  
Reply
post #3 of 15
Thread Starter 
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)
 
  
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 15
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
Compooter
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k ASUS P8P67 Deluxe Sapphire HD 6970 G.Skill DDR3 16 GB 
Hard DriveHard DriveCoolingOS
Seagate 1 TB Seagate 1 TB Thermalright HR01-Plus Windows 8 Professional 
MonitorPowerCaseMouse
Acer G245HQ Antec BP550Plus CORSAIR CARBIDE 200R Logitech G400 
Audio
JVC HA-RX500 
  hide details  
Reply
Compooter
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k ASUS P8P67 Deluxe Sapphire HD 6970 G.Skill DDR3 16 GB 
Hard DriveHard DriveCoolingOS
Seagate 1 TB Seagate 1 TB Thermalright HR01-Plus Windows 8 Professional 
MonitorPowerCaseMouse
Acer G245HQ Antec BP550Plus CORSAIR CARBIDE 200R Logitech G400 
Audio
JVC HA-RX500 
  hide details  
Reply
post #5 of 15
Thread Starter 
Quote:
Originally Posted by l1o2l View Post

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

Whats a modulus?
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 15
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
Gaming Rig
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4,2GHz MSI P67-GD65 MSI Radeon HD6950 2GB + Sapphire Radeon HD6950 2GB 8GB Kingston HyperX 1600 
Hard DriveOptical DriveOSMonitor
Crucial C300 64GB + 3 x 1,5TB HDD LG DVD-RW  Windows 7 Professional x64 Samsung SyncMaster T240HD 24'' 
KeyboardPowerCaseMouse
Razer BlackWidow Corsair TX850M Lian Li A10B Corsair M90 
Mouse PadAudio
SteelSeries QcK mini Logitech G930 
  hide details  
Reply
Gaming Rig
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k @4,2GHz MSI P67-GD65 MSI Radeon HD6950 2GB + Sapphire Radeon HD6950 2GB 8GB Kingston HyperX 1600 
Hard DriveOptical DriveOSMonitor
Crucial C300 64GB + 3 x 1,5TB HDD LG DVD-RW  Windows 7 Professional x64 Samsung SyncMaster T240HD 24'' 
KeyboardPowerCaseMouse
Razer BlackWidow Corsair TX850M Lian Li A10B Corsair M90 
Mouse PadAudio
SteelSeries QcK mini Logitech G930 
  hide details  
Reply
post #7 of 15
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)
 
  
CPUMotherboardGraphicsGraphics
2x E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) ASUS 7870HD (win7 VM) 
RAMHard DriveHard DriveCooling
64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 2x Apogee HD  
CoolingCoolingCoolingCooling
2x RX 480 2x MCP 655 RP-452x2 rev1 (deprecated.) 16x AP-29  
CoolingOSOSOS
EK FC 580 Gentoo + around 14 other VMs + windows 7 gaming VM 
MonitorMonitorKeyboardPower
Viewsonic 23" 1080P Viewsonic 19" Logitech G510 Corsair AX 1200 
CaseMouseAudioAudio
Case Labs TH10 Logitech G500 Onboard (Linux host) Usb sound adapter (win7 VM) 
AudioOtherOtherOther
2 sets of Logitech speakers (Linux host / win7 VM) 2x Lamptron FC-5 IOGEAR 2 way HDMI KVM Switch Sonnet USB2 controller (win7 VM) 
  hide details  
Reply
untitled
(30 items)
 
  
CPUMotherboardGraphicsGraphics
2x E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) ASUS 7870HD (win7 VM) 
RAMHard DriveHard DriveCooling
64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 2x Apogee HD  
CoolingCoolingCoolingCooling
2x RX 480 2x MCP 655 RP-452x2 rev1 (deprecated.) 16x AP-29  
CoolingOSOSOS
EK FC 580 Gentoo + around 14 other VMs + windows 7 gaming VM 
MonitorMonitorKeyboardPower
Viewsonic 23" 1080P Viewsonic 19" Logitech G510 Corsair AX 1200 
CaseMouseAudioAudio
Case Labs TH10 Logitech G500 Onboard (Linux host) Usb sound adapter (win7 VM) 
AudioOtherOtherOther
2 sets of Logitech speakers (Linux host / win7 VM) 2x Lamptron FC-5 IOGEAR 2 way HDMI KVM Switch Sonnet USB2 controller (win7 VM) 
  hide details  
Reply
post #8 of 15
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.
Compooter
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k ASUS P8P67 Deluxe Sapphire HD 6970 G.Skill DDR3 16 GB 
Hard DriveHard DriveCoolingOS
Seagate 1 TB Seagate 1 TB Thermalright HR01-Plus Windows 8 Professional 
MonitorPowerCaseMouse
Acer G245HQ Antec BP550Plus CORSAIR CARBIDE 200R Logitech G400 
Audio
JVC HA-RX500 
  hide details  
Reply
Compooter
(16 items)
 
  
CPUMotherboardGraphicsRAM
Intel i7 2600k ASUS P8P67 Deluxe Sapphire HD 6970 G.Skill DDR3 16 GB 
Hard DriveHard DriveCoolingOS
Seagate 1 TB Seagate 1 TB Thermalright HR01-Plus Windows 8 Professional 
MonitorPowerCaseMouse
Acer G245HQ Antec BP550Plus CORSAIR CARBIDE 200R Logitech G400 
Audio
JVC HA-RX500 
  hide details  
Reply
post #9 of 15
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!
Boba Fett
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k Asus Maximus Extreme-Z 3 x Palit GTX 580 3GB 16g Corsair Vengeance 1600 
Hard DriveOSMonitorKeyboard
OCZ Agility 3 120g Windows 7 Ultimate 64 bit 3 x Dell u2410 Razer Lycosa 
PowerCaseMouse
Corsair AX1200 Danger Den LDR Tower 29 Logitech G9X 
  hide details  
Reply
Boba Fett
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600k Asus Maximus Extreme-Z 3 x Palit GTX 580 3GB 16g Corsair Vengeance 1600 
Hard DriveOSMonitorKeyboard
OCZ Agility 3 120g Windows 7 Ultimate 64 bit 3 x Dell u2410 Razer Lycosa 
PowerCaseMouse
Corsair AX1200 Danger Den LDR Tower 29 Logitech G9X 
  hide details  
Reply
post #10 of 15
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)
 
  
CPUMotherboardGraphicsRAM
Core i5 2500k 4.7 Ghz ASRock Z68 Extreme4 MSI GTX 670 PE 8 GB Crucial Ballistix 
Hard DriveHard DriveCoolingOS
1 TB WD Green Crucial M4 Antec Kuhler H20 620 Windows 7 Ultimate x86_64 SP1 
MonitorMonitorKeyboardPower
HPW23something Dell U2312HM CM Storm Quickfire Pro OCZ 600W 
CaseMouseMouse PadAudio
Fractal Design Define R4 Logitech G400 Steelseries Qck Pioneer VSX-821 
  hide details  
Reply
DeadHost
(16 items)
 
  
CPUMotherboardGraphicsRAM
Core i5 2500k 4.7 Ghz ASRock Z68 Extreme4 MSI GTX 670 PE 8 GB Crucial Ballistix 
Hard DriveHard DriveCoolingOS
1 TB WD Green Crucial M4 Antec Kuhler H20 620 Windows 7 Ultimate x86_64 SP1 
MonitorMonitorKeyboardPower
HPW23something Dell U2312HM CM Storm Quickfire Pro OCZ 600W 
CaseMouseMouse PadAudio
Fractal Design Define R4 Logitech G400 Steelseries Qck Pioneer VSX-821 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming