• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

For loops and Arrays

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok. I am totally new to programming and I'm starting a project. I'm not even really sure how to ask the question but here it goes.

I have an array. I want to use a loop to go through and look at each individual element in the array compare it to a number then if its greater than, equal to, or less than that value, do a math operation to that variable. After the loop runs the variable will be set to the number it should be after each individual check is done.

As the loop runs it may add 1 to the value on the first element then on the second element it may subtract 1 from the value and on the 3rd it may do nothing to the value. I dont want the value of the variable to be overwritten each element, only adjusted from its current value on each loop.

this is my attempt at it but i'm having a difficult time wrapping my head around loops and arrays.

thanks in advance for any guidance.

example:

 
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Erick Fenstermaker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you I will try these changes and see if I can get this to work.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Sorry for the last coment, first time comenting  here.
Couldnt find a away to edit it yet.
 
Carey Brown
Saloon Keeper
Posts: 10705
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Eliminating braces, especially for beginners, is highly discouraged. Braces help eliminate, or at least point out, some common programming errors. DON'T DO IT.

If you were to eliminate the braces, you wouldn't need the for() loop braces either (in this case). Either way, proper indentation is vitally important for readability.

 
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome t the Ranch

Carey is right: make sure to write all the braces. And indent everything correctly. Your lines 7‑8‑9 can cause no end of confusion because they are indented incorrectly. And you will have realised that keywords are lower‑case throughout: else not Else.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic