C++ Programming Language
Table of content:
- A Brief Intro To C++
- The Timeline Of C++
- Importance Of C++
- Versions Of C++ Language
- Comparison With Other Popular Programming Languages
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Variables In C++?
- Declaration & Definition Of Variables In C++
- Variable Initialization In C++
- Rules & Regulations For Naming Variables In C++ Language
- Different Types Of Variables In C++
- Different Types of Variable Initialization In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Primitive Data Types In C++?
- Derived Data Types In C++
- User-Defined Data Types In C++
- Abstract Data Types In C++
- Data Type Modifiers In C++
- Declaring Variables With Auto Keyword
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Structure Of C++ Program: Components
- Compilation & Execution Of C++ Programs | Step-by-Step Explanation
- Structure Of C++ Program With Example
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What is Typedef in C++?
- The Role & Applications of Typedef in C++
- Basic Syntax for typedef in C++
- How Does typedef Work in C++?
- How to Use Typedef in C++ With Examples? (Multiple Data Types)
- The Difference Between #define & Typedef in C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Strings In C++?
- Types Of Strings In C++
- How To Declare & Initialize C-Style Strings In C++ Programs?
- How To Declare & Initialize Strings In C++ Using String Keyword?
- List Of String Functions In C++
- Operations On Strings Using String Functions In C++
- Concatenation Of Strings In C++
- How To Convert Int To Strings In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is String Concatenation In C++?
- How To Concatenate Two Strings In C++ Using The ‘+' Operator?
- String Concatenation Using The strcat( ) Function
- Concatenation Of Two Strings In C++ Using Loops
- String Concatenation Using The append() Function
- C++ String Concatenation Using The Inheritance Of Class
- Concatenate Two Strings In C++ With The Friend and strcat() Functions
- Why Do We Need To Concatenate Two Strings?
- How To Reverse Concatenation Of Strings In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is Find In String C++?
- What Is A Substring?
- How To Find A Substring In A String In C++?
- How To Find A Character In String C++?
- Find All Substrings From A Given String In C++
- Index Substring In String In C++ From A Specific Start To A Specific Length
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Pointers In C++?
- Pointer Declaration In C++
- How To Initialize And Use Pointers In C++?
- Different Types Of Pointers In C++
- References & Pointers In C++
- Arrays And Pointers In C++
- String Literals & Pointers In C++
- Pointers To Pointers In C++ (Double Pointers)
- Arithmetic Operation On Pointers In C++
- Advantages Of Pointers In C++
- Some Common Mistakes To Avoid With Pointers In Cpp
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Understanding Pointers In C++
- What Is Pointer To Object In C++?
- Declaration And Use Of Object Pointers In C++
- Advantages Of Pointer To Object In C++
- Pointer To Objects In C++ With Arrow Operator
- An Array Of Objects Using Pointers In C++
- Base Class Pointer For Derived Class Object In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is 'This' Pointer In C++?
- Defining ‘this’ Pointer In C++
- Example Of 'this' Pointer In C++
- Describing The Constness Of 'this' Pointer In C++
- Important Uses Of 'this' Pointer In C++
- Method Chaining Using 'this' Pointer In C++
- C++ Programs To Show Application Of 'This' Pointer
- How To Delete The ‘this’ Pointer In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What is Reference?
- What is Pointer?
- Comparison Table Of C++ Pointer Vs. Reference
- Differences Between Reference And Pointer: A Detailed Explanation
- Why Are References Less Powerful Than Pointers?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- How To Declare A 2D Array In C++?
- C++ Multi-Dimensional Arrays
- Ways To Initialize A 2D Array In C++
- Methods To Dynamically Allocate A 2D Array In C++
- Accessing/ Referencing Two-Dimensional Array Elements
- How To Initialize A Two-Dimensional Integer Array In C++?
- How To Initialize A Two-Dimensional Character Array?
- How To Enter Data In Two-Dimensional Array In C++?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Arrays Of Strings In C++?
- Different Ways To Create String Arrays In C++
- How To Access The Elements Of A String Array In C++?
- How To Convert Char Array To String?
- Conclusion
- Frequently Asked Questions
Table of content:
- What is Memory Allocation in C++?
- The “new" Operator In C++
- The "delete" Operator In C++
- Dynamic Memory Allocation In C++ | Arrays
- Dynamic Memory Allocation In C++ | Objects
- Deallocation Of Dynamic Memory
- Dynamic Memory Allocation In C++ | Uses
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is A Substring In C++ (Substr C++)?
- Example For Substr In C++
- Points To Remember For Substr In C++
- Important Applications Of substr() Function
- How to Get a Substring Before a Character?
- Print All Substrings Of A Given String
- Print Sum Of All Substrings Of A String Representing A Number
- Print Minimum Value Of All Substrings Of A String Representing A Number
- Print Maximum Value Of All Substrings Of A String Representing A Number
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is Operator In C++?
- Types Of Operators In C++ With Examples
- What Are Arithmetic Operators In C++?
- What Are Assignment Operators In C++?
- What Are Relational Operators In C++?
- What Are Logical Operators In C++?
- What Are Bitwise Operators In C++?
- What Is Ternary/ Conditional Operator In C++?
- Miscellaneous Operators In C++
- Precedence & Associativity Of Operators In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is The New Operator In C++?
- Example To Understand New Operator In C++
- The Grammar Elements Of The New Operator In C++
- Storage Space Allocation
- How Does The C++ New Operator Works?
- What Happens When Enough Memory In The Program Is Not Available?
- Initializing Objects Allocated With New Operator In C++
- Lifetime Of Objects Allocated With The New Operator In C++
- What Is The Delete Operator In C++?
- Difference Between New And Delete Operator In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Types Of Overloading In C++
- What Is Operator Overloading In C++?
- How To Overload An Operator In C++?
- Overloadable & Non-overloadable Operators In C++
- Unary Operator Overloading In C++
- Binary Operator Overloading In C++
- Special Operator Overloading In C++
- Rules For Operator Overloading In C++
- Advantages And Disadvantages Of Operator Overloading In C++
- Function Overloading In C++
- What Is the Difference Between Operator Functions and Normal Functions?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Operators In C++?
- Introduction To Logical Operators In C++
- Types Of Logical Operators In C++ With Example Program
- Logical AND (&&) Operator In C++
- Logical NOT(!) Operator In C++
- Logical Operator Precedence And Associativity In C++
- Relation Between Conditional Statements And Logical Operators In C++
- C++ Relational Operators
- Conclusion
- Frequently Asked Important Interview Questions:
- Test Your Skills: Quiz Time
Table of content:
- Different Type Of C++ Bitwise Operators
- C++ Bitwise AND Operator
- C++ Bitwise OR Operator
- C++ Bitwise XOR Operator
- Bitwise Left Shift Operator In C++
- Bitwise Right Shift Operator In C++
- Bitwise NOT Operator
- What Is The Meaning Of Set Bit In C++?
- What Does Clear Bit Mean?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Types of Comments in C++
- Single Line Comment In C++
- Multi-Line Comment In C++
- How Do Compilers Process Comments In C++?
- C- Style Comments In C++
- How To Use Comment In C++ For Debugging Purposes?
- When To Use Comments While Writing Codes?
- Why Do We Use Comments In Codes?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Storage Classes In Cpp?
- What Is The Scope Of Variables?
- What Are Lifetime And Visibility Of Variables In C++?
- Types of Storage Classes in C++
- Automatic Storage Class In C++
- Register Storage Class In C++
- Static Storage Class In C++
- External Storage Class In C++
- Mutable Storage Class In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- Decision Making Statements In C++
- Types Of Conditional Statements In C++
- If-Else Statement In C++
- If-Else-If Ladder Statement In C++
- Nested If Statements In C++
- Alternatives To Conditional If-Else In C++
- Switch Case Statement In C++
- Jump Statements & If-Else In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is A Switch Statement/ Switch Case In C++?
- Rules Of Switch Case In C++
- How Does Switch Case In C++ Work?
- The break Keyword In Switch Case C++
- The default Keyword In C++ Switch Case
- Switch Case Without Break And Default
- Advantages & Disadvantages of C++ Switch Case
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is A For Loop In C++?
- Syntax Of For Loop In C++
- How Does A For Loop In C++ Work?
- Examples Of For Loop Program In C++
- Ranged Based For Loop In C++
- Nested For Loop In C++
- Infinite For Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is A While Loop In C++?
- Parts Of The While Loop In C++
- C++ While Loop Program Example
- How Does A While Loop In C++ Work?
- What Is Pre-checking Process Or Entry-controlled Loop?
- When Are While Loops In C++ Useful?
- Example C++ While Loop Program
- What Are Nested While Loops In C++?
- Infinite While Loop In C++
- Alternatives To While Loop In C++
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are Loops & Its Types In C++?
- What Is A Do-While Loop In C++?
- Do-While Loop Example In C++ To Print Numbers
- How Does A Do-While Loop In C++ Work?
- Various Components Of The Do-While Loop In C++
- Example 2: Adding User-Input Positive Numbers With Do-While Loop
- C++ Nested Do-While Loop
- C++ Infinitive Do-while Loop
- What is the Difference Between While Loop and Do While Loop in C++?
- When To Use A Do-While Loop?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Are 2D Vectors In C++?
- How To Declare 2D Vector In C++?
- How To Initialize 2D Vector In C++?
- C++ Program Examples For 2D Vectors
- How To Access & Modify 2D Vector Elements In C++?
- Methods To Traverse, Manipulate & Print 2D Vectors In C++
- Adding Elements To 2-D Vector Using push_back() Function
- Removing Elements From Vector In C++ Using pop_back() Function
- Creating 2D Vector In C++ With User Input For Size Of Column & Row
- Advantages of 2D Vectors Over Traditional Arrays
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- How To Print A Vector In C++ By Overloading Left Shift (<<) Operator?
- How To Print Vector In C++ Using Range-Based For-Loop?
- Print Vector In C++ With Comma Separator
- Printing Vector In C++ Using Indices (Square Brackets/ Double Brackets & at() Function)
- How To Print A Vector In C++ Using std::copy?
- How To Print A Vector In C++ Using for_each() Function?
- Printing C++ Vector Using The Lambda Function
- How To Print Vector In C++ Using Iterators?
- Conclusion
- Frequently Asked Questions
Table of content:
- Definition Of C++ Find In Vector
- Using The std::find() Function
- How Does find() In Vector C++ Function Work?
- Finding An Element By Custom Comparator Using std::find_if() Function
- Use std::find_if() With std::distance()
- Element Find In Vector C++ Using For Loop
- Using The find_if_not Function
- Find Elements With The Linear Search Approach
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Sort() Function In C++?
- Sort() Function In C++ From Standard Template Library
- Exceptions Of Sort() Function/ Algorithm In C++
- The Stable Sort() Function In C++
- Partial Sort() Function In C++
- Sorting In Ascending Order With Sort() Function In C++
- Sorting In Descending Order With Sort Function In C++
- Sorting In Desired Order With Custom Comparator Function & Sort Function In C++
- Sorting Elements In Desired Order Using Lambda Expression & Sort Function In C++
- Types of Sorting Algorithms In C++
- Advanced Sorting Algorithms In C++
- How Does the Sort() Function Algorithm Work In C++?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Function Overloading In C++?
- Ways Of Function Overloading In C++
- Function Overloading In C++ Using Different Types Of Parameters
- Function Overloading In C++ With Different Number Of Parameters
- Function Overloading In C++ Using Different Sequence Of Parameters
- How Does Function Overloading In C++ Work?
- Rules Of Function Overloading In C++
- Why Is Function Overloading Used?
- Types Of Function Overloading Based On Time Of Resolution
- Causes Of Function Overloading In C++
- Ambiguity & Function Overloading In C++
- Advantages Of Function Overloading In C++
- Disadvantages Of Function Overloading In C++
- Operator Overloading In C++
- Function Overriding In C++
- Difference Between Function Overriding & Function Overloading In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is An Inline Function In C++?
- How To Define The Inline Function In C++?
- How Does Inline Function In C++ Work?
- The Need For An Inline Function In C++
- Can The Compiler Ignore/ Reject Inline Function In C++ Programs?
- Normal Function Vs. Inline Function In C++
- Classes & Inline Function In C++
- Understanding Inline, __inline, And __forceinline Functions In C++
- When To Use An Inline Function In C++?
- Advantages Of Inline Function In C++
- Disadvantages Of Inline Function In C++
- Why Not Use Macros Instead Of An Inline Function In C++?
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is Static Data Member In C++?
- How To Declare Static Data Members In C++?
- How To Initialize/ Define Static Data Member In C++?
- Ways To Access A Static Data Member In C++
- What Are Static Member Functions In C++?
- Example Of Member Function & Static Data Member In C++
- Practical Applications Of Static Data Member In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Constant In C++?
- Ways To Define Constant In C++
- What Are Literals In C++?
- Pointer To A Constant In C++
- Constant Function Arguments In C++
- Constant Member Function Of Class In C++
- Constant Data Members In C++
- Object Constant In C++
- Conclusion
- Frequently Asked Questions(FAQ)
Table of content:
- What Is Friend Function In C++?
- Declaration Of Friend Function In C++ With Example
- Characteristics Of Friend Function In C++
- Global Friend Function In C++ (Global Function As Friend Function )
- Member Function Of Another Class As Friend Function In C++
- Function Overloading Using Friend Function In C++
- Advantages & Disadvantages Of Friend Function in C++
- What Is A C++ Friend Class?
- A Function Friendly To Multiple Classes
- C++ Friend Class Vs. Friend Function In C++
- Some Important Points About Friend Functions And Classes In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Function Overriding In C++?
- The Working Mechanism Of Function Overriding In C++
- Real-Life Example Of Function Overriding In C++
- Accessing Overriding Function In C++
- Accessing Overridden Function In C++
- Function Call Binding With Class Objects | Function Overriding In C++
- Function Call Binding With Base Class Pointers | Function Overriding In C++
- Advantages Of Function Overriding In C++
- Variations In Function Overriding In C++
- Function Overloading In C++
- Function Overloading Vs Function Overriding In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- Errors In C++
- What Is Exception Handling In C++?
- Exception Handling In C++ Program Example
- C++ Exception Handling: Basic Keywords
- The Need For C++ Exception Handling
- C++ Standard Exceptions
- C++ Exception Classes
- User-Defined Exceptions In C++
- Advantages & Disadvantages Of C++ Exception Handling
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Templates In C++ & How Do They Work?
- Types Of Templates In C++
- What Are Function Templates In C++?
- C++ Template Functions With Multiple Parameters
- C++ Template Function Overloading
- What Are Class Templates In C++?
- Defining A Class Member Outside C++ Template Class
- C++ Template Class With Multiple Parameters
- What Is C++ Template Specialization?
- How To Specify Default Arguments For Templates In C++?
- Advantages Of C++ Templates
- Disadvantages Of C++ Templates
- Difference Between Function Overloading And Templates In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- Structure
- Structure Declaration
- Initialization of Structure
- Copying and Comparing Structures
- Array of Structures
- Nested Structures
- Pointer to a Structure
- Structure as Function Argument
- Self Referential Structures
- Class
- Object Declaration
- Accessing Class Members
- Similarities between Structure and Class
- Which One Should You Choose?
- Key Difference Between a Structure and Class
- Summing Up
- Test Your Skills: Quiz Time
Table of content:
- What Is A Class And Object In C++?
- What Is An Object In C++?
- How To Create A Class & Object In C++? With Example
- Access Modifiers & Class/ Object In C++
- Member Functions Of A Class In C++
- How To Access Data Members And Member Functions?
- Significance Of Class & Object In C++
- What Are Constructors In C++ & Its Types?
- What Is A Destructor Of Class In C++?
- An Array Of Objects In C++
- Object In C++ As Function Arguments
- The this (->) Pointer & Classes In C++
- The Need For Semicolons At The End Of A Class In C++
- Difference Between Structure & Class In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Static Members In C++?
- Static Member Functions in C++
- Ways To Call Static Member Function In C++
- Properties Of Static Member Function In C++
- Need Of Static Member Functions In C++
- Regular Member Function Vs. Static Member Function In C++
- Limitations Of Static Member Functions In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Constructor In C++?
- Characteristics Of A Constructor In C++
- Types Of Constructors In C++
- Default Constructor In C++
- Parameterized Constructor In C++
- Copy Constructor In C++
- Dynamic Constructor In C++
- Benefits Of Using Constructor In C++
- How Does Constructor In C++ Differ From Normal Member Function?
- Constructor Overloading In C++
- Constructor For Array Of Objects In C++
- Constructor In C++ With Default Arguments
- Initializer List For Constructor In C++
- Dynamic Initialization Using Constructor In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Constructor In C++?
- What Is Constructor Overloading In C++?
- Dеclaration Of Constructor Ovеrloading In C++
- Condition For Constructor Overloading In C++
- How Constructor Ovеrloading In C++ Works?
- Examples Of Constructor Overloading In C++
- Lеgal & Illеgal Constructor Ovеrloading In C++
- Types Of Constructors In C++
- Characteristics Of Constructors In C++
- Advantage Of Constructor Overloading In C++
- Disadvantage Of Constructor Overloading In C++
- Conclusion
- Frеquеntly Askеd Quеstions
Table of content:
- What Is A Destructor In C++?
- Rules For Defining A Destructor In C++
- When Is A Destructor in C++ Called?
- Order Of Destruction In C++
- Default Destructor & User-Defined Destructor In C++
- Virtual Destructor In C++
- Pure Virtual Destructor In C++
- Key Properties Of Destructor In C++ You Must Know
- Explicit Destructor Calls In C++
- Destructor Overloading In C++
- Difference Between Normal Member Function & Destructor In C++
- Important Uses Of Destructor In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Constructor In C++?
- What Is A Destructor In C++?
- Difference Between Constructor And Destructor In C++
- Constructor In C++ | A Brief Explanation
- Destructor In C++ | A Brief Explanation
- Difference Between Constructor And Destructor In C++ Explained
- Order Of Calling Constructor And Destructor In C++ Classes
- Conclusion
- Frequently Asked Questions
- Test Your Skills: Quiz Time
Table of content:
- What Is Type Conversion In C++?
- What Is Type Casting In C++?
- Types Of Type Conversion In C++
- Implicit Type Conversion (Coercion) In C++
- Explicit Type Conversion (Casting) In C++
- Advantages Of Type Conversion In C++
- Disadvantages Of Type Conversion In C++
- Difference Between Type Casting & Type Conversion In C++
- Application Of Type Casting In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is A Copy Constructor In C++?
- Characteristics Of Copy Constructors In C++
- Types Of Copy Constructors In C++
- When Do We Call The Copy Constructor In C++?
- When Is A User-Defined Copy Constructor Needed In C++?
- Types Of Constructor Copies In C++
- Can We Make The Copy Constructor In C++ Private?
- Assignment Operator Vs Copy Constructor In C++
- Example Of Class Where A Copy Constructor Is Essential
- Uses Of Copy Constructors In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- Why Do You Need Object-Oriented Programming (OOP) In C++?
- OOPs Concepts In C++ With Examples
- The Class OOPs Concept In C++
- The Object OOPs Concept In C++
- The Inheritance OOPs Concept In C++
- Polymorphism OOPs Concept In C++
- Abstraction OOPs Concept In C++
- Encapsulation OOPs Concept In C++
- Other Features Of OOPs In C++
- Benefits Of OOP In C++ Over Procedural-Oriented Programming
- Disadvantages Of OOPS Concept In C++
- Why Is C++ A Partial OOP Language?
- Conclusion
- Frequently Asked Questions
Table of content:
- Introduction To Abstraction In C++
- Types Of Abstraction In C++
- What Is Data Abstraction In C++?
- Understanding Data Abstraction In C++ Using Real Life Example
- Ways Of Achieving Data Abstraction In C++
- What Is An Abstract Class?
- Advantages Of Data Abstraction In C++
- Use Cases Of Data Abstraction In C++
- Encapsulation Vs. Abstraction In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Encapsulation In C++?
- How Does Encapsulation Work In C++?
- Types Of Encapsulation In C++
- Why Do We Need Encapsulation In C++?
- Implementation Of Encapsulation In C++
- Access Specifiers & Encapsulation In C++
- Role Of Access Specifiers In Encapsulation In C++
- Member Functions & Encapsulation In C++
- Data Hiding & Encapsulation In C++
- Features Of Encapsulation In C++
- Advantages & Disadvantages Of Encapsulation In C++
- Difference Between Abstraction and Encapsulation In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Inheritance In C++?
- What Are Child And Parent Classes?
- Syntax And Structure Of Inheritance In C++
- Implementing Inheritance In C++
- Importance Of Inheritance In C++
- Types Of Inheritance In C++
- Visibility Modes Of Inheritance In C++
- Access Modifiers & Inheritance In C++
- How To Make A Private Member Inheritable?
- Member Function Overriding In Inheritance In C++
- The Diamond Problem | Inheritance In C++ & Ambiguity
- Ways To Avoid Ambiguity Inheritance In C++
- Why & When To Use Inheritance In C++?
- Advantages Of Inheritance In C++
- The Disadvantages Of Inheritance In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Hybrid Inheritance In C++?
- Importance Of Hybrid Inheritance In Object Oriented Programming
- Example Of Hybrid Inheritance In C++: Using Single and Multiple Inheritance
- Example Of Hybrid Inheritance In C++: Using Multilevel and Hierarchical Inheritance
- Real-World Applications Of Hybrid Inheritance In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Multiple Inheritance In C++?
- Examples Of Multiple Inheritance In C++
- Ambiguity Problem In Multiple Inheritance In C++
- Ambiguity Resolution In Multiple Inheritance In C++
- The Diamond Problem In Multiple Inheritance In C++
- Visibility Modes In Multiple Inheritance In C++
- Advantages & Disadvantages Of Multiple Inheritance In C++
- Multiple Inheritance Vs. Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Multilevel Inheritance In C++?
- Block Diagram For Multilevel Inheritance In C++
- Multilevel Inheritance In C++ Example
- Constructor & Multilevel Inheritance In C++
- Use Cases Of Multilevel Inheritance In C++
- Multiple Vs Multilevel Inheritance In C++
- Advantages & Disadvantages Of Multilevel Inheritance In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is Hierarchical Inheritance In C++?
- Example 1: Hierarchical Inheritance In C++
- Example 2: Hierarchical Inheritance In C++
- Impact of Visibility Modes In Hierarchical Inheritance In C++
- Advantages And Disadvantages Of Hierarchical Inheritance In C++
- Use Cases Of Hierarchical Inheritance In C++
- Conclusion
- Frequently Asked Questions
Table of content:
- What Are Access Specifiers In C++?
- Types Of Access Specifiers In C++
- Public Access Specifiers In C++
- Private Access Specifier In C++
- Protected Access Specifier In C++
- The Need For Access Specifiers In C++
- Combined Example For All Access Specifiers In C++
- Best Practices For Using Access Specifiers In C++
- Why Can't Private Members Be Accessed From Outside A Class?
- Conclusion
- Frequently Asked Questions
Table of content:
- What Is The Diamond Problem In C++?
- Example Of The Diamond Problem In C++
- Resolution Of The Diamond Problem In C++
- Virtual Inheritance To Resolve Diamond Problem In C++
- Scope Resolution Operator To Resolve Diamond Problem In C++
- Conclusion
- Frequently Asked Questions
For Loop In C++ | Syntax, Working, Types & More (+Code Examples)

A loop in computer programming allows programmers to run a block of code repeatedly for a set number of times or until a particular condition is satisfied. In other words, they allow us to perform repetitive tasks with a high degree of automation and efficiency. There are primarily three kinds of loops: for, while, and do-while.
In this article, we will focus on the for loop in C++ programming and discuss the syntax, its uses, and more with the help of detailed code examples.
As mentioned before, there are generally three types of loops used in C++:
- For loop: It allows users to execute a block of code a specific number of times.
- While loop: It allows users to execute a block of code if a specific condition is true.
- Do-while loop: This allows users to execute a block of code at least once and then repeatedly execute it if a specific condition is true.
What Is A For Loop In C++?
A for loop in C++ language is a fundamental construct that enables developers to iterate over a block of code multiple times. It is frequently used when we already know the number of iterations, making it simpler to write effective and brief code.
- We have a counter variable which is set to the initial value for the first iteration.
- The value of the variable for loop is increased/ decreased (updation expression) after every iteration.
- The C++ for loop iterates as long as the predefined condition (also known as test expression) remains true. Meaning, the loop terminates if the statement expression/ condition becomes false.
This structure allows programmers to control the flow of their code and perform repetitive tasks with ease.
Syntax Of For Loop In C++
for (initialization; condition; increment/decrement) {
// code to be executed
}
Here,
- The for keyword marks the beginning of the for loop.
- The circular braces () contain the loop conditions/ elements and the curly brackets {} contain the code to be executed during every iteration.
The breakdown of the other elements (inside the circular braces) of syntax to create the for loop in C++ is given below.
Elements Of The For Loop In C++
In addition to the components explained above, the C++ for loop has three primary elements:
- Initialization expression: This statement is executed only once, at the beginning of the for loop. It initializes the loop control variable, which keeps track of the frequently/ number of times the loop has been executed. This often involves setting a variable's initial value to 0.
- Condition expression: Every time the C++ for loop iterates, this Boolean expression is evaluated. The loop keeps running if the condition is true. The loop terminates if the condition is false. Any expression that evaluates to a Boolean value can be the loop condition.
- Increment/Decrement: This statement/ expression is executed after every iteration of the loop. It defines how to modify the loop control variable and is hence called the update expression. Typically, this involves incrementing or decrementing the value of the variable by a fixed amount.
How Does A For Loop In C++ Work?
The diagram above illustrates the flow of control through a for loop in C++ programs. The step-by-step working of the basic cpp for loop is as follows:
- Initialization: A counter variable is initialized with a starting value at the beginning of the loop. This variable is used to track the number of the current iteration.
- Loop Condition Check: Before each iteration, we check the value of counter variable as per the conditional limit (e.g., i < 10). If the condition evaluates to true, the loop continues. If the condition evaluates to false, the loop terminates.
- Loop Body Execution: The loop body consists of code statements or block of executable instructions. These instructions are performed for each element in the sequence. It could involve computations, changing data, or calling functions.
- Increment/ Decrement: After executing the loop's body, the counter variable is updated (incremented or decremented). The loop then goes back to step 2 to recheck the condition.
- End: The loop repeats this process until the condition becomes false, at which point the loop stops.
Examples Of For Loop Program In C++
Now that we have a clear understanding of the syntax and functionality of the for loop in C++, let's look at a few code examples.
Example 1: To Find The Factorial Of A Number Using For Loop In C++
In this example, we’ll use a C++ for loop to calculate the factorial of a given number step by step.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CmludCBuLCBmYWN0b3JpYWwgPSAxOwoKY291dCA8PCAiRW50ZXIgYSBudW1iZXI6ICI7CmNpbiA+PiBuOwoKZm9yIChpbnQgaSA9IDE7IGkgPD0gbjsgaSsrKSB7CmZhY3RvcmlhbCAqPSBpOwp9Cgpjb3V0IDw8ICJGYWN0b3JpYWwgb2YgIiA8PCBuIDw8ICIgaXM6ICIgPDwgZmFjdG9yaWFsIDw8IGVuZGw7CgpyZXR1cm4gMDsKfQ==
Output:
Enter a number: 5
Factorial of 5 is: 120
Explanation:
In C++ program above,
- We start by including the <iostream> header file, which allows us to use input and output streams. We also use the namespace std; directive to avoid prefixing standard library names with std::.
- Next, inside the main() function, we declare two variables: n to store the user input and factorial, initialized to 1, to keep track of the factorial result.
- We then use the cout statement to prompt the user to enter a number, read the input using the cin statement, and store it in the variable n.
- Then, we define a for loop to calculate the factorial of the number n. Here:
- We initialize the loop control variable i with 1 to make the loop start from first value.
- After that, we define the loop condition to check if i is less than or equal to n, i.e., i<=n, (using relational operator).
- If the condition is false, the loop terminates. If the condition is true, the loop continues execution of the code block.
- Inside the loop body, we declare a variable factorial and use compound assignment operator to update its value to the current value multiplied by i (i.e., factorial *=i, means, factorial = factorial*i).
- After every iteration, we increment the value of i by 1 to move to the next number.
- As the loop continues iteration, it keeps accumulating the product of all numbers from 1 to n in the variable factorial.
- We repeat this process until i exceeds n (loop condition becomes false), at which point the loop terminates and the variable factorial holds the factorial of n.
- After that, we use the cout command to display the factorial of the number n.
- Finally, the main() function returns 0 to indicate that the program has been executed successfully.
Example 2: To Generate & Print A Fibonacci Series Using For Loop In C++
In this example, we’ll generate a fibonacci series using a for loop in C++ to calculate each term sequentially.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CmludCBuLCB0MSA9IDAsIHQyID0gMSwgdDM7Cgpjb3V0IDw8ICJFbnRlciB0aGUgbnVtYmVyIG9mIHRlcm1zOiAiOwpjaW4gPj4gbjsKCmNvdXQgPDwgIkZpYm9uYWNjaSBTZXJpZXM6ICI7Cgpmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspIHsKY291dCA8PCB0MSA8PCAiLCAiOwp0MyA9IHQxICsgdDI7CnQxID0gdDI7CnQyID0gdDM7Cn0KCnJldHVybiAwOwp9
Output:
Enter the number of terms: 7
Fibonacci Series: 0, 1, 1, 2, 3, 5, 8,
Explanation:
In the example C++ program,
- Inside the main() function, we declare four variables: n to store the number of terms in the Fibonacci series, t1 initialized to 0 (the first term of the series), t2 initialized to 1 (the second term), and t3 to hold the next term in the series.
- Next, we use cout statement to prompt the user to enter the number of terms they want in the Fibonacci series, read the input using cin statement and store it in the variable n.
- We also print a string message marking the Fibonacci series, using cout.
- Then, we define a for loop to generate and print the Fibonacci series, as follows:
- We initialize the loop variable i to 1 and it continues iteration as long as i is less than or equal to n (loop condition).
- In every iteratoin, we use cout to print the current term t1, followed by a comma seperator and a white space.
- Then, we calculate the next term t3 in the series by adding t1 and t2 (using addition operator).
- After that, we update t1 to be the previous value of t2, and t2 to be the new value t3, preparing for the next iteration.
- After every iteration, we increment the value of i by 1 as per the updation condition.
- This loop continues iteration generating and printing the terms of the Fibonacci series until n terms.
Check out this amazing course to become the best version of the C++ programmer you can be.
Ranged Based For Loop In C++
In C++11 and later versions, the C++ language added a new concept known as the range-based for loop, which is far more advanced than the traditional for-loop in C++.
A range-based for loop in C++ is used to iterate through a collection of elements (like arrays, vectors, etc.). For each element in the collection, the loop variable is automatically initialized with the current element's value, sparing the programmer from manually managing an index or loop counter variable.
Syntax Of Range Based For Loop In C++:
for (type variable: collection) {
// code to be executed for each element in the collection
}
Here,
- The term type denotes the data type of the elements stored in the collection, which in turn refers to the data structure that loop will iterate over.
- Variable denotes the loop variable that will store the value of the current collection element, it keeps changing with every iteration.
- The curly braces contain the code block which will be executed for each elementin the collection/ each iteration of the loop.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKSB7CnN0ZDo6dmVjdG9yPGludD4gbnVtYmVycyA9IHsxLCAyLCAzLCA0LCA1fTsKCi8vIFVzaW5nIGEgcmFuZ2UtYmFzZWQgZm9yIGxvb3AgdG8gaXRlcmF0ZSBvdmVyIHRoZSBlbGVtZW50cyBvZiB0aGUgdmVjdG9yCmZvciAoaW50IG51bWJlciA6IG51bWJlcnMpIHsKc3RkOjpjb3V0IDw8IG51bWJlciA8PCAiICI7Cn0KCnJldHVybiAwOwp9
Output:
1 2 3 4 5
Explanation:
In the C++ code example, we include the <iostream> header file to handle input and output, and the <vector> header file to use the std::vector container.
- Inside the main() function, we declare and initialize a vector named numbers with the values {1, 2, 3, 4, 5}.
- Note that a vector in C++ is a dynamic array that can store multiple elements of the same type—in this case, integers.
- We then use a range-based for loop to iterate over each element in the numbers vector:
- In the loop header int number : numbers means that for each iteration, the current element from the numbers vector will be assigned to the variable number.
- During each iteration, the value of number is printed using std::cout, followed by a space to separate the elements.
- The loop automatically ends once all the elements in the vector have been printed.
Temporary Range Expression In For Loops In C++
A range-based for loop can also iterate over temporary range expressions, which are expressions that yield a temporary object to be iterated over. For example, you can use containers like vectors directly in the loop without storing them in variables:
for (auto x : std::vector<int>{11, 22, 33, 44, 55}) {
std::cout << x << " ";
}
In this case, the temporary vector {11, 22, 33, 44, 55} is created and iterated over within the loop without being stored as a variable.
Foreach Loop In C++
The range based for loop in C++ is also sometimes referred to as the foreach loop. An alternative syntax for this loop can be as follows:
for (auto element : container) {
// Do something with element
}
Here, the auto keyword helps automatically determines the type of element based on the container being iterated over. The C++ for loop continues iterations until all elements in the container have been processed.
Level up your coding skills with the 100-Day Coding Sprint at Unstop and earn your bragging rights today!
Nested For Loop In C++
Nested for loops in C++ are loops inside loops. The outer loop is executed once, and the inner loop is executed repeatedly for each iteration of the outer loop.
Syntax Of Nested For Loop In C++:
for (initialization; condition; increment) {
// Outer loop code
for (initialization; condition; increment) {
// Inner loop code
}
}
Here,
- As you can see, the second occurrence of for keyword is inside the curly braces following the first for keyword. This is the basic structure of nested loops, where we have an inner loop inside the outer loop. Both have initialization, condition, and increment statements.
- The initialization/ initial expression defines how to initialize the counter variables used in the loop statement.
- We define a condition to check if the loop should continue iterating, also referred to as loop condition.
- The increment statement defines how to update the loop variables after each iteration of loop.
Note that the inner loop completes a set number of iterations, for every single iteration of the outer loop. Look at the sample C++ program below which shows the implementation of nested for loops.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgoKaW50IG1haW4oKSB7CmNvbnN0IGludCByb3dzID0gMzsKY29uc3QgaW50IGNvbHMgPSAzOwoKaW50IG1hdHJpeDFbcm93c11bY29sc10gPSB7ezEsIDIsIDN9LCB7NCwgNSwgNn0sIHs3LCA4LCA5fX07CmludCBtYXRyaXgyW3Jvd3NdW2NvbHNdID0ge3s5LCA4LCA3fSwgezYsIDUsIDR9LCB7MywgMiwgMX19OwppbnQgc3VtW3Jvd3NdW2NvbHNdOwoKLy8gT3V0ZXIgbG9vcCBpdGVyYXRlcyBvdmVyIHRoZSByb3dzCmZvciAoaW50IGkgPSAwOyBpIDwgcm93czsgaSsrKSB7Ci8vIElubmVyIGxvb3AgaXRlcmF0ZXMgb3ZlciB0aGUgY29sdW1ucwpmb3IgKGludCBqID0gMDsgaiA8IGNvbHM7IGorKykgewovLyBDYWxjdWxhdGUgdGhlIHN1bSBvZiB0aGUgY29ycmVzcG9uZGluZyBlbGVtZW50cyBpbiB0aGUgbWF0cmljZXMKc3VtW2ldW2pdID0gbWF0cml4MVtpXVtqXSArIG1hdHJpeDJbaV1bal07Cn0KfQoKLy8gUHJpbnQgdGhlIHN1bSBtYXRyaXgKc3RkOjpjb3V0IDw8ICJUaGUgc3VtIG9mIHRoZSB0d28gbWF0cmljZXMgaXM6XG4iOwpmb3IgKGludCBpID0gMDsgaSA8IHJvd3M7IGkrKykgewpmb3IgKGludCBqID0gMDsgaiA8IGNvbHM7IGorKykgewpzdGQ6OmNvdXQgPDwgc3VtW2ldW2pdIDw8ICIgIjsKfQpzdGQ6OmNvdXQgPDwgIlxuIjsKfQoKcmV0dXJuIDA7Cn0=
Output:
The sum of the two matrices is:
10 10 10
10 10 10
10 10 10
Explanation:
In this code example-
- We first declare and initialize two constant integer variables, rows and cols, with values of 3. They represent the dimensions (3x3) of the matrices.
- Next, we declare two 2D arrays, matrix1 and matrix2, with three rows and columns.
- The matrix1 holds the elements {1, 2, 3}, {4, 5, 6}, {7, 8, 9} while matrix2 holds {9, 8, 7}, {6, 5, 4}, {3, 2, 1}.
- We then declare a third 2D array/ integer matrix called sum (dimensions rows*cols) to store the outcome of the addition operation on first two arrays.
- Next, we use a nested for loop to iterate over the elements of the matrices and carry out the addition, as follows:
- The outer loop counter variable i tracks the rows, and the inner loop counter variable j tracks the columns.
- Inside the inner loop, we add the elements at the (i*j)th position in matrix1 and matrix2 and store it at the (i*j)th position in the sum matrix.
- The inner loop iterates through all the columns for every single row iteration by the outer loop.
- After both the loops terminate, the sum array will store the addition of matrix1 and matrix2.
- As mentioned in the code comments, we then print the sum array (representing the sums), using another set of nested for loops and std::cout statement.
- Once again, the outer loop iterates over the rows and inner loops iterates through all the columns in that row, printing the elements as we go.
- After a row is printed (inner loop), print the newline escape sequence which shifts the cursor to the next line before another iteration of the outer loop.
- Finally, the program ends with return 0, indicating successful execution.
Infinite For Loop In C++
A loop that never ends and continues to execute until the program is manually halted is known as an infinite for loop. In C++, we can create an infinite loop by using a condition that is always true or by leaving the termination condition empty.
Syntax Of Infinite For Loop In C++:
for (;condition; updating expression) {
// body of the loop
}
Here,
- As usual the for keyword marks the beginning of the loop, which does not have an initialization expression.
- Expression condition refers to the loop condition which we check to determine if the loop should continue iterating. In case of infinite for loop, this condition is always true, the loop will run indefinitely.
- The updating expression defines how to update loop variable after every iteration. Alternatively, we can omit this expression which will may also lead to the loop running infinitely.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CmludCBpID0gMDsKCmZvciAoOyBpID49IDA7IGkrKykgewpjb3V0IDw8ICJUaGlzIGlzIGFuIGluZmluaXRlIGxvb3AiIDw8IGVuZGw7Cn0KCnJldHVybiAwOwp9
Output:
This is an infinite loop
.............
.............
Explanation:
In the above code example,
- We declare an integer variable i and assign the value of 0, inside the main() function.
- The for loop the condition is that i be greater than or equal to zero (i >= 0), we check it before every iteration. And after every iteration we increment the value of i by 1.
- Inside the loop, we print the message "This is an infinite loop" to the console using cout and then move to the next line using endl.
- The loop condition will always be true as i starts at 0 and increases with each iteration, meaning it will never be less than 0.
- As a result, the loop keeps running indefinitely, making an infinite for loop.
- Note that even though there's a return 0; at the end of the program, it will never be reached because of the infinite loop.
Looking for mentors? Find the perfect mentor for select experienced coding & software experts here.
Conclusion
The for loop in C++ is a strong tool for repeating tasks or iterating across various data sets/ different types of data structures. In other words, it can be used for a fixed number of iterations as well as for iterating through arrays and other data structures.
- This loop is made up of three primary parts: initialization, condition, and update expression. They respectively define how the loop variable is initialized, the condition to check if the loop should continue for another iteration, and how the loop variable should be updated to ready for the next iteration.
- The typical for-loop in C++ enables fine-grained control over the loop's behavior.
- With the introduction of the ranged-based for loop in C++, iterating over a set of values or a container, like an array or vector, is made simpler. The shorter form of this loop's syntax makes the code more effective and understandable.
- The for loop and its variants are robust C++ building blocks that enable effective and adaptable control over program flow and data processing.
It is essential to choose the right type of for-loop in C++ programs, to ensure that the code is more effective, readable, and maintainable.
Also read- 51 C++ Interview Questions For Freshers & Experienced (With Answers)
Frequently Asked Questions
Q. Can we put a for loop in a for loop in C++?
Yes, you can use a for loop inside another for loop in C++ programming language. The concept of building a construct with one for loop inside another for loop, is known as a nested for loop in C++. Below is an example for nested for loop in C++ language.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgoKaW50IG1haW4oKSB7CmludCByb3dzOwoKc3RkOjpjb3V0IDw8ICJFbnRlciB0aGUgbnVtYmVyIG9mIHJvd3M6ICI7CnN0ZDo6Y2luID4+IHJvd3M7Cgpmb3IgKGludCBpID0gMTsgaSA8PSByb3dzOyArK2kpIHsKZm9yIChpbnQgaiA9IDE7IGogPD0gaTsgKytqKSB7CnN0ZDo6Y291dCA8PCAiKiAiOwp9CnN0ZDo6Y291dCA8PCAiXG4iOwp9CgpyZXR1cm4gMDsKfQ==
Output:
Enter the number of rows: 3
*
* *
* * *
Explanation:
This program asks the user how many rows they want to print and then prints a pattern of stars using a nested for loop. The outer loop iterates through each row, while the inner body of loop iterates through each column within that row. A star is printed to the console after every inner loop iteration. This results in a pattern of stars, with each row having one more star than the one before it.
Q. What is ++ I and I ++ in for loop?
The notations ++I and I++ in for loop represent the increment operators in C++. They are the pre-increment and post-increment operators, respectively.
- While both increase the number, the first notation (++i) increases it before the current expression is evaluated.
- And the second notation (i++) increases it after the expression is evaluated.
Below is an example to give you a better understanding of this concept.
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7CmludCB4ID0gNTsKCi8vIFVzaW5nIHBvc3QtaW5jcmVtZW50IG9wZXJhdG9yCmludCB5ID0geCsrOyAvLyB5ID0gNSwgeCA9IDYKCi8vIFVzaW5nIHByZS1pbmNyZW1lbnQgb3BlcmF0b3IKaW50IHogPSArK3g7IC8vIHogPSA3LCB4ID0gNwoKY291dCA8PCAieSA9ICIgPDwgeSA8PCBlbmRsOwpjb3V0IDw8ICJ6ID0gIiA8PCB6IDw8IGVuZGw7CmNvdXQgPDwgInggPSAiIDw8IHggPDwgZW5kbDsKCnJldHVybiAwOwp9
Output:
y = 5
z = 7
x = 7
Explanation:
Here, we first declare the integer variable x and set its initial value to 5.
- The value of x is then assigned to another integer variable, y using the post-increment operator (++), and x is then increased by 1 unit after that.
- As a result, the values of y and x are 5 and 6, respectively.
- The value of x is then increased by 1 using the pre-increment operator (++) before its new value is sent to z.
- The end consequence is that both z and x have values of 7.
- Finally, we print the values of y, z, and x using cout.
Q. What are the three types of loops?
In computer programming, a loop enables developers to run a block of code repeatedly, without having to write the code again and again, for a certain number of times or up until a specific condition is met. When writing computer programs, loops give programmers the ability to automate and efficiently complete repetitive tasks.
There are generally three types of loops used:
- For loop: which allows you to execute a block of code a specific number of times.
- While loop: which allows to execute a block of code if a specific loop condition is true.
- Do-while loop: This allows you to execute a block of code at least once and then repeatedly execute it if a specific condition is true.
Q. What is a Goto statement in C++?
The goto statement in C++ is a control transfer statement that allows the program execution to jump to a specific labeled statement within the same function or block. The program's execution jumps to the statement indicated by the label when the goto statement occurs in the code and continues from there.
The general syntax of the goto statement in C++ is as follows:
goto label;
...
...
label;
...
...
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgoKaW50IG1haW4oKSB7CmludCBpID0gMDsKCnN0YXJ0OgppZiAoaSA8IDUpIHsKc3RkOjpjb3V0IDw8IGkgPDwgIiAiOwppKys7CmdvdG8gc3RhcnQ7Cn0gZWxzZSB7CnN0ZDo6Y291dCA8PCAiRG9uZSEiOwp9CgpyZXR1cm4gMDsKfQ==
Output
0 1 2 3 4 Done!
Explanation:
In this illustration, we use a loop statement that outputs i's values from 0 to 4 is implemented using the goto instruction. The start label identifies the statement to which program execution will jump, and the loop continues there until the condition is no longer true. The program switches to the else block and writes "Done!" when i reaches 5.
Q. What is return 0 used for in C++?
The return 0 statement is typically used at the conclusion of the main function in C++. It statement suggests that the program has run successfully and returns a value of 0 to the calling process. In other words, the 0-return result implies the successful completion of the program without any errors.
Here's an example of how return 0 can be used at the end of the main function:
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgoKaW50IG1haW4oKSB7CnN0ZDo6Y291dCA8PCAiQmUgdW5zdG9wcGFibGVcbiI7CnJldHVybiAwOwp9
Output:
Be unstoppable
Example:
In this example, the main function prints "Be unstoppable" to the console and returns a value of 0 to the calling process.
Q. How do I get out of a loop in CPP?
Depending on the type of loop and the circumstance, there are several ways to escape a loop in C++. Some of the most common ways to do this are, using any of the break, exit, and Goto statements.
- In a loop such as for, while, or do-while, the break statement can be used to immediately exit the loop and continue executing the program outside of the loop.
- While the goto statement can also be used to jump out of a loop to a labeled statement outside the loop.
- The exit function in C++ is used to end a program completely and hand control back to the operating system.
- When a fatal error occurs, or the program needs to end for whatever reason, the exit function is commonly utilized.
Q. Can we use the for each loop in C++?
Yes, a for-each loop, also referred to as a range-based loop, is a condensed form of the standard for loop. This type of Cpp for loop offers a simpler approach to iterating through container elements and was first introduced in C++11.
The for-each loop comes in handy when we don't need to know the current element's index or when we don't know the container size in advance. It makes the code more readable and maintainable by streamlining the syntax and lowering the chance of errors. For example, to print all the elements of a vector using a for-each loop, we can write:
Code Example:
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8dmVjdG9yPgoKaW50IG1haW4oKSB7CnN0ZDo6dmVjdG9yPGludD4gbnVtcyA9IHsxLCAyLCAzLCA0LCA1fTsKCmZvciAoaW50IG51bSA6IG51bXMpIHsKc3RkOjpjb3V0IDw8IG51bSA8PCAiICI7Cn0KCnJldHVybiAwOwp9
Output:
1 2 3 4 5
Explanation:
The for-each type of for loop in C++ shortens the code and makes it easy to integrate over elements.
Test Your Skills: Quiz Time
Here are a few other articles that you might be interested in reading to know more about interesting topics in C++:
- Strings In C++ | Functions, How To Convert & More (With Examples)
- Typedef In C++ | Syntax, Application & How To Use It (With Examples)
- Storage Classes In C++ & Its Types Explained (With Examples)
- References In C++ | Declare, Types, Properties & More (+Examples)
- Dynamic Memory Allocation In C++ Explained In Detail (With Examples)
An economics graduate with a passion for storytelling, I thrive on crafting content that blends creativity with technical insight. At Unstop, I create in-depth, SEO-driven content that simplifies complex tech topics and covers a wide array of subjects, all designed to inform, engage, and inspire our readers. My goal is to empower others to truly #BeUnstoppable through content that resonates. When I’m not writing, you’ll find me immersed in art, food, or lost in a good book—constantly drawing inspiration from the world around me.
Comments
Add commentLogin to continue reading
And access exclusive content, personalized recommendations, and career-boosting opportunities.
Krishna Anand 1 day ago
Absar Ansari 1 day ago
Divyansh Shrivastava 1 day ago