Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: variable in for loop

  1. #1
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default variable in for loop

    Hi there,
    I'm very begginer at Java and I want to modify a code. Here is the entire code:
    var NodeName1 = "Curve1"; 
    var NodeName2 = "Curve2"; 
    var NodeName3 = "Curve3"; 
    var NodeName4 = "Curve4"; 
    var NodeName5 = "Curve5"; 
    var NodeName6 = "Curve6";
     
    var count = ag.fm.FeatureCount;
    var num1 = 0;
    var Yes = agc.Yes;
    for (var i = 0; i < count; i++) {
     
      var current = ag.fm.Feature(i);
      var Name = current.Name;
     
      if (Name.toLowerCase() == NodeName1.toLowerCase()) { 
        current.CurveRefresh = Yes; 
        num1 += 1;
      } else if (Name.toLowerCase() == NodeName2.toLowerCase()) {
        current.CurveRefresh = Yes;
        num1 += 1;
      } else if (Name.toLowerCase() == NodeName3.toLowerCase()) {
        current.CurveRefresh = Yes;
        num1 += 1;
      } else if (Name.toLowerCase() == NodeName4.toLowerCase()) {
        current.CurveRefresh = Yes;
        num1 += 1;
      } else if (Name.toLowerCase() == NodeName5.toLowerCase()) {
        current.CurveRefresh = Yes;
        num1 += 1;
      } else if (Name.toLowerCase() == NodeName6.toLowerCase()) {
        current.CurveRefresh = Yes;
        num1 += 1;
      } else if (num1 == 6) {
        break;
      }
    }
    agb.regen();
    Actually i want to make this code as short as possible... maybe using for loop to prevent from repeating some statements... can anyone help me?

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: variable in for loop

    One reduction would be to only call Name.toLowerCase() only once and save the results in a variable that would be used in the if statements instead of repeatedly calling it.
    Also define if NodeName1 etc does not require mixed case, define it with lower case and remove the calls to toLowerCase()
    Another change might be to use a switch statement instead of if/else if statements.
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    safasml (January 20th, 2021)

  4. #3
    Junior Member
    Join Date
    Jan 2021
    Posts
    2
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: variable in for loop

    Dear friend(s),
    Honestly, I’m not a java programmer and I know nothing but a little about java… if anyone can do this for me (I mean rewriting the code to be shortened) I’ll greatly be appreciated…
    Also, I don’t know if we can do something with the first part of the code:
    var NodeName1 = "Curve1"; 
    var NodeName2 = "Curve2"; 
    var NodeName3 = "Curve3"; 
    var NodeName4 = "Curve4"; 
    var NodeName5 = "Curve5"; 
    var NodeName6 = "Curve6";
    for example, defining a for loop or something else… many thanks

Similar Threads

  1. do while loop, variable can't be found
    By boobology in forum Loops & Control Statements
    Replies: 4
    Last Post: November 6th, 2013, 01:32 AM
  2. Please help issue with a variable in a loop.
    By Zebo999 in forum What's Wrong With My Code?
    Replies: 1
    Last Post: September 29th, 2013, 04:05 PM
  3. [SOLVED] While loop won't allow variable to change
    By Thor in forum Loops & Control Statements
    Replies: 13
    Last Post: October 9th, 2012, 08:35 PM
  4. For-loop: initialisation of variable, can't set variable to value zero?
    By Bitbot in forum Loops & Control Statements
    Replies: 4
    Last Post: July 15th, 2012, 02:32 PM
  5. Variable updates after loop
    By tecno40 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 9th, 2011, 05:34 AM