C++ Programming/Code/Statements
From Wikibooks, the open-content textbooks collection
[edit] Statements
Most programming languages share the concept of a statement. A statement is a command the programmer gives to the computer. It is also referred to as an expression.
Example
cout << "Hi there!"; // a single statement
Each valid C++ statement is terminated by a semicolon (;). Each statement performs an action. That statement and command will be examined in detail later on, for now consider that it has a verb ("cout") and the other details as information (what to print). In this case, the command "cout" means "send to the standard output stream," (in this case we assume the default, the console).
The programmer either enters the statement directly to the computer (by typing it while running a special program), or creates a text file with the command in it (you can use any text editor for that). You could create a file called "hi.txt", put the above command in it, and give the file to the computer.
If one were to write multiple statements, it is recommended that each statement be entered on a separate line and should end with a semicolon (;).
cout << "Hi there!"; // a statement cout << "Strange things are afoot..."; // another statement
However, there is no problem writing the code this way:
cout << "Hi there!"; cout << "Strange things are afoot...";
The former code gathers appeal in the developer circles. Writing statements as in the second example only makes your code look more complex and incomprehensible. We will speak of this deeply in the Coding Style Conventions Section of the book.
If you have more than one command in the file, each will be performed in order, top to bottom.
The computer will perform each of these commands sequentially. It's invaluable to be able to "play computer" when programming. Ask yourself, "If I were the computer, what would I do with these statements?" If you're not sure what the answer is, then you are very likely to write incorrect code. Stop and check the manual for the programming language you're using.
In the above case, the computer will look at the first statement, determine that it's a cout statement, look at what needs to be printed, and display that text on the computer screen. It'll look like this:
Hi there!
Note that the quotation marks aren't there. Their purpose in the program is to tell the computer where the text begins and ends, just like in English prose. The computer will then continue to the next statement, perform its command, and the screen will look like this:
Hi there! Strange things are afoot...
When the computer gets to the end of the text file, it stops. There are many different kinds of statements, depending on which programming language is being used. For example, there could be a beep statement that causes the computer to output a beep on its speaker, or a window statement that causes a new window to pop up.
Also, the way statements are written will vary depending on the programming language. These differences are fairly superficial. The set of rules like the first two is called a programming language's syntax. The set of verbs is called its library.
cout << "Hi there!";
- Statement Blocks
Also referred to Code Blocks (or in C++-speak, a compound statement), consist on one or more statements or commands that are contained between a pair of curly braces { }. Such a block of statement can be named or be provided a condition for execution. Below is how you'd place a series of statements in a block.
{ int a = 10; int b = 20; int result = a + b; }
Blocks are used primarily in loops, conditionals and functions. Blocks can be nested inside one another, for instance as an if structure inside of a loop inside of a function.
- Program Control Flow
As seen above the statements are evaluated in the order as they occur (sequentially). The execution of flow begins at the top most statement and proceed downwards till the last statement is encountered. A statement can be substituted by a statement block. There are special statements that can redirect the execution flow based on a condition, those statements are called branching statements, described in detail in the Control Flow Construct Statements Section of the book.