Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Pseudo Code Calculator... confused about WHILE loop insertion

pseudo code loop pseudocode calculator

  • Please log in to reply
4 replies to this topic

#1 wolfman

wolfman

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 02 October 2011 - 09:22 PM

Alright, I wanted to create a calculator to get two numbers and a operator symbol for the desired arithmetic. I just reviewed WHILE and FOR loops, but probably over thinking it, and don't understand where to place it to quit the program or where to insert it to check if the user has entered "Q" to quit. I didn't want to use too many IF statements. If you could please review the code to maybe make it more "efficient" that would be helpful.

This is just pseudo code by the way. Just trying to learn the logic and design of programming.

classCalculatorProgram
	main()
	num numberA, numberB
	num operatorSym

	output “Welcome to the Calculator Program”
	output “(+)Addition”
	output “(-)Subtraction”
	output “(*)Multiplication”
	output “(/)Division”
	output “(Q)Quit
	output “Please enter a number“
	input numberA
	output “Please enter an operator symbol for the desired arithmetic”
	input operatorSym
	output “Please enter another number”
	input numberB

	num numResult
	if operatorSym = + then
	numResult = numberA + numberB
	else
	if operatorSym = - then
	numResult = numberA – numberB
	else
	if operatorSym = * then
	numResult = numberA * numberB
	else
	if operatorSym = / and numberB = 0 then
	output “You cannot divide by 0, please enter another number ”
	input numberB
	else
	if operatorSym = / then
	numResult = numberA/numberB

	endif
	endif
	endif
	endif
	endif

	output “You entered,  “ numberA “ “ operatorSym “ “ numberB “ = ”numResult”.”
	
	return
endClass


Should I put an IF/WHILE statement at the very beginning of my IF's to check whether or not the user typed "Q" to quit?

Thanks,

wolfman
  • 0

#2 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 02 October 2011 - 10:04 PM

You wish for it to:
  • Loop at "enter first number" including output of final result, repeat
  • Break if Q is entered in the first input
Therefore, I would have the while loop conditions being while(numberA != 'Q') for a start. before the prompt to tell the user to enter their first number occurs.

Try to give it a go and see if you can do it.

PS: You could check numberA != 'Q' && numberB != 'Q' && symbol != 'Q' to allow them to enter Q in any of the three inputs. You could as well check for Q's case, so 'q' is a valid option as well.

After the while loop you could certainly print "quitting" and assume that message will be reached if they indeed had wished to quit with Q.

Alexander.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#3 wolfman

wolfman

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 03 October 2011 - 12:03 AM

So does this make sense?

classCalculatorProgram
	main()
	num numberA, numberB
	num operatorSym

output “Welcome to the Calculator Program”
	output “(+)Addition”
	output “(-)Subtraction”
	output “(*)Multiplication”
	output “(/)Division”
	output “(Q)Quit

while numberA = Q OR numberB = Q or operatorSym = Q
	output “Quitting“
	
        while numberA <> Q
             output "Please enter your number "
             input numberA
        endwhile

        while operatorSym <> Q
	     output “Please enter an operator symbol for the desired arithmetic”
	     input operatorSym
        endwhile

        while numberB <>Q
	     output “Please enter another number”
	     input numberB
        endwhile
endwhlie

	num numResult
	if operatorSym = + then
	numResult = numberA + numberB
	else
	if operatorSym = - then
	numResult = numberA – numberB
	else
	if operatorSym = * then
	numResult = numberA * numberB
	else
	if operatorSym = / and numberB = 0 then
	output “You cannot divide by 0, please enter another number ”
	input numberB
	else
	if operatorSym = / then
	numResult = numberA/numberB

	endif
	endif
	endif
	endif
	endif

	output “You results are,  “ numberA “ “ operatorSym “ “ numberB “ = ”numResult”.”
        output "Please enter Q to quit or enter another number"
        input numberA
	
	return
endClass

So how bad did I jack this up?
  • 0

#4 Alexander

Alexander

    YOL9

  • Moderator
  • 3963 posts
  • Location:Vancouver, Eh! Cleverness: 200
  • Programming Language:C, C++, PHP, Assembly

Posted 03 October 2011 - 02:53 PM

Think: The while loop can be infinite, so you give it conditions of when to stop, no? while(conditions) ...

You should only check for 'Q' once per iteration of the loop - and the conditions already check for this each iteration. In your program you are checking once, and then again with each input (even though the inputs will be infinite loop, as they will just repeat until Q is pressed)

Instead of checking for conditions within the while loop conditions, you can as well do this:

while(1) {
   if((...)== 'Q') {
      break;
   }
}

The point of pseudocode is to help you understand the logic of the program you will be writing, so there is no need to make it complex, and you do need to understand what you are writing.

An even simpler one to help you understand the point of the while loop:
...

while(first or symbol or second != 'Q') {
      first = get_input();
      symbol = get_input();
      second = get_input();
  
      display_results(first, symbol, second);
}

As you can see, the code will accept input and also display their results. It will as well loop to the top of the loop statement to get more input in case they wish for another equation, until one of them is 'Q' and then it will break automatically out of it.

You will see why the while loop is useful, it will simply go back to the beginning and ask the user for another set of numbers and operators to calculate. If you review your code, it will loop on each individual input and also not do the calculations each loop iteration.
  • 0

All new problems require investigation, and so if errors are problems, try to learn as much as you can and report back.


#5 wolfman

wolfman

    CC Newcomer

  • Member
  • PipPip
  • 18 posts

Posted 04 October 2011 - 12:19 AM

Alright Alexander, I understand what and how the WHILE loop works, it's just the implementation of it that is kicking my butt. I had another go at the pseudo code and I believe it is correct this time. I went with your second example in your last post.

classCalculatorProgram
   main()
	num numberA, numberB
	num operatorSym
	output “Welcome to the Calculator Program”
	output “(+)Addition”
	output “(-)Subtraction”
	output “(*)Multiplication”
	output “(/)Division”
	output “(Q)Quit

        output “Please enter an operator symbol for the desired arithmetic”
        input operatorSym

        while numberA = Q OR numberB = Q or operatorSym = Q        [B][COLOR="#FF0000"]<---- Can I do this here, even if I'm just asking for the input of the operator symbol first? [/COLOR][/B]
        output "Please enter your number "
        input numberA
        output “Please enter another number”
        input numberB

	num numResult
	if operatorSym = + then
	numResult = numberA + numberB
	else
	if operatorSym = - then
	numResult = numberA – numberB
	else
	if operatorSym = * then
	numResult = numberA * numberB
	else
	if operatorSym = / and numberB = 0 then
	output “You cannot divide by 0, please enter another number ”
	input numberB
	else
	if operatorSym = / then
	numResult = numberA/numberB
	
        endif
	endif
	endif
	endif
	endif

	output “You entered,  “ numberA “ “ operatorSym “ “ numberB “ which equals:  ”numResult”.”
        output “Enter another number or Q to quit”
	input numberA
endwhlie

output “Quitting“

	return
endClass


I really appreciate your help. I don't know why I'm over thinking this.

-wolfman
  • 0





Also tagged with one or more of these keywords: pseudo code, loop, pseudocode, calculator

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download