Input/output statement

Updated: 10/18/2022 by Computer Hope
io statement

An input/output statement or IO statement is a portion of a program that instructs a computer how to read and process data. It pertains to gathering information from an input device, or sending information to an output device.

The syntax of the IO statements will be different based on which programming language is used.

Examples of IO statements

An example of an IO statement is the Python input function, which allows a programmer to get information from a user and utilize it elsewhere.

month = input('What is your birthday month?')

Using the Python snippet above, the program asks for a month, assigns the keyboard input to month, stores that month value in the month variable, and uses it elsewhere.

With the C programming language, the printf function outputs text or other information to the computer screen.

printf("Hello World!");

The C language printf example above would display Hello World! on the computer screen.

If you program with Java, you could use the print function to output data to the Java console.

System.out.print("Good morning :)");

This Java print example displays Good Morning :) in the Java console.

Complete IO program examples

The following complete examples, when executed, output a question (asks for name), assigns the user's response to the username variable, and then outputs a greeting. It looks like this:

What is your name?
Computer Hope
Hello, Computer Hope.

C++ example

Below is an IO example in the C++ programming language.

#include <iostream>
using namespace std;

int main()
{
    string username;

    cout << "What is your name?\n";
    getline(cin, username);
    cout << "Hello, " << username << ".\n" << endl;

    return 0;
}

Perl example

Below is an IO example in the Perl programming language.

use strict;
use warnings;

print "What is your name?\n";
my $username = <STDIN>;
chomp $username;

print "Hello, $username.\n";

Python example

Below is an IO example in the Python programming language.

print('What is your name?')
username = input()
print('Hello, ' + username + '.')

Bash shell script example

Below is an IO example as a Bash shell script.

#!/bin/bash
echo "What is your name?"
read username
echo "Hello, $username."

Go example

Below is an IO example in the Go programming language.

package main

import (
  "fmt"
  "bufio"
  "os"
  "strings"
)

func main() {

  reader := bufio.NewReader(os.Stdin)

  fmt.Println("What is your name?")

  username, _ := reader.ReadString('\n')

  username = strings.TrimSuffix(username, "\n")

  fmt.Printf("Hello, %s.\n", username)

}

Input, Output, Programming terms, Statement