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 7 of 7

Thread: Do while loop in CeaserCipher

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Do while loop in CeaserCipher

    Hello friends long time no see! I've got a really simple question this time around and I'm not sure if im just oblivous or what. The question is in regards to this do whie loop.

     
    public class test {
    	public static void main(String[] args) {
    		char input, encrypted;
    		int distance;
     
    		input = In.readChar();
     
    		do {
    			Out.println("input number between -10 and 10 without 0");
    			distance = In.readInt();
    		}while(distance < -10 || distance > 10 || distance == 0);
     
     
    	encrypted = (char) ((input + distance % 128));
     
    	Out.println(encrypted);
    	}
    }

    Now If you look the text of the print statement and the while statement it (in my eyes) doesn't seem right. I'll try to explain what I see:

    read in the values while the value is less than -10 or higher than ten or equal to null. Now the program works I've tested it but I obvioulsy dont have a good grasp of the do while loop (even tho I've used it before), so some explanation would be great.Thanks!

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

    Default Re: Do while loop in CeaserCipher

    read in the values while the value is less than -10 or higher than ten or equal to null
    An int is a primitive numeric variable. It can not contain null. I assume you mean 0 which is a valid value. A null value in a reference variable means that the variable does not reference an object.

    If the null is changed to a 0, then that looks like a valid description of what will keep the do{}while loop iterating.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop in CeaserCipher

    Yea I mean 0, my bad, I know its valid but it confuses me,as to how can we put distance > 10 because the values need to be within - 10 and 10, but this says greater than 10 or am i just that oblivious?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Do while loop in CeaserCipher

    how can we put distance > 10
    The loop will iterate if distance > 10. If you want to allow values > 10, remove that condition from the while.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop in CeaserCipher

    Okay, is there a way you could write comments in my code explaining step by step what the loop those? That would probably clear it up for me.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,162
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: Do while loop in CeaserCipher

    Your statement explains what keeps the loop iterating:
    while the value is less than -10 or higher than ten or equal to 0
    If any of those conditions are true, the loop execution goes back to the top of the loop.
    If they are all false, execution leaves the loop.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: Do while loop in CeaserCipher

    Omg now I see, thank Im just not on my game,I'm missing the easiest thing just not my day.Thanks buddy!

    PS. I passed my programing curriculum with an B.

Similar Threads

  1. loop once or loop multiple times
    By stanlj in forum Loops & Control Statements
    Replies: 3
    Last Post: November 7th, 2013, 02:14 PM
  2. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  3. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 11:22 PM
  4. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 08:57 PM
  5. hi. i want to rewrite this do loop into a while loop.
    By etidd in forum Loops & Control Statements
    Replies: 3
    Last Post: January 26th, 2010, 05:27 PM