- Computers & Software»
- Computer Science & Programming
How to Write A Line Of Code In C++ Programming Language And Some Other Basic Computer Programming Concepts
This Information Is Particularly Useful For Computer Science Students
For those of you that are Computer Science majors you will find this article a useful start in the development of your knowledge. I’d like to take the opportunity to say that I’ll be writing tutorials for computer programming for those that are interested in resources such as this. This one will address how to write a simple program for the C++ programming language. And the next part of this article will discuss some of the basic concepts in computer programming.
In C++ Details Really Matter
Writing a simple program does not take many steps actually. First of all since this is a simple demonstration for how to do this, there won’t be too many lines of written code. For the more complex tutorials that may be coming in the future, there will be more lines of code and a more detailed explanation. For now, I just want to introduce those new to programming that C++ like other programming languages is case sensitive. Details really matter in computer science. For example, the character a is different from the character A. One of these is an uppercase letter and the other one is a lowercase letter.
A C++ Photo
A Basic Line Of Code In C++ And Explanation of the Functions
Okay, now since that is done, let me say that there are a few statements that you must have before you start coding.
using namespace std;
Include iostream tag in C++ is designed so that you can type input and have output printed onto the screen. Then you can have comments. Comments in a program explain to the persons who are reading the program what is going on. It makes others follow along easier. There are single line comments and multi line comments. Single line comments have a format like this:// comments. Here is a simple one line program in C++:
using namespace std;
cout << “This demonstrates what goes on in a C++ program.”;
The cout statement allows the end user or the person typing the program to enable the compiler to read output. The system pause command prevents the program from aborting abruptly and you can see what kind of output will be displayed on the screen. The return statement must be in every C++ program. There are no exceptions. The opening curly brace is there to tell the compiler that the main function is now running. The closing curly brace tells the compiler that this marks the end of the function. All functions in C++ must have opening and closing curly braces to indicate the beginning and end of the main function.
As you see, this is just a basic program. The more complex programs will have more functions and more lines of code.
Programming Language Cloud
The Purpose of Pseudocode and Comments In Computer Science
In computer programming, there are certain steps that every programmer should complete before they actually start the writing or coding process of the program. One of the first things that I suggest that you do is write down what is called pseudocode. Pseudocode is basically short phrases written in English and these phrases describe the algorithm of the program that you are working on. An algorithm is a series of instructions that are written which enable the programmer to complete a specific task or to solve a problem in a finite amount of time. Some programs will take longer than others to complete depending upon their complexity. Pseudocode makes everything easier because when you write pseudocode before you start coding, you have a better general idea of what steps to take to complete the program. I would like to consider pseudocode as the blueprint of the program. When you start writing the pseudocode, you can use the symbol /*. And then, when you are finished with that part, you can end the pseudocode section with the symbol */. Pseudocode and comments are ignored by the compiler.
Writing comments during the coding process of the program gives other programmers a better idea of where the program is going. It helps make your program more readable. Programmers can then understand what you are doing. When a programmer is busy coding and writing the program, it is their responsibility to be able to come up with an algorithm that solves the task correctly and efficiently. Just compiling the program is not good enough. You also must run the program to make sure that it displays the output that you would like to see on the screen. If you run the program and then you see that it does not display the correct output on the screen, this is where a logic error can occur. Getting the program to run efficiently and display the correct output is very challenging because when you write the program, you have to have a solid general idea of the concepts that you are using to complete the task. And it takes lots of practice to learn the concepts in C++, Java or any other programming language. In other words, to say this in more simple language, in order for your program to be complete, it must compile properly and be free of any syntax errors and it must also run correctly and display the proper output on the screen. Do not think that just because your program compiles correctly then it is done and complete. Your program must do both steps just described. Every method or function as they are known in C++ must have a main function.
How Variables Work and How to Declare Them
After you have declared the main function in the program, you can write a comment saying that you want to declare the variables. A variable is something that has a name and that it describes a certain value. But first you must assign a specific data type to the variable. Some examples of variable declarations are:
A variable can always be changed during the execution of a program unless you decide to declare the variable as a named constant. If you decide to declare the variable as a named constant, then it cannot be changed during the execution of a program. A variable name cannot begin with any numbers, special characters such as a dollar sign, number sign or exclamation point. A variable name also cannot have one of the reserved key words of the programming language such as while, return or do-while. And another thing to remember is that when you declare a variable make sure to do it using lowercase letters.
When you declare your variables in your program and assign values to them, make sure that you write the value after the name of the data type. For example, you cannot declare a variable like this: 25 = int;
If you do it that way, you will get a syntax error. So make sure to declare the variable and assign the value like this instead: int = 25;
And as always, do not forget to put the semicolon after the variable has been declared and assigned the value. A lot of programmers make this mistake. But as you get better at programming, you will make fewer errors. Remember that it takes patience and lots of practice. Everyone makes mistakes especially in coding. The key to success in Computer Science is to be patient while coding! Good luck!