Input and Output
Communication with users is essential in any program. Common Pseudocode provides simple statements for reading input and displaying output.
Output Statement
Section titled “Output Statement”Display information to the user with the output statement:
output "Hello, World!"Outputting Variables
Section titled “Outputting Variables”Display variable values:
name = "Alice"age = 30output nameoutput ageConcatenating Output
Section titled “Concatenating Output”Combine text and variables:
name = "Bob"age = 25output "Hello, my name is " name " and I am " age " years old"String Interpolation
Section titled “String Interpolation”Embed variables directly in strings using curly braces:
name = "Charlie"score = 95output "Player {name} scored {score} points!"Multiple Output Statements
Section titled “Multiple Output Statements”output "Welcome to the program!"output "" // Empty lineoutput "Please select an option:"output "1. Start"output "2. Exit"Input Statement
Section titled “Input Statement”Read user input with the input statement:
input userInputThe value entered by the user is stored in the variable userInput.
Prompting for Input
Section titled “Prompting for Input”It’s good practice to tell users what to enter:
output "What is your name? "input userName
output "How old are you? "input ageSingle Line Prompt and Input
Section titled “Single Line Prompt and Input”output "Enter your email: "input userEmailComplete Example
Section titled “Complete Example”Here’s a full program that uses input and output:
// Greeting programoutput "=== Welcome ==="output ""output "What is your name? "input userNameoutput "What is your age? "input userAge
output ""output "Hello, {userName}!"output "You are {userAge} years old."
// Calculate birth year (approximate)currentYear = 2024birthYear = currentYear - userAgeoutput "You were born around {birthYear}."Menu Example
Section titled “Menu Example”Creating an interactive menu:
output "Options: "output "1. Book a ticket"output "2. Cancel a ticket"output "3. View a ticket"output "4. View all tickets"output "5. Track train"output "6. Track all trains"output "9. Exit"output ""output "Please enter an option: "input userOption- Always provide clear prompts so users know what to enter
- Use empty output statements (
output "") to create blank lines for readability - When reading numbers, remember to convert string input to number if needed
- Consider validating user input to handle errors gracefully
output "Enter a number: "input userInputnumber = convert userInput to number
if is number(number) output "You entered: {number}"else output "That's not a valid number!"