Statements - cppreference.com (2024)

C

Compiler support
Language
Headers
Type support
Program utilities
Variadic function support
Error handling
Dynamic memory management
Strings library
Algorithms
Numerics
Date and time utilities
Input/output support
Localization support
Concurrency support (C11)
Technical Specifications
Symbol index

[edit]

C language

Basic concepts
Keywords
Preprocessor
Statements
Expressions
Initialization
Declarations
Functions
Miscellaneous
History of C
Technical Specifications

[edit]

Statements

Labels

label : statement

Expression statements

expression ;

Compound statements

{ statement... }

Selection statements

if

switch

Iteration statements

while

do-while

for

Jump statements

break

continue

return

goto

[edit]

Statements are fragments of the C program that are executed in sequence. The body of any function is a compound statement, which, in turn is a sequence of statements and declarations:

int main(void){ // start of a compound statement int n = 1; // declaration (not a statement) n = n+1; // expression statement printf("n =%d\n", n); // expression statement return 0; // return statement} // end of compound statement, end of function body


There are five types of statements:

1) compound statements

2) expression statements

3) selection statements

4) iteration statements

5) jump statements

An attribute specifier sequence (attr-spec-seq) can be applied to an unlabeled statement, in which case (except for an expression statement) the attributes are applied to the respective statement.

(since C23)

Contents

  • 1 Labels
  • 2 Compound statements
  • 3 Expression statements
  • 4 Selection statements
  • 5 Iteration statements
  • 6 Jump statements
  • 7 References
  • 8 See also

[edit] Labels

Any statement can be labeled, by providing a name followed by a colon before the statement itself.

attr-spec-seq(optional)(since C23) identifier : (1)
attr-spec-seq(optional)(since C23) case constant-expression : (2)
attr-spec-seq(optional)(since C23) default : (3)

1) Target for goto.

2) Case label in a switch statement.

3) Default label in a switch statement.

Any statement (but not a declaration) may be preceded by any number of labels, each of which declares identifier to be a label name, which must be unique within the enclosing function (in other words, label names have function scope).

Label declaration has no effect on its own, does not alter the flow of control, or modify the behavior of the statement that follows in any way.

A label shall be followed by a statement.

(until C23)

A label can appear without its following statement. If a label appears alone in a block, it behaves as if it is followed by a null statement.

The optional attr-spec-seq is applied to the label.

(since C23)

[edit] Compound statements

A compound statement, or block, is a brace-enclosed sequence of statements and declarations.

{ statement | declaration...(optional) } (until C23)
attr-spec-seq(optional) { unlabeled-statement | label | declaration...(optional) } (since C23)

The compound statement allows a set of declarations and statements to be grouped into one unit that can be used anywhere a single statement is expected (for example, in an if statement or an iteration statement):

if (expr) // start of if-statement{ // start of block int n = 1; // declaration printf("%d\n", n); // expression statement} // end of block, end of if-statement

Each compound statement introduces its own block scope.

The initializers of the variables with automatic storage duration declared inside a block and the VLA declarators are executed when flow of control passes over these declarations in order, as if they were statements:

int main(void){ // start of block { // start of block puts("hello"); // expression statement int n = printf("abc\n"); // declaration, prints "abc", stores 4 in n int a[n*printf("1\n")]; // declaration, prints "1", allocates 8*sizeof(int) printf("%zu\n", sizeof(a)); // expression statement } // end of block, scope of n and a ends int n = 7; // n can be reused}

[edit] Expression statements

An expression followed by a semicolon is a statement.

expression(optional) ; (1)
attr-spec-seq expression ; (2) (since C23)

Most statements in a typical C program are expression statements, such as assignments or function calls.

An expression statement without an expression is called a null statement. It is often used to provide an empty body to a for or while loop. It can also be used to carry a label in the end of a compound statement or before a declaration:

puts("hello"); // expression statementchar *s;while (*s++ != '\0') ; // null statement

The optional attr-spec-seq is applied to the expression.

An attr-spec-seq followed by ; does not form an expression statement. It forms an attribute declaration instead.

(since C23)

[edit] Selection statements

The selection statements choose between one of several statements depending on the value of an expression.

attr-spec-seq(optional)(since C23) if ( expression ) statement (1)
attr-spec-seq(optional)(since C23) if ( expression ) statement else statement (2)
attr-spec-seq(optional)(since C23) switch ( expression ) statement (3)

1) if statement

