Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • C++ Data Types
  • C++ Input/Output
  • C++ Arrays
  • C++ Pointers
  • C++ OOPs
  • C++ STL
  • C++ Interview Questions
  • C++ Programs
  • C++ Cheatsheet
  • C++ MCQ
  • C++ Projects
  • C++ Exception Handling
  • C++ Memory Management
Open In App
Next Article:
C++ if Statement
Next article icon

Compound Statements in C++

Last Updated : 19 Oct, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

Compound statements in C++ are blocks used to group multiple statements together into a single unit. These statements are enclosed in curly braces {} and can be used wherever a single statement is expected. The statements put inside curly braces are executed just like a single statement would have been executed. The compound statements help in organizing code and provide a way to execute multiple statements sequentially.

Syntax of Compound Statements

The compound statements look like this in the program:

{      // Statements      statement1;      statement2;      // ...      statementN;  }

We just have to put multiple statements in the braces {}.

Examples of Compound Statements

Example 1: Using Compound Statements with Functions

The compound statements are commonly used within the function definitions. All the statements inside a function's body are enclosed as the compound statement.

C++
// C++ Program to illustrate Compound Statements in // Functions #include <iostream> using namespace std;  void GFG() // compound statement start {     cout << "Hello World" << endl; } // compound statement end  // driver code int main() {     GFG();     return 0; } 

Output
1 2 3 4 5 

Example 2: Compound Statement with if Statement

The compound statements are used in if statements for the instance when multiple statements need to be executed conditionally.

C++
// C++ Program to illustrate Compound Statements in // Conditional Statements #include <iostream> using namespace std;  int main() {     int x = 20;     if (x > 6) {         cout << "x is greater than 6"              << std::endl; // statement 1         cout << "Inside the if block"              << std::endl; // statement 2     }     return 0; } 

Output
x is greater than 6  Inside the if block  

Example 3: Simple Compound Statement Inside a Function

In this program, we will use we'll use a compound statement in between a function which also controls the scope of the variables.

C++
// C++ Program to illustrate Compound Statements in Variable // Scope #include <iostream> using namespace std;  int main() {     int x = 8; // Outer variable     cout << "Outer x: " << x << endl;      // Variable Scope Starts     {         int x = 30;         cout << "Inner x: " << x << endl;     }     // Variable Scope Ends      // Access the outer x after the inner block scope     cout << "Outer x: " << x << endl;      return 0; } 

Output
Outer x: 8  Inner x: 30  Outer x: 8

Conclusion

As we have seen, compound statements are an integral part of the C programming languages. It is used with almost all of the components of the C to group the statements together or provide a separate scope.


Next Article
C++ if Statement

S

subramanyasmgm
Improve
Article Tags :
  • C++
  • Geeks Premier League
  • Geeks Premier League 2023
Practice Tags :
  • CPP

Similar Reads

  • goto Statement in C++
    The goto statement in C++ is a control flow statement that provides for an unconditional jump to the same function's predefined label statement. In simple terms, the goto is a jump statement transfers the control flow of a program to a labeled statement within the scope of the same function, breakin
    5 min read
  • std::is_compound Template in C++
    The std::is_compound template of C++ STL is used to check the whether the type is a compound type or not. It returns a boolean value showing the same. Syntax: template < class T > struct is_compound; Parameter: This template contains single parameter T (Trait class) to check whether T is a com
    2 min read
  • C++ if Statement
    The C++ if statement is the most simple decision-making statement. It is used to decide whether a certain statement or block of statements will be executed or not executed based on a certain condition. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using namespace std; int
    3 min read
  • C++ Compound Data Types Quiz
    Built-in data types cannot store all the information in an easily accessible and organized way. That is why C++ provides compound data types such as arrays, pointers, strings, etc. that are derived from the built-in data types and provide different way to use them. Good understanding of compound dat
    2 min read
  • count() in C++ STL
    In C++, the count() is a built-in function used to find the number of occurrences of an element in the given range. This range can be any STL container or an array. In this article, we will learn about the count() function in C++. Let’s take a quick look at a simple example that uses count() method:
    3 min read
  • C++ Nested if-else Statement
    Nested if-else statements are those statements in which there is an if statement inside another if else. We use nested if-else statements when we want to implement multilayer conditions (condition inside the condition inside the condition and so on). C++ allows any number of nesting levels. Let's ta
    3 min read
  • Naming Convention in C++
    Naming a file or a variable is the first and the very basic step that a programmer takes to write clean codes, where naming has to be appropriate so that for any other programmer it acts as an easy way to read the code. In C++, naming conventions are the set of rules for choosing the valid name for
    6 min read
  • multimap key_comp in C++ STL
    This is the part of Standard Template Library(STL) of C++. To use this STL, use Namespace: std and include “map” header file in the program. It returns the function object or comparison object or ordering delegate that compares the keys, which is a copy of this container's constructor argument. It i
    2 min read
  • std::function in C++
    The std::function() in C++ is a function wrapper class which can store and call any function or a callable object. In this article, we will learn about std::function in C++ and how to use it in different cases. Table of Content What is std::function in C++?Example of std::functionMember Functions of
    6 min read
  • find() in C++ STL
    C++ find() is a built-in function used to find the first occurrence of an element in the given range. It works with any container that supports iterators, such as arrays, vectors, lists, and more. In this article, we will learn about find() function in C++. [GFGTABS] C++ //Driver Code Starts{ #inclu
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences