Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Question about variables in loops`
New Posts  All Forums:Forum Nav:

Java Question about variables in loops`

post #1 of 6
Thread Starter 
Hey guys,

I started back into school about 4 weeks ago working on an IT degree. In my Programming I (Intro to Java) class, our professor posed a question to us and told us we could not find the answer in our book or online, we would need to track a programmer down and ask them. Anyway, I know some of you have some programming experience with Java, so I thought I'd post the question here and see if any of you would know. The question is:
Quote:
When working with loops, one letter variables are OK to use, however the typical variables are i, j, k, l, m and n. Why, when working with loops do we use these variables instead of using other letters, i.e. a, b, c, d, e, etc?

I'm trying to track down one of the programmers here at work that program in Java, but like I said, I just wanted to see if any of you might know. Thanks, guys!
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
post #2 of 6
I want to say this rings a bell in my mind or that I heard something about it before... but I can't seem to remember what I'm thinking of. ...I assume it's not Java specific.?.?
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
Main Desktop PC
(23 items)
 
  
CPUMotherboardGraphicsRAM
Intel Core i7 5820k ASUS x99 Deluxe EVGA GTX 1080 FTW 4 x 4GB 2800 DDR4 Corsair Vengeance 
Hard DriveHard DriveHard DriveHard Drive
256GB Samsung 840 Pro 240GB OCZ ARC100 3TB 7200 RPM Seagate Barracuda 2TB 7200 RPM Seagate Barracuda 
Optical DriveCoolingOSOS
ASUS 24x DVD/Burner Custom Loop Windows 10 Pro 64bit Debian Linux Distro 
MonitorMonitorKeyboardPower
Acer Predator XB270HU ACHIEVA Shimian QH2700 IPSMS Ducky Shine 2 (White LED/Brown Switches) EVGA 1300w G2 + Individually sleeved cables 
CaseMouseMouse PadAudio
Gun Metal NZXT Switch 810 EVGA Troq x10 Logitech G440 Onkyo TX-NR646 
Other
Bitfenix Recon Fan Controller 
  hide details  
Reply
post #3 of 6
You can use any variable of course, now WHY i, j, k... I don't think anyone told me a reason for it, if I had to guess I'd say for loops need an iterator variable(hence the letter), and if you need more than one well keep moving after i. May also refer to int variables in the loop. Anyways... kinda moo point really, just write your code like this and shh him:
Code:
for(int thisIsMyLoopVariable; thisIsMyLoopVariable < thisIsMyOtherVariable; thisIsMyLoopVariable++)

biggrin.gif
Edited by NorCa - 9/25/12 at 11:46am
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
Atroc.V3.2
(16 items)
 
Home Server
(9 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500k [Asrock] Z77E-ITX [EVGA] GTX 770 [Samsung] 8GB 30nm 
Hard DriveCoolingCoolingOS
[Crucial] M4 64Gb [Silverstone] NT06-PRO [Gelid] 120mm PWM Windows 7 Ultimate x64 
MonitorKeyboardPowerCase
[Samsung] 22" [Microsoft] Sidewinder X4 [Silverstone] SFX450w [Silverstone] SG05 
MouseAudioOtherOther
[Microsoft] Sidewinder [Shure] SRH-840 [Shiit] Magni [Schiit] Modi 
CPUMotherboardRAMHard Drive
[AMD] Athlon X3 450 [Asus] M4A785TD-V Evo [Corsair] 4GB XMS3 DDR3 [Western Digital] 200Gb 
Hard DriveOptical DriveOSPower
[Western Digital] 2Tb [Acer] DVD RW ArchLinux [CoolerMaster] Elite 460w 
Case
[NZXT] Gamma 
  hide details  
Reply
post #4 of 6
Thread Starter 
Quote:
Originally Posted by NorCa View Post

You can use any variable of course, now WHY i, j, k... I don't think anyone told me a reason for it, if I had to guess I'd say for loops need an iterator variable(hence the letter), and if you need more than one well keep moving after i. May also refer to int variables in the loop. Anyways... kinda moo point really, just write your code like this and shh him:
Code:
for(int thisIsMyLoopVariable; thisIsMyLoopVariable < thisIsMyOtherVariable; thisIsMyLoopVariable++)
biggrin.gif

lachen.gif Yeah, that was my thought! Personally, I've never done any kind of programming before (unless you count the HTML stuff I did in high school, which I don't, and that was 12 years ago anyway), but in the 4 weeks I've been studing Java, I've come to the conclusion that I would never use a one letter variable in the real world. I could be wrong on this, but it seems to me that it is easier to keep track of if you use discrptive variables, like "thisIsMyLoopVariable" but the prof insists that in loops one letter variables are common and they are usually those variables (i, j, k, etc).