2) if statement with an else clause

3) switch statement

[edit] Iteration statements

The iteration statements repeatedly execute a statement.

attr-spec-seq(optional)(since C23) while ( expression ) statement (1)
attr-spec-seq(optional)(since C23) do statement while ( expression ) ; (2)
attr-spec-seq(optional)(since C23) for ( init-clause ; expression(optional) ; expression(optional) ) statement (3)

1) while loop

2) do-while loop

3) for loop

[edit] Jump statements

The jump statements unconditionally transfer flow control.

attr-spec-seq(optional)(since C23) break ; (1)
attr-spec-seq(optional)(since C23) continue ; (2)
attr-spec-seq(optional)(since C23) return expression(optional) ; (3)
attr-spec-seq(optional)(since C23) goto identifier ; (4)

1) break statement

2) continue statement

3) return statement with an optional expression

4) goto statement

[edit] References

  • C17 standard (ISO/IEC 9899:2018):
  • 6.8 Statements and blocks (p: 106-112)
  • C11 standard (ISO/IEC 9899:2011):
  • 6.8 Statements and blocks (p: 146-154)
  • C99 standard (ISO/IEC 9899:1999):
  • 6.8 Statements and blocks (p: 131-139)
  • C89/C90 standard (ISO/IEC 9899:1990):
  • 3.6 STATEMENTS

[edit] See also

C++ documentation for Statements

Statements - cppreference.com (2024)

FAQs

What are statements in C plus plus? ›

C++ statements are the program elements that control how and in what order objects are manipulated. This section includes: Overview. Labeled Statements. Categories of Statements.

What is the break statement in Cppreference? ›

Causes the enclosing for, range-for, while or do-while loop or switch statement to terminate. Used when it is otherwise awkward to terminate the loop using the condition expression and conditional statements.

What are the different types of statements? ›

A statement forms a complete unit of execution and is terminated with a semicolon ( ; ). There are three kinds of statements: expression statements, declaration statements, and control flow statements.

What are the two types of statements in C++? ›

C++ includes the following types of statements:
  • 1) labeled statements;
  • 2) expression statements;
  • 3) compound statements;
  • 4) selection statements;
  • 5) iteration statements;
  • 6) jump statements;
  • 7) declaration statements;
  • 8) try blocks;
Jun 13, 2024

How do or statements work in C++? ›

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise. The operands are implicitly converted to type bool before evaluation, and the result is of type bool . Logical OR has left-to-right associativity.

Which statement is used in C++? ›

C++ contains various types of statements such as labeled statements,expression statements, compound statements, selection statements, etc. if, else, and switch are some selection statements. while, do while, and for loop are some of the iteration statements in C++.

What is using statement in C++? ›

​The using keyword is used to: Bring a specific member from the namespace into the current scope. Bring all members from the namespace into​ the current scope. Bring a base class method ​or variable into the current class's scope.

What is a jumping statement in C++? ›

Jump statements are used to manipulate the flow of the program if some conditions are met. It is used to terminating or continues the loop inside a program or to stop the execution of a function. In C++ there is four jump statement: break, continue, goto and return.

What is used to terminate a statement in C++? ›

Break Statement in C++

The break in C++ is a loop control statement used to terminate the loop.

What is the use of void? ›

When used for a function's parameter list, void specifies that the function takes no parameters. When used in the declaration of a pointer, void specifies that the pointer is "universal." If a pointer's type is void* , the pointer can point to any variable that's not declared with the const or volatile keyword.

What is an example of a statement? ›

A statement is a sentence that says something is true, like "Pizza is delicious." There are other kinds of statements in the worlds of the law, banking, and government. All statements claim something or make a point. If you witness an accident, you make a statement to police, describing what you saw.

How to write statement example? ›

Here are the basic steps you need to take to write a statement:
  1. Identify your ultimate objective. First, identify what you want to accomplish with your statement. ...
  2. Write an introduction. ...
  3. Write the body. ...
  4. Create a strong conclusion. ...
  5. Proofread your statement.
Jul 30, 2024

What are 10 examples of statement sentences? ›

What are some examples of statement sentences?
  • I need to do my homework tonight.
  • I don't want to have pasta for dinner, so I'll have to buy something else.
  • Our flight back to London from Spain was cancelled, so we had to stay an extra night.
  • I go to my swimming lessons every Wednesday afternoon.

What are include statements in C++? ›

#include is a way of including a standard or user-defined file in the program and is mostly written at the beginning of any C program. The #include preprocessor directive is read by the preprocessor and instructs it to insert the contents of a user-defined or system header file in our C program.

What are statements in C programming? ›

C statements consist of tokens, expressions, and other statements. A statement that forms a component of another statement is called the "body" of the enclosing statement. Each statement type given by the following syntax is discussed in this section.

What is an expression and a statement in CPP? ›

A statement is analogous to a line of code. It may do many things but it's treated as a unit of execution. A statement is always terminated with a semicolon. An expression is anything that returns a value whether or not the value is used.

What are C Plus Plus control statements? ›

What Are Control Statements in C? In simple words, Control statements in C help the computer execute a certain logical statement and decide whether to enable the control of the flow through a certain set of statements or not. Also, it is used to direct the execution of statements under certain conditions.

References

Top Articles
Magic: The Gathering - The Infamous Cruelclaw Commander Deck Guide
THE DOORS OF MIDNIGHT by R. R. Virdi (EXCERPT)
Supermotocross Points Standings
Wmaz 13
Cremation Services | Mason Funeral Home serving Westfield, New York...
Craigslist Centre Alabama
Luxiconic Nails
Sarah Coughlan Boobs
Treasure Hunt Deals Racine Wi
Craigslist Carpet Installers
Tate Sweat Lpsg
Press And Sun-Bulletin Obits Today
Carmax Chevrolet Tahoe
NYC Drilled on Variant Response as Vaccine Limits Push State Appointments to Mid-April
Melissa N. Comics
Ravens 24X7 Forum
Hill & Moin Top Workers Compensation Lawyer
Minor Additions To The Bill Crossword
Krystal Murphy Below Deck Net Worth
Cappacuolo Pronunciation
Ff14 Cloth Softening Powder
8 of the best things to do in San Diego: get a taste of nature near a laid-back city
Huniepop Jessie Questions And Answers
Ma.speedtest.rcn/Merlin
Rugged Gentleman Barber Shop Martinsburg Wv
Lanie Gardner: The Rising Star Behind the Viral Fleetwood Mac Cover - Neon Music - Digital Music Discovery & Showcase Platform
6 Best Doublelist Alternatives Worth Trying in 2024
6 Fun Things to Do in Bodega Bay - Sonoma County Tourism
Aspenx2 Newburyport
Account Now Login In
Shaws Star shines bright selling for 16,000gns at the Red Ladies and Weaned Calf sale.
Women On Twitch Go Without Makeup To Support A Fellow Streamer
Jersey Mikes Ebt
Wolf Of Wallstreet 123 Movies
10 Best Laptops for FL Studio in 2023 | Technize
Computer Repair Tryon North Carolina
Planet Zoo Obstructed
Musc Food Truck Schedule
Tamilrockers 2023 Tamil Movies Download Kuttymovies
Boostmaster Lin Yupoo
99 Cents Food Handler
Saw X Showtimes Near Stone Theatres Sun Valley 14 Cinemas
Vadoc Gtlvisitme App
P1 Offshore Schedule
Gowilkes For Rent
German American Bank Owenton Ky
Ebony Grinding Lesbian
Rocky Aur Rani Kii Prem Kahaani - Movie Reviews
The 7 best games similar to Among Us for Android - Sbenny’s Blog
Lubbock Avalanche Journal Newspaper Obituaries
Find Such That The Following Matrix Is Singular.
Southwest Airlines Departures Atlanta
Latest Posts
Article information

Author: Frankie Dare

Last Updated:

Views: 5622

Rating: 4.2 / 5 (53 voted)

Reviews: 92% of readers found this page helpful

Author information

Name: Frankie Dare

Birthday: 2000-01-27

Address: Suite 313 45115 Caridad Freeway, Port Barabaraville, MS 66713

Phone: +3769542039359

Job: Sales Manager

Hobby: Baton twirling, Stand-up comedy, Leather crafting, Rugby, tabletop games, Jigsaw puzzles, Air sports

Introduction: My name is Frankie Dare, I am a funny, beautiful, proud, fair, pleasant, cheerful, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.