Statements are how we tell the compiler what we want our program to do. In other words, they are the instructions written in C++ code. A statement may be a simple instruction to invoke a function call, such as:
foo();
Or to perform an operation, such as adding two integers:
x+=y;
Note that all statements end with a semi-colon.
We can also group individual statements together to form a compound statement. For instance, when we use an if statement to evaluate a condition, we might want more than one statement to execute if the condition were true. We use curly braces to create a compound statement:
if (x==42)
{
for(int i=0; i<100; ++i)
std::cout<<i<<std::endl;
foo();
}
In the above example, if x were 42, then we'd print the number 0 to 99 and then call foo(). If x were not 42, then we'd skip over the entire compound statement and execute the next statement instead.
Compound statements may also be nested. In the above example, for instance, the for loop might contain a compound statement:
if (x==42)
{
for(int i=0; i<100; ++i)
{
std::cout<<i<<std::endl;
foo(i);
}
}
In this case, we print the value 0 and then call foo(0) on the first iteration of the loop, then print 1 and call foo(1), and so on. But since the for loop constitutes the entire compound of the if statement, we can eliminate the outer set of braces completely:
if (x==42)
for(int i=0; i<100; ++i)
{
std::cout<<i<<std::endl;
foo(i);
}
We can also create compound statements using commas to separate the individual statements. For instance, when we delete a pointer we will typically nullify the pointer straight away. Like so:
if (p)
delete (p), p=NULL;
The above is simply a shorthand for the following compound statement:
if(p)
{
delete(p);
p=NULL;
}
For example: <expression>, <statement-block>, <null-statement>, switch, if, while, for, return, break, continue, try, catch...
x = 12;
The semi-colon converts a C++ expression into a statement.
Statements that check an expression then may or may not execute a statement or group of statements depending on the result of the condition.
Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }
for( ; ; ) { statement_block; } while( conditional_expression ) { statement_block; } do { statement_block; }while( conditional_expression )
Control statements are statements that alter the flow of execution according to the evaluation of an expression (the condition). The C++ control statements are ifstatements, switch statements and the tertiary conditional operator, ?:.
There is no difference. Both statements are invalid.
x = 12;
The semi-colon converts a C++ expression into a statement.
Decision making statements make use of conditional expressions. In C++ there are three possibilities: if/else, switch/case and the ternary operator (?:).
Control instructions are instructions that alter the flow of execution. In C++ this include if, if-else statements, switch-case statements and the conditional ternary operator (?:), as well as loop structures (for, while, do-while) and procedural goto statements.
Statements that check an expression then may or may not execute a statement or group of statements depending on the result of the condition.
There are several 'looping' statements in C++. They are:while () { }do { } while () ;for (index-start, index-end; index increment/decrement) { }They are used to repetitively execute statements as long as the statement(s) controlling the loop are true.
Unconditional statements are statements that are invoked unconditionally. Conditional statements have a controlling expression, while unconditional statements do not. For example: void f (bool b) { if (b==true) do_something(); // conditional statement (controlled by the expression b==true) do_something_else(); // unconditional (executes regardless of b's value) }
for( ; ; ) { statement_block; } while( conditional_expression ) { statement_block; } do { statement_block; }while( conditional_expression )
All statements must be terminated with a semi-colon in C.
The C and C++ library routines for output might, or might not, include use of different buffers. If they are the same buffer (unlikely) then you can simply intermix the techniques. If they are not the same buffer then you need to do a flush sequence between techniques.