Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Junky

Thread: Can anyone explain this logic?

  1. #1
    hotsauce is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default Can anyone explain this logic?

    This part of the program takes a string and print each letter on a single line, while speaking the string backwards.


    To termination the For loop in the printBackwards method it's n>= 0. I originally thought it would be n==0, but using n==0 the compiler just terminates the program. I was only able to get it to work through trial and error (n>=0).


    Java Code:
    	
    
    public static void main(String[] args) {
    		
    		String word = "Backpack";
    		
    		printBackwards(word);
    
    	}
    
    public static void printBackwards (String s){
    	
    		for ( int n = length(s) - 1; n >= 0; n--){
    			System.out.println(s.charAt(n));
    			
    		}
    	}
    Output:
    Java Code:
    kcapkcaB
    Last edited by hotsauce; 08-31-2011 at 11:01 AM.

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,755
    Rep Power
    25

    Default

    Add a println to show the values of the variable n as the loop executes.

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,800
    Rep Power
    7

    Default

    Quote Originally Posted by hotsauce View Post
    I originally thought it would be n==0
    If you used that as the condition it means that the loop would only execute if n IS zero. There are 3 possibilities.

    Initial value of n is zero. Condition is true. Execute loop. Change value of n. Condition is false. Exit. Loop only executes once which is pointless.

    Initial value of n is zero. Condition is true. Execute loop. Never change value of n. Condition is true. Endless loop. Not very useful either.

    Initial value of n is not zero. Condition is false. Never executes loop. Yet again a pointless loop.
    hotsauce likes this.

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

  6. #6
    hotsauce is offline Member
    Join Date
    Aug 2011
    Posts
    3
    Rep Power
    0

    Default Re: Can anyone explain this logic?

    No, I'm still here. Didn't have internet for awhile.

    Thanks for the explanation, it really helped.

Similar Threads

  1. Need help in logic
    By nn12 in forum New To Java
    Replies: 3
    Last Post: 03-23-2011, 07:44 PM
  2. Need help on logic
    By nn12 in forum New To Java
    Replies: 6
    Last Post: 03-10-2011, 12:06 PM
  3. Replies: 2
    Last Post: 12-13-2010, 02:34 PM
  4. Replies: 3
    Last Post: 12-13-2010, 08:22 AM
  5. Cant get the logic right
    By jermaindefoe in forum New To Java
    Replies: 4
    Last Post: 03-11-2008, 01:22 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •