answersLogoWhite

0

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;

}

User Avatar

Wiki User

11y ago

What else can I help you with?

Related Questions

Control statement in c plus plus?

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, ?:.


What is the difference between statements in c plus plus charconstp and char constp?

There is no difference. Both statements are invalid.


What c plus plus statements assign x the value 12?

x = 12;


What is the character used at the end of executable statements in C plus plus?

The semi-colon converts a C++ expression into a statement.


How to make decision making statements in c plus plus?

Decision making statements make use of conditional expressions. In C++ there are three possibilities: if/else, switch/case and the ternary operator (?:).


Explain control instructions in c plus plus?

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.


What are the conditional statement of turbo c plus plus dos based?

Statements that check an expression then may or may not execute a statement or group of statements depending on the result of the condition.


Looping statement in c plus plus?

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.


What are unconditional statements in c plus plus?

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) }


What is the notation used to place block of statements in a looping structure in C plus plus?

for( ; ; ) { statement_block; } while( conditional_expression ) { statement_block; } do { statement_block; }while( conditional_expression )


How do you terminate statements in C?

All statements must be terminated with a semi-colon in C.


Can you see a programme of if syntax in c plus plus?

My self Dhilib... it is a simple query. if statement is a basic control statement. mostly it used in all the languages. also in c plus plus syntax: if(test condition) { true statements; } else { false-statements; } Example: void main() { int a,b; a=54; b=65; if(a&gt;b) { cout&lt;&lt;" a value is big"; } else { cout&lt;&lt;"b value is big"; } }