
Every software program is made up of different parts. The constituent parts of these sections are programming statements. There are different types of statements in programming. In this post, you will get to know the types of statements in programming in very simple language.
I'm going to talk about the types of statements in this section. Everything we write in the coding process will be part of one of these types.
In fact, software is created from a set of statements. When these statements are executed consecutively and in the order that we as programmers intended for it, our desired result is achieved.
First, I will talk about simple and complex statements so that you have an overview of the set of statements. After that, we will review the 7 main types of programming statements together.
Types of statements in programming
If we want to categorize all programming statements in two parts; We will have two simple and compound categories:
- Simple Statements
- Complex statements (Compound Statements)
Simple statements are defined to perform simple operations. These statements are usually one line. Assignment operations, variable definition, calculations such as addition and multiplication are simple statements in programming.
Complex statements usually control the execution of other statements or affect their execution process.
Let me separate these two types with a very simple example. Suppose I say to you, "Learn to program!" This is a simple statement. You will easily understand what I am saying and do it. (Of course I hope 🙂)
But if I say "If you like to do extraordinary things, learn programming!" What difference does it make in its implementation?
You first check whether you like to do extraordinary things or not. If this is what you wanted, you will start learning programming.
The sentence I told you with the condition is a complex statement.
In programming languages, the following are considered complex statements:
- Code blocks (to organize statements or define a set of statements)
- Conditional statements
- Repeat loops
Programming Statements
Now that you are familiar with the two main types of statements in programming, you should be familiar with the 7 main types, each of which falls into one of these categories.
The first five are simple statements and the last two (condition and repetition) are compound statements.
Almost a very large percentage of the code we write in any program and in any language is part of these seven items.
Call Statement
This type of statement, which is called Operation Call statement, is the simplest and most basic statement in programming.
Call statements are a type of imperative sentence. For example, in colloquial language, we say to someone "draw a picture".
This imperative statement can take another parameter or parameters. For example, we might say "Draw a picture of me" or "Draw a color picture of a landscape."
In programming, we can also set parameters for calling statements.
These parameters can be mandatory or optional. If a parameter is mandatory, we must define the parameter when calling (writing the statement code), otherwise we will encounter an error. If a parameter is optional, depending on our needs, we can use it in the call or write the statement without it.
There are many call statements in programming. Some of them are part of the programming language and others are part of the features of the operating system.
Function definition in programming
Suppose a person knows four basic operations (addition, subtraction, multiplication and division). Without doing anything, we can instruct him/her to perform a simple calculation with these operators. But if we want him to solve an integral, we should first teach him/her how to take an integral and then ask him to solve a complex problem.
It should be done exactly the same way on the computer. All programming languages can do some things by themselves. For example, perform basic mathematical operations or take an input from the user.
If we want them to perform more complex operations that are not defined for them, we must write a function using the types of programming statements that you will learn about in this post. A function is a set of statements that we can call over and over again.
Most of the calling statements in programming are in the form of input operations or output operations. That is, by making a call, we usually seek to get an input or perform a task in the output of the system.
Definition statement in programming
I'm going to tell you to assume that x
is a positive number. In your mind, an x
is defined without having a specific value. You have only one feature of it and one name.
We call this operation definition operation. It is called a so-called variable, and the method of defining this variable is defined by the definition statement(Declaration).
In programming, it is sometimes possible to define a variable without assigning it a value.
Assignment Statement
After we have created a variable with the definition statement, we may assign a value to it at any time. This work is called assignment.
We may assign different values to a variable many times at different times. This is usually done by putting the equals operator between the variable name and its value. For example, by writing x = 16
, I assign the number 16
to the variable x
.
Simultaneous definition and assignment statement
In some cases, we perform definition and assignment operations simultaneously. For example, I say to you: "Consider the number x
equal to 16
."
In this case, at the same time that you define the variable x
in your mind, you also assign its value to it. This method is mostly used in programming. That is, at the same moment that the initial value for the variable is determined, we also define the variable. (as x = 16
)
Definition statement and assignment statement in programming usually do not have operational output. The only change these statements make is a change in memory. Of course, I mean the output that we as users see, otherwise the changes in the memory can also be a type of output for the computer system.
Return Statement
One of the most important operations in programming is the return statement.
Think you are working with a ATM and you choose the option to declare balance. You have nothing to do with how the device connects to the bank or how your account information is stored by the bank. You just need the result. Finally, it returns the result (account balance) to you.
We found out what the function is in the part of the programming call statement. We usually expect the functions we define to have an output.
Usually, large programs are divided into small parts. Each small part does a part of the main work. These subsections are called Sub-Program or Sub-Routine. In the main program, we run these smaller programs and use their results.
GoTo statement
The goto
statement is used in programming to move between lines. Suppose your program is at line 150, but you want to go back to line 100 and do something again. Or a condition has been established that you have to jump to line 200.
These tasks are done with the help of the goto
statement. In fact, this statement is used in programming to control the execution flow (Flow Control).
This statement may exist in modern and newer languages, but it is rarely used. Instead of using goto
, we use conditional or repetition structures that we will examine below.
Conditional statement (Control statement)
We want to program a system of a site that says hello to users at the top of all pages! If it was day, display the message "Good day" and "Good night" if it was night.
For this we need to check a situation: is it day or night now?!
In the simplest case, to create such a program, we have to say "if
it's daytime, then print the good morning message." And "If
it's night now, then print the good night message."
In fact, we have used a simple condition to check the situation. This statement is called a conditional statement or structure. In most languages, the keyword "if
" is used to define a condition.
When we talk about a condition, we usually have a proposition. In the same example I mentioned, two different propositions (there is a day / there is a night) are used. When this statement is true, it executes the statements defined for it. If this statement is not true, nothing will be done.
Therefore, to use conditional programming statements, there must be a logic in the language.
If you have noticed, we use different conditional structures in colloquial language. For example:
If the weather is mild, then I go to the park, otherwise I stay at home.
The first part is a simple condition using if
. To define the second part, which is somehow dependent on the main condition, we use the else
keyword.
switch and select statements
In some languages, in addition to the if
conditional structure, we have another structure called switch
or select
. These structures allow us to create more complex and longer conditions.
For the same example we said about good time, we can write a program with the following modes using the switch
structure:
- If it was 6 to 11, say good morning.
- If it was 11 to 14, say good afternoon.
- If it was 14 to 18, say good evening.
- Otherwise, say good night
Of course, we can create this program by defining several conditions in a row, but with the select
structure, we will have a cleaner and more readable code.
if(condition){
// do stuff
}else if(otherCondition){
// do something else
}else{
// Default
}
Repeat statement in programming
One of the advantages of using computers is that computers can do the same thing over and over again without any difference. A repetitive task is done with repetition statements in programming.
The repetition statement is also called a loop.
In general, we have two types of loops in programming:
for
loopwhile
loop
There is another type of compound or complex statement that is used to handle exception or error condition. Of course, it is better to say that there is a type of code block for this. I have talked about these types of blocks in the post types of programming errors.