• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

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: 10918
87
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: 10918
87
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: 79800
385
  • 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.
 
I've read about this kind of thing at the checkout counter. That's where I met this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic