New Posts  All Forums:Forum Nav:

For loops in C# - Page 2

post #11 of 15
Quote:
Originally Posted by Zaxbys View Post

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

Here's probably the best answer you can give:

int product = 1;

for (int i = 1; i < 8; i++) {
if (i%2 == 1)
product *= i;
}

return product
Logic
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASRock P67 Extreme4 Gen3 ASUS HD 7950 TWIN FROZR 3GB Corsair Vengeance 16GB DDR3-1600 
Hard DriveCoolingOSMonitor
Intel 330 120GB SSD + Spinpoint F3 1TB Corsair H60 Windows 7 Ultimate x64 Samsung 24.6" P2570HD 
KeyboardPowerCaseMouse
Filco Ninja Majestouch-2 Tenkeyless Antec EarthWatts 650W Corsair 600T Silver Logitech G500 
  hide details  
Reply
Logic
(14 items)
 
  
CPUMotherboardGraphicsRAM
i7 2600K ASRock P67 Extreme4 Gen3 ASUS HD 7950 TWIN FROZR 3GB Corsair Vengeance 16GB DDR3-1600 
Hard DriveCoolingOSMonitor
Intel 330 120GB SSD + Spinpoint F3 1TB Corsair H60 Windows 7 Ultimate x64 Samsung 24.6" P2570HD 
KeyboardPowerCaseMouse
Filco Ninja Majestouch-2 Tenkeyless Antec EarthWatts 650W Corsair 600T Silver Logitech G500 
  hide details  
Reply
post #12 of 15
wrong, that is doing extra instructions inside of the loop. The i += 2 variant will run faster

and you forgot the braces for the block inside the if - yes they're not required, but you should always use braces, even for blocks of only one statement for a number of reasons. What the code monkey that takes your job has to add j+=i; below your line in the block 6 months from now? .. hmm..
But there's no chance of a problem if you would have put the braces there for him to start with. It can also cause problems when applying patches..

and c# does have those lamba expressions.... i wouldn't do that here but it's pretty clever sometimes. it might net you some bonus points, either that of an F, depends.
Edited by lloyd mcclendon - 2/19/12 at 5:31pm
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
stable again
(25 items)
 
  
CPUCPUMotherboardGraphics
E5-2687W E5-2687W ASUS Z9PED8-WS EVGA GTX 570 (Linux host) 
GraphicsRAMHard DriveHard Drive
EVGA GTX 970 FTW (win7 guest) 64GB G.SKILL 2133 2x Crucial M4 256GB raid1 4x 3TB raid 10 
CoolingCoolingCoolingCooling
2x Apogee HD  2x RX 480 2x MCP 655 RP-452x2 rev2 (new) 
CoolingCoolingOSOS
16x Cougar Turbine CFT12SB4 (new) EK FC 580 Gentoo (host) Gentoo (x23 guests) 
OSMonitorMonitorPower
windows 7 (guest w/ vfio-pci) Viewsonic 23" 1080P Viewsonic 19" Antec HCP Platinum 1000 (new) 
CaseOtherOther
Case Labs TH10 (still the best ever) 2x Lamptron FC-5 IOGEAR 2 way DVI KVM Switch 
  hide details  
Reply
post #13 of 15
Thread Starter 
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {

            int product = 1;

            Console.WriteLine("To find the product of the odd integers from 1 to 7, press any key");
            Console.ReadKey();

            for (int i = 1; i < 8; i += 2)
            {
                product *= i;
            }

            Console.WriteLine("");
            Console.Write("The Product of (1)*(3)*(5)*(7) is {0}", product);

            Console.ReadKey();


        }
    }
}
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 #14 of 15
Thread Starter 
Also, I remember my instructor saying it is bad form to change "i" inside the { } of the for loop

What did he mean by this?

Can this code be written without doing that?
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 #15 of 15
What your instructor meant is probably something like this:
Code:
for(i=1; i<10; i++)
{
  if(something happens)
  {
    do this
  }
  else
  {
    i=i-1;
  }
}

If you modify the "i" inside the loop, that's bad practice, if there is a case you need to do that, it should be done with a while.

However, you are using the numbers stored in the variable "i", so there is no problem there, as you aren't making "i" change inside the loop, you are simply using it.
Edited by Barbaroti - 2/19/12 at 5:50pm
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
First Build
(17 items)
 
  
CPUMotherboardGraphicsRAM
i7-2600k @4.5Ghz Gigabyte Z68X-UD7 B3 MSI 6950 G.Skill Sniper 2x4GB 
Hard DriveHard DriveHard DriveCooling
Crucial M4 64GB Samsung SpinPoint 250GB Hitachi 1TB Noctua NH-D14 
OSMonitorMonitorKeyboard
Windows Server 2012 Samsung S22B300 22' ViewSonic VA703B 17" CM Quickfire TK 
PowerCaseMouse
Corsair HX850 NZXT Phantom Mionix Naos 3200 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming