Converting pseudo-code while loop in to java

George Silva :

I am trying to convert a pseudo-code written to java, i have come across a while loop that i am able to understand In the given pseudo-code i am unable to comprehend what both the while loops denote, especially what to key word means.

Does it mean that i need to run the while loop until i is less than a and b variables? I checked all throughout the internet and i was unable to find any pseudo-code example that used such a while loop

 read a;
    read b;
    i=0
    if a>b
    while(to i<a)
    print(i)
    i++;
    end while
    else
    while(to i<b)
    print(i)
    i++;
    end while
    end if;
DevilsHnd :

This is how I see it, Line by Line:

read a;

Get an Integer value from User and place it into a declared int type variable named a. Use the Scanner class with System.in to do this. Be sure to let the User know what is going on and what is expected by way of a prompt. Also, validate the input to ensure a proper integer value is supplied and that the application will not fall into an exception.

Hint: Read up on the Scanner#nextInt() method and Prompting in while loops.


read b;

Get an Integer value from User and place it into a declared int type variable named b. Use the same Scanner class with System.in to do this. Be sure to let the User know what is going on and what is expected by way of a prompt. Also, validate the input to ensure a proper integer value is supplied and that the application will not fall into an exception.

Hint: Read up on the Scanner#nextInt() method and Prompting in while loops.


i=0

Declare a integer counter variable named i and initialize it to 0.


if a>b

Create an if statement code block with (a > b) as its' condition. Yes, be sure your if statement code block has the opening and closing curly braces and your condition is properly enclosed in parentheses.


while(to i<a)

Within the (a > b) if statement code block (within the curly braces) start a while loop with the condition of (i < a). This loop is to continue iterating until i gets to a which at that point the looping ends and any code below the loop can continue to run. Make sure your while loop contains a code block denoted by way of curly braces and make sure your while loop condition is properly enclosed within parentheses.


print(i)

Within the while loop code block (within its' curly braces) write the current integer value of counter i to the Console Window. You can use System.out.println(i); or you can use System.out.print(i + " "); to do this. The latter doesn't make the Console Window scroll so much while it prints all the values of i until it reaches one less than the value of a.


i++;

Add this actual code line directly below the code you used for printing to Console Window within the same while loop code block. This code line increments the counter variable i by 1. It is the very same as writing: i = i + 1; but shorter. So, upon the next iteration of the while loop i will be one more than before when printed to the Console Window. This continues until i is one less that the value of a which was supplied by the User (remember).


end while

If you properly created your while loop code block earlier with open and closing curly braces, then you can basically ignore this line. It is simply the closing curly brace ( } ) for the while loop code block.


else

Directly below the (a > b) if statement's code block (after its' closing curly brace) add an else statement and code block (open and closing curly braces). If the code is to run within this block then the condition within the if statement above was false, a must be less than b instead of greater then b. There is no condition for an else statement.


while(to i<b)

Within the else statement code block (within the curly braces) start a while loop with the condition of (i < b). This loop is to continue iterating until i gets to b which at that point the looping ends and any code below the loop can continue to run. Make sure your while loop contains a code block denoted by way of curly braces and make sure your while loop condition is properly enclosed within parentheses.


print(i)

Within this next while loop code block (within its' curly braces) write the current integer value of counter i to the Console Window. You can use System.out.println(i); or you can use System.out.print(i + " "); to do this. The latter doesn't make the Console Window scroll so much while it prints all the values of i until it reaches one less than the value of b.


i++;

Add this actual code line directly below the code you used for printing to Console Window within the same while loop code block. This code line increments the counter variable i by 1. It is the very same as writing: i = i + 1; but shorter. So, upon the next iteration of this second while loop i will be one more than before when printed to the Console Window. This continues until i is one less that the value of b which was the second value supplied by the User (remember).


end while

If you properly created your second while loop code block earlier with open and closing curly braces within the else statement code block, then you can basically ignore this line. It is simply the closing curly brace ( } ) for the while loop code block.


end if;

If you properly created your code block for the else statement earlier with open and closing curly braces then you can basically ignore this line. It is simply the closing curly brace ( } ) for the else statement code block. If there was no else statement then it would simply be the closing brace ( } ) for the if statement.


You should hopefully have no problem writing this code now.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=370879&siteId=1