A statement is a command given by the programmer to the computer program. Statements are used by different computer programming languages, such as Java, C, and C++. The syntax for declaring and using statements varies in each language. A simple example of a statement can be:
println “Hello ! I hope you are enjoying your stay here”
The statement uses a set of words “Hello! I hope you are enjoying your stay here?” and other details, such as what will be printed on screen. The command println tells the computer to show the output on the monitor and not on the printer. The computer understands this command in the following two ways:
The programmer can give the command to the computer in the running mode, as the program is being executed.
They can also store a statement or a set of statements in a text file and execute it as and when required. This will then save the task of executing the statements again and again. For example, the command we just saw can be saved in a file Hello.txt and give this file to the computer.
As stated above, you can also save a set of multiple statements in a text file, which will then be executed in the top to bottom order. This means that the statement on the top will be executed first, followed by all other statements in that order.
Continuing with the above example, the file “Hello.txt” can contain multiple statements in a single file:
println “Hello! I hope you are enjoying your stay here.”
println “How was the dinner yesterday?”
println “Enjoy your lunch.”
As stated above, each of these commands will be performed from top to bottom in a sequential manner. On seeing the text file, the computer:
- Reads the first statement from the text file
- Determines if it is a println statement
- Determines the text that needs to be printed
- Displays the text on screen.
Therefore, the first command to be displayed on the screen would be: Hello! I hope you are enjoying your stay here.
The last statement would be: What is the duration of the course
The question marks that you saw in the code are not displayed on the computer screen. These punctuation marks are used to tell the computer where the statement would begin and end respectively. The computer then reads the second line, followed by the third line and displays the output in the following manner:
Hello! I hope you are enjoying your stay here.
How was the dinner yesterday?
Enjoy your lunch.
The computer stops reading the file once it reaches the end of the file. It is important to provide correct code in these text files, otherwise the code can also be erroneous.
Different programming languages use different types of computer statements. For example, a language can use a beep statement directing the computer to produce a beep in its speakers. Similarly, a window statement can direct the computer to popup a new window.
Not only this, different programming languages display and code statements differently. The set of rules such as println used to describe a statement is known as its syntax, while the actual message that needs to be displayed in known as its library.
Therefore, we can say that statements are used to change the value of variables, process input, display the desired output, or produce an action in response to user mouse or keyboard actions.
Different types of statements that we will commonly come across in the Java are:
- Assignment Statements
- Message Statements
- Statement Blocks