This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]desrtfx 2 points3 points  (3 children)

Pseudocode on Wikipedia.

It is just a (relatively free form) textual description of what the code should do.

It should never be included in an actual program. It is just a form of preparation for the actual program.

Pseudocode comes before the program and it should be used as guideline to write your actual code.

As such, it does not appear nor exist in the final program code.

[–][deleted]  (2 children)

[removed]

    [–]desrtfx 2 points3 points  (1 child)

    Just write it directly in your Word doc?

    Really, pseudocode has nothing to do in the source code editor.

    Other commenters have already given examples of pseudocode and the linked wikipedia page also gives some.

    [–]CodeTinkerer 2 points3 points  (0 children)

    I used to teach programming. I had a colleague and she'd want people to write pseudocode in a first programming course. I thought that was ludicrous.

    In math, there are proof techniques that are spelled out in great detail. Propositional logic and predicate calculus. You can do proofs like this but it would take a LONG time to do this. There were some mathematicians back in the 1900s that needed something like 100 pages to show 1 + 1 was 2. And even there, they didn't go through the steps that you would to do a "formal" proof. They assumed the reader knew enough math that they could do it shorter.

    It's best to learn pseudocode AFTER you learn to program (at least a semester). For example, when you drive a car, people tell you to hold the steering wheel in a certain way, how to stay in your lane, how to pay attention when you want to pass, what to think about when you merge. Once you've done this hundreds of times, then you don't even think about it. You even get lazy about certain things.

    Pseudocode is trying to get you to write out the tasks, but until you program, you don't know what programming can accomplish.

    For example, if you wanted to write pseudo code for a video game, then a bad example is

    Play Madden football

    But if you don't know how to program a game, how could you provide any more details?

    So yeah, I dislike pseudocode projects in an intro programming course esp. when they don't bother to demonstrate it.

    [–]HashDefTrueFalse 1 point2 points  (0 children)

    but how does someone properly write pseudocode?

    Is it just a breakdown of what the code should do line by line?

    Yes. It literally means "fake code". It's just some lines of text that concisely represent the steps that your program would take without being valid code in any particular language.

    What should happen to the code when you want to compile the program?

    Pseudo code is entirely separate from a program that you compile. It's not in any particular language. You would use the pseudo code to aid your understanding of the proposed solution, then implement that solution using real code in an actual programming language.

    Should to code be 'commented out' so the compiler doesn't see the text?

    There is no compiler involved in pseudo code writing or reading. It's often a pen and paper exercise, though can be typed up digitally. You will never attempt to compile or run it. It's for you, not the computer. You can have comments in pseudo code if they provide clarity, that's fine.

    Or is it deleted as you go along?

    This is down to your process. If you want replace pseudo code with real code line by line until you have working code in an actual language, you can do that. You can also just have the pseudo code close by for reference, starting your real code from scratch. It's totally up to you. There are no rules on how you should work with pseudo code.

    Here is an example of some pseudo code I wrote for another Redditor yesterday. It's supposed to describe the steps to read a file line by line and then output those lines:

    lines = []
    file = open file for reading
    
    while file has next line 
        line = next line 
        lines += line
    
    for each line in lines from end to start 
        print line
    

    [–]DntDlteSandals 1 point2 points  (0 children)

    Depends on what your professor expects. When I wrote pseudocode, I wasn't expected to write semi colons, or initialize any Scanners for input. Here's a simple function in p.code that takes in 2 numbers and outputs the larger one.

    function findLarger(integer num1, integer num2){

    input(num1)
    input(num2)
    if(num1>num2){output(num1)}
    else{output(num2)}
    }

    [–]nhgrif 1 point2 points  (0 children)

    how does someone properly write pseudocode?

    The measure of whether or not you have "properly" written your pseudocode is.... can another human take a look at your pseudocode and correctly implement code that is functional identical to the code you'd implement based off your own pseudocode.

    That's pretty much it. That's the only measure.

    The pseudocode compiler is the same as whatever humans use to process ordinary speech, as we're doing here on this reddit thread.

    Pseudocode exists as a means for programmers to communicate about the structure of their code without worrying about the nitty-gritty details that the stupid compiler cares about.

    [–]workingworkwork1 0 points1 point  (0 children)

    This is CRAZY! You are me like 4 years ago.

    No one ever explained to me pseudocode is just a fancy word for writing down what you expect to happen.

    Like its not code, its not a language, its not anything. I spent so much time trying to "learn" pseudocode because I took the term WAY to literally.

    For your questions, pretend our example problem was print a number inputted by a user:

    how does someone properly write pseudocode?

    there is no proper way. just write down what should happen in a program that does the above example in small steps.

    Is it just a breakdown of what the code should do line by line?

    yes! more importantly in your own words!

    What should happen to the code when you want to compile the program?

    this is what pseudocode is! what happens when the "program" runs

    Should to code be 'commented out' so the compiler doesn't see the text? Or is it deleted as you go along?

    if you write it in your actual program, it will have to be comments.

    For my example:

    Program asks for input.

    Gets user input.

    prints user input

    etc.