How to use While and IF Statements in Visual Logic

megan1989

Baseband Member
Messages
29
Location
USA
Write a program that reads in a list of positive integers from the user
until the user enters the value –1 as a sentinel. At that time the program should display the largest value in the input list.

So I think I have the first part figured out, ending when you input -1. The problem that I am having is figuring out how to make the visual logic program give me the largest value at the end of the loop. Anyway this is what it looks like:

Begin:
Input: Number (and in the input statement I have it saying "Pick a Number")
While Statement: TRUE>true (I have nothing routing into the false)
From the true statement it goes into a IF statement
IF Statement: Number = -1> if true it goes to OUTPUT: "The largest Number was "
and if it is false to create a loop I put another input statement
INPUT: Number (and in the input statement it again says "Pick a Number")

I know that it can't be too complicated, I am probably just overthinking it, thanks!
 
That'd work, but you are over-thinking it. for a little pseudo code, i'd do this:

Code:
//Initialize loop/input variable
integer number = 0;
//initialize max number variable
max = -1;

while (number != -1)
{
    prompt for number;
    number = user input;
    if (number > max)
    {
        max = number;
    }
}
print: max;
 
Thank you for the pseudo code, but my main problem is when the loop is concluding(after you input -1) I need it to give me largest number that was inputted, so far I haven't added that into the program as I have been unsuccessful.
For instance if the history is this: 1, 45, 56, 4, 0, -1, I need it to give me 56 in the final output "the largest number was 56." I was thinking to input an assignment or another while statement into the program, but I have been stuck on how to do that
 
Last edited:
The pseudo code i gave you would do that. If you look again, you'll see I initialized a second integer (although I didn't expressly call it an integer) with a value of -1 (so your first value will be larger, guaranteed). Within the loop it checks to see if the user's input was larger than the currently saved max, and if so saves it. Once it's done (user types -1) it'll print out the max and you can add whatever text you'd like.
 
Okay, I think that I have got a bit of a grasp on this. I think what is causing me a problem is how to initialize the second integer which would then initialize keeping the largest number in its memory. Would I use an input statement in the beginning or later on?
 
You would not need any additional input statements as I've written it. the number used to save the max is initialized to -1 so the user's first attempt (assuming they don't immediately enter -1) will be the max and the if statement that's nested within will validate & update this saved number as appropriate.
 
Okay, thanks for helping me through this problem thus far. Right now, I've edited my original set up to:
Begin:
Input: Number (Pick a Number)
Input: Max=-1
While Statement: Number > Max
routes to true, nothing to false.
IF statement: Number > Max
if this statement is true:
Output: "The Largest Number was." &Number
if the IF statement is false it then routes to:
Output: "Please try again"
Input: Number (Pick a number)

What this is doing is failing to go through the false statement for the IF statement and gives me what I entered for the first input number statement. i.e I enter for pick a number 8, then -1 for max it gives me
The largest number was 8 and ends.
If you could elaborate on where I am going wrong I would appreciate, as I have applied your pseudo code the best that I could. Thanks
 
I see something in your description that doesn't make sense to me so I'm going to ask you to do one of 2 things.

First option is to paste your code here so we can look at it and see what you're doing. The second option would take a little more work on your part BUT you might find it beneficial. What I'm thinking as the second choice is you post a walk through of my code and state what happens line by line when the user enters options of 3,8,5,-1.
 
Okay, let's see if this makes a bit more sense:
Following your code inputting it into the visual logic, I will do my best to make this as clear as I can.

Input: Number>3
Input: Max>-1
While Statement: Max = -1, the number =-1 routed to false and ended the program
Input: User_Input>3 (the next input statements are the program routing back to this statement so I can input the 3,8,5, -1)
Input: User_Input > 8
Input: User_Input > 5
Input: User_Input > -1
This continues to prompt for the user_input, I am pretty much stuck on how to make it give me 8 as the largest. I have toyed with using placeholder scenarios using assignment operators but again I am getting nowhere. I hope this helps to clear up some confusion. Thank you
 
Max = -1, the number =-1 routed to false and ended the program

How and why? the first time it hits the while loop (before the user has even had a chance to enter anything) it's comparing 0 != -1 which is true so it'll fall into the loop and prompt the user for the first input. I wonder if you're looking at my initialization as inputs (which is why I put comments in the code to try and prevent)

So I'll walk through the while loop now a few times and show line by line what is happening:


Code:
while (number != -1) /* translates to 0 != -1 as the user hasn't entered anything yet.  True statement so go into the loop*/
{
    prompt for number;
    number = user input;  /* number is now set to 3 */
    if (number > max) /* translates to 3 > -1.  This is true*/
    {
        max = number; /* max is now set to 3
    }

while (number != -1) /* Now this translates to 3 != -1, still true so let's loop again. */
{
    prompt for number;
    number = user input; /* User enters 8, number now equals 8 */
    if (number > max) /* translates to 8 > 3 which is again true */
    {
        max = number; /* max is now assigned the value of 8 */
    }
}

while (number != -1) /* we're now at 8!=-1 which is still true, loop again. */
{
    prompt for number;
    number = user input; /* user has entered 5.  The number variable now is set to 5.
    if (number > max) /* checks 5 > max, this is false. */
    {
        max = number; /* LINE SKIPPED because the if-statement was false*/
    }
}

while (number != -1) /* 5 != -1 so it loops again. */
{
    prompt for number;
    number = user input; /* user enters -1, number is now equal to -1 */
    if (number > max) /* -1 > 8 is false, do nothing.
    {
        max = number;  /* LINE SKIPPED */
    }
}

while (number != -1) /* -1 != -1, this is finally false.  skip the entire contents of the while loop.
{
 /* code deleted, as we're skipping it all */
}

Output:  "Your max number was 8"
}

If this still doesn't help, please post your code.
 
Back
Top Bottom