So far I've talked to two programmers here at work and they both said what Accuracy said, they know they've heard it, but they don'tt know why!

Also, to answer you, Accuracy, no, it is not specific to Java, that just happens to be the language we are workoing in.
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
post #5 of 6
i is just the convention for loop counter variables. Since i is used in iteration, j, k, l... come next and we usually use them. There's no specific reason to use these, I think. It's just convention.
The reason, I think, that a, b, c, d, e, f, g, h aren't used as variables is that they are usually used to represent constants in Maths (particularly Algebra).
The reason p, q, r, s, t, u, v, w, x, y, z aren't used as iterator variables because they are used as normal (random? biggrin.gif) variables in Maths (Algebra).
The letters from i to n are conventionally used as variables that represent iterating constants (this feels confusing, I know redface.gif).

I'm pretty sure you'll recognise this pattern if you open up any Algebra textbook. (There may be exceptions, though).
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
MacBook Pro 13"
(6 items)
 
 
Desktop
(13 items)
 
CPUGraphicsRAMHard Drive
Intel i5 3210 @ 2.5 GHz Intel HD4000 4 GB DDR3 @ 1600 MHz 500 GB @ 5400 RPM 
OSMonitor
OSX Mountain Lion 13.3" @ 1280 x 800 
CPUGraphicsRAMHard Drive
Intel i5 480m@2.67GHz AMD Radeon Mobility 5650 4GB DDR3 500GB 
OSMonitor
Windows 7 64bit HP 15.6" 1366x768 
CPUMotherboardGraphicsRAM
E7500 Intel...:( MSI GTS250 1GB 2GB 
Hard DriveOSMonitorPower
250GB Windows XP 17" LG CRT 1280x768@85hz 400W 
  hide details  
Reply
post #6 of 6
Thread Starter 
Quote:
Originally Posted by {Unregistered} View Post

i is just the convention for loop counter variables. Since i is used in iteration, j, k, l... come next and we usually use them. There's no specific reason to use these, I think. It's just convention.
The reason, I think, that a, b, c, d, e, f, g, h aren't used as variables is that they are usually used to represent constants in Maths (particularly Algebra).
The reason p, q, r, s, t, u, v, w, x, y, z aren't used as iterator variables because they are used as normal (random? biggrin.gif) variables in Maths (Algebra).
The letters from i to n are conventionally used as variables that represent iterating constants (this feels confusing, I know redface.gif).
I'm pretty sure you'll recognise this pattern if you open up any Algebra textbook. (There may be exceptions, though).

No, that makes perfect sense, thanks!
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
R2-D2 2700k
(22 items)
 
   
CPUMotherboardGraphicsGraphics
Intel Core i7 2700K P8Z77-V Pro nVidia GTX970 Watercooled nVidia GTX970 Watercooled 
RAMHard DriveHard DriveHard Drive
CORSAIR Vengeance 32GB (4 x 8GB) 240-Pin DDR3 S... Samsung 840 Evo OCZ Agility 3  Crucial MX300 
Optical DriveCoolingCoolingCooling
LG Modisc XSPC Rasa 750 RS240 plus custom loop Danger Den Black Ice SR1 360 RAD Corsair AF SP120 fans (5x) 
OSMonitorMonitorMonitor
Windows 7 Professional 64 Bit Acer S231HL Acer S231HL Acer S231HL 
KeyboardPowerCaseMouse
CM Storm QuickFire XT Stealth with Cherry MX Gr... OCZ ZX 850W Corsair 750D Logitech M510 
Mouse PadAudio
Corsair MM400 Logitech LS21 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Java Question about variables in loops`