What is a print statement?

A statement in a program that displays the variables, object, or text to the output device (screen) is known as a print statement. The print statement consists of a function that forwards the output to standard output devices such as printers, monitors, etc. Depending on the programming language, the syntax of the print statement differs. The print statement allows the user to print the values of multiple variables in a single statement; however, the variables are separated using a separator (comma, plus, etc,).

Example of a print statement in Python and Perl:

Perl: print “HELLO WORLD !!”;

Python: print (“HELLO WORLD !!”)

Print statementReturn statement
A print statement is a function call from the userIt returns a value from the function.
The print statement does not change the flow of the program.Return statement might change the program flow.
When there is a call for a print statement, the program will write or display the text, value, or object.When the program reaches the return statement, the execution of the current function is stopped and control is sent back to the calling function.
When the user needs to show a value, object, or text in the terminal or output devices, the print statement is used.A return statement is used, when the user needs to send a value from one part to another part of the program.

The syntax of the print statements will vary from one programming language to another. The print statement in various programming languages is as follows,

C

C is the oldest and complicated programming language. The printf() function is an inbuilt library function available in the C library, which is used to print output in a program. The print() function is included in the stdio.h header file. In C, the printf() function is used to display the data using different format specifiers to print string, character, integer, and float-point values to the output screen. To display output in a new line, the “n” character is used (n is the newline character).

Example

#include <stdio.h>

int main()

{

    printf("Display hello world!!"); //print statement

    return 0;

}

Output

Display hello world!!

Explanation: In the above code, <stdio.h> is the header file which contains the printf() and scanf() functions. The program execution starts form main() function and printf() function is used to display the text “display hello world!!”.

C++

In C++, cout object is one of the objects of class ostream. The header file iostream is included in the program to use the cout object. The cout object holds the data to be displayed and << (insertion operator) is used to print multiple variables, values and statements.

Example

#include <iostream>

using namespace std;

int main()

{

    cout<<"C++"<<" Print statement!!"<<endl; //print statement

    return 0;

}

Output

C++ Print statement!!

Explanation: In the above code, iostream header file content is included to use the count object. The cout is used to display “C++ print statement!! ”. The insertion operator (<<) in the cout is used to print multiple statements and values.

Java

In Java, the println() method is generally used to print any string, values, text, etc. The class PrintStream consists of println() function. Java uses three different print statements and they are,

  • print() method: print() method is used to print the data in the console. The print() function is the overloaded function of the class PrintStream. the print() method just prints the data which is inside double-quotes.
  • printf() method: To print formatted string in the console with particular argument and string, then printf() function is used. This is similar to the execution of the format() function.
  • println() method: println() function is the print() method’s upgraded version. The user uses the println() function to print/display the data in the console. Once the statement gets printed, then the cursor moves to the beginning point of the succeeding line.

System.out.println() statement consists of three parts,

  • System refers to a class (belongs to java.lang.package).
  • out is the instance of the System class and is of type PrintStream.
  • println() is a method of class PrintStream, which prints the statements in the console.

Example

class display

{

            public static void main(String[] args) {

                        System.out.println("Java print statement");

            }

}

Output

Java print statement

Explanation: In the above code, "display" is the name of the class. Public static void main(String[] args) is the main method. System.out.println is the print statement used to display the text “Java print statement ”.

Python

In python, print() function is used to display the output on the screen. The syntax of print() function is,

Syntax: print(*objects, sep=’ ’, end=’n’, file=sys.stdout, flush=False)

The parameters of the print statement are,

  • Object: Signifies which object needs to be printed and * indicates that it contains multiple objects.
  • Sep: Used to separate the objects and the default value of sep is ‘ ‘.
  • End: The parameter value that will be printed at last.
  • File: An object with write (string) method. If the value is omitted, the objects is printed by using sys.stdout.
  • Flush:  When the flush is true, the stream is flushed forcibly, however, false is the default value.

Example

print("Python print function example")

aa = 15

print("Value of aa is", aa) # Two objects are passed

bb = aa

print('value of aa = ', aa, '= value of bb')# Three objects are passed

Output

Python print function example

Value of aa is 15

Value of aa = 15 = value of bb

Explanation: In the above code, the object parameter is passed in the print() function. The ‘ ’ separator is used, which provides space between the objects. print() function prints the values of variables aa and bb.

Advantages

  • Simple to add to the program.
  • The program flow will not be changed.
  • Provides concatenation of a string with various data types.
  • During program runtime, it is quite easy to check the program state.

Disadvantages

  • When the program generates multiple outputs then it is difficult to identify what is happening.
  • Additional maintenance point is created at the code.
  • Determination of process sequence is difficult.

Common Mistakes

  • Remember the data to be printed using print statement need to be within the double-quotes.
  • In order to print data with various data types in C , format specifiers should be used.
  • Without << (insertion operator), the user cannot print the information in C++.

Context and Applications

This topic is important for postgraduate and undergraduate courses, particularly for,

  • Bachelors in computer science engineering.
  • Associate of science in computer science.

Practice Problems

Question 1: The ____ is used to send the resulting string directly to the output screen.

  1. Declaration
  2. Assignment statement
  3. scanf() function
  4. printf() function

Answer: Option d is correct.

Explanation: Printf() function is used to send the resulting data or string directly to the output. The print statement is used to display the data in a standard output device. The syntax of the print statements will differ from one programming language to another.

Question 2: In python, the ____ parameter of the print statement is used to separate the objects.

  1. Sep
  2. End
  3. Flush
  4. All the above

Answer: Option a is correct.

Explanation: The parameter sep in python is used to separate two objects and ' ' is the default value. The other parameters of the print statements are end, flush, object, and file.

Question 3: Select the print statement to display the value of xx variable.

  1. printf(“n value of xx is: %d”, xx);
  2. cout<<”value of xx is: ” <<x<<endl;
  3. System.out.println(“value of xx is:  ”+ xx);
  4. All the above

Answer: Option d is correct.

Explanation: All three print functions are used to print the value of the xx variable. System.out.println is used in Java, cout is used in C++, and printf() function in C programming.

Question 4: Which of the following class contains print() and println() methods?

  1. System
  2. System.out
  3. PrintStream
  4. BufferedOutputStream

Answer: Option c is correct.

Explanation: The class PrintStream is used to define print() and println() method. System.out is a byte stream used by print() and println() method.

Question 5: What will be the output of the following program in Python?

Code:

number_list = [2,3,1]

print(number_list)

  1. [2,3,1]
  2. Error
  3. number_list
  4. None of the above

Answer: Option a is correct.

Explanation: In python, a list is one of the built-in data types, which stores several items under a single variable. From the above code, number_list is a list that has some values,  the data present in the number_list is printed on the screen using the print() function. The output of the given code will be [2,3,1].

Want more help with your computer science homework?

We've got you covered with step-by-step solutions to millions of textbook problems, subject matter experts on standby 24/7 when you're stumped, and more.
Check out a sample computer science Q&A solution here!

*Response times may vary by subject and question complexity. Median response time is 34 minutes for paid subscribers and may be longer for promotional offers.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Introduction to Coding

Print statement

Print statement Homework Questions from Fellow Students

Browse our recently answered Print statement homework questions.

Search. Solve. Succeed!

Study smarter access to millions of step-by step textbook solutions, our Q&A library, and AI powered Math Solver. Plus, you get 30 questions to ask an expert each month.

Tagged in
EngineeringComputer Science

Programming

Introduction to Coding

Print statement