EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login

Control Statement in C++

Home » Software Development » Software Development Tutorials » C ++ Programming Tutorial » Control Statement in C++

Control Statement in C++

Introduction to Control Statement in C++

A control statement is used in a programming language to control the flow of the program. They are nothing but a keyword or statements that are used in a program to transfer the flow of control to another statement based on the conditions. Based on the given condition, it evaluates the result and executes the corresponding statements. Control statements are the statement that controls the flow of the program in order to execute the piece of the code using various controls statement like if statement, if-else statement, break statement, continue statement, for loop, while loop, do while loop. In this article, we are going to discuss the various control statements available in the C++ language with the help of examples.

Different Control Statement in C++

Below is the different control statement in C++.

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

1. C++ Switch Statement

From the multiple conditions, a C++ Switch statement executes a single statement. It’s like a ladder statement if-else-if in C++.

Syntax of C++ Switch statement

Switch(expression)
{
case value1:
//code should be executed;
break;
case value2:
//code should be executed;
break;
…
Default:
//Code to execute if not all cases matched
break;
}

Example of C++ Switch Statement

#include<iostream>
using namespace std;
int main () {
int number;
cout << "To check the grade enter a number:";
cin >> number;
switch (number)
{
case 2: cout << "It is 2"; break;
case 3: cout << "It is 3"; break;
case 4: cout << "It is 4"; break;
default: cout << "Not 2, 3 or 4"; break;
}
}

Output:

check the grade

2. C++ if-else statement

To test the condition in C++ programming if the statement is been used. They are different types of if statement

  • If statement in C++
  • If-else statement in c++
  • If-else-if ladder in c++
a. If statement in C++

C++ if the condition is evaluated by the argument. If the condition is valid, it is executed.

Popular Course in this category
C++ Training (4 Courses, 5 Projects, 4 Quizzes)4 Online Courses | 5 Hands-on Projects | 37+ Hours | Verifiable Certificate of Completion | Lifetime Access | 4 Quizzes with Solutions
4.5 (4,899 ratings)
Course Price

View Course

Related Courses
Java Training (40 Courses, 29 Projects, 4 Quizzes)C Programming Training (3 Courses, 5 Project)

Syntax of if statement in c++

if(condition)
{
//code should be executed;
}

Example of if Statement

#include <iostream>
using namespace std;
int main () {
int number = 10;
if (number % 2 == 0)
{
cout << "The Number you have Enter it is Even";
}
return 0;
}

Output:

number you have entered

b. If else statement in C++

The statement C++ if-else also checks the condition. The declaration executes if the condition is true otherwise the block is carried out.

Syntax of the if-else statement in c++

if(condition)
{
//code should be executed;
}else
{
//code should be executed;
}

Example of if-else Statement

#include<iostream>
using namespace std;
int main () {
int number = 15;
if (number % 2 == 0)
{
cout << "The Number you have Enter it is Even";
}
else
{
cout << "The Number you have Enter it is Odd";
}
return 0;
}

Output:

enter it is odd

c. If-else-if ladder statement in C++

The C++ if-else-if ladder declaration executes from multiple statements in one condition.

Syntax of if-else ladder statement in c++

If(condition1)
{
// code should be executed if condition1 is true
}
else if(condition2)
{
// code should be executed if condition2 is true
}
else if(condition3)
{
// code should be executed if condition3 is true
}
. . .
else{
// code should be executed if all condition is false
}

Example of if-else ladder Statement     

#include <iostream>
using namespace std;
int main () {
int number;
cout << "To Check Grade Enter a Number:";
cin >> number;
if (number < 0 || number  > 100)
{
cout << "wrong No";
}
else if(number >= 0 && number < 40){
cout << "Fail";
}
else if (number >= 40 && number < 59)
{
cout << "D Grade";
}
else if (number >= 60 && number < 70)
{
cout <<" C Grade";
}
else if (number >= 71 && number < 79)
{
cout << "B Grade";
}
else if (number >= 80 && number < 89)
{
cout << "A Grade";
}
else if (number >= 90 && number <= 100)
{
cout << "A+ Grade";
}
}

Output:

if else ladder statement in c++

3. For Loop in C++

The C++ loop is used multiple times to iterate a part of the program. It is recommended that you use for loops when the iteration number is set. For loops, it is recommended.

Syntax of for loop statement in c++

For(initialization; condition; incr/decr){
//code should be executed;
}

Example of for loop Statement

#include <iostream>
using namespace std;
int main() {
for(int i = 2; i <= 20; i++){
cout << i << "\n";
}
}

Output:

For loop in c++

4. C++ Nested For Loop

In C++, we can use the loop inside the loop, called loop nest. The inner loop is fully executed once the external loop is executed.

Example of Nested Loop in C++

#include<iostream>
using namespace std;
int main () {
for(int i = 1; i <= 3; i++){
for(int j = 1; j <= 3; j++){
cout << i << " "<< j << "\n";
}
}
}

Output:

c++ nested for loop

5. C++ While Loop

In C++, the loop is used several times for the iteration of a part of the program. If the iteration number is not set, it is advisable to use the loop rather than the loop.

Syntax of while loop statement in c++

While(condition0
{
//code should be executed;
}

Example of while Loop in C++

#include<iostream>
using namespace std;
int main() {
int i = 5;
while(i <= 20)
{
cout << i  << "\n";
i++;
}
}

Output:

 c++ while statement

Nested Example of While loop in C++

#include<iostream>
using namespace std;
int main () {
int i = 2;
while(i <= 5)
{
int j = 3;
while (j <= 3)
{
cout << i << " " << j << "\n";
j++;
}
i++;
}
}

Output:

nested example of while statement in c++

6. Do while loop statement in C++

C++ is used many times to iterate a part of the software. It is advised that you use a do-while loop, if the number of iteration is not known and the loop must be performed at least once.

Syntax of a do-while loop statement in c++

do
{
//code should be executed;
}
While(condition);

Example of do-while loop statement;

#include<iostream>
using namespace std;
int main() {
int j = 2;
do{
cout << j << "\n";
j++;
} while (j <= 10) ;
}

Output:

do while loop statement in c++

Nested Do-while loop statement in C++

In C++, when you use do-while in another do-while loop, the nested do-while loop is known. For each external loop the nestled do-whilst loop is completely executed.

#include <iostream>
using namespace std;
int main() {
int j = 1;
do{
int k = 1;
do{
cout << j << "\n";
k++;
} while (k <= 4) ;
j++;
} while (j <= 3) ;
}

Output:

nested do while statement in c++

7. Break Statement in C++

The break C++ is used for loop breakage or statement switching. It breaks the program’s current flow in the given state. In the case of an inner loop, only an internal loop splits.

Syntax of break statement in C++

Jump-statement;
break;

Example of Break statement in C++

#include<iostream>
using namespace std;
int main() {
for (int j = 1; j <= 10; j++)
{
if (j == 10)
{
break;
}
cout << j << "\n";
}
}

Output:

break statement in c++

Inner loop break statement in C++

The C++ break declaration only breaks the inner loop if you use an inside break statement.

#include<iostream>
using namespace std;
int main()
{
for(int j = 1; j <= 5; j++){
for(int k = 1; k <= 3; k++){
if(j == 2&&k == 2){
break;
}
cout << j << " " << k << "\n";
}
}
}

Output:

inner loop break statement in c++

8. Continue Statement in C++

The declaration C++ is used for the continuation of the loop. The current program flow continues and the remaining code is omitted at a specified state. If there is an inner loop, only an inner loop continues.

Syntax of continue statement in C++

Jump-statement;
Continue;

Example of break statement in C++

#include<iostream>
using namespace std;
int main()
{
for(int j = 1; j <= 10; j++){
if(j == 10){
continue;
}
cout << j << "\n";
}
}

Output:

continue statement in c++

9. Goto statement in C++

The C+ + goto declaration is also called a jump declaration. The control to the other part of the program is transferred. It saves to the specified label unconditionally.

Example of Goto Statement in C++

#include<iostream>
using namespace std;
int main()
{
ineligible:
cout << "For the driving you are not eligible \n";
cout << "Please enter your Age:\n";
int age;
cin >> age;
if (age < 18){
goto ineligible;
}
else
{
Cout << "You are eligible for driving!";
}
}

Output:

Go To statement

Recommended Articles

This is a guide to Control Statement in C++. Here we discuss the basic concept, Different Control Statement in C++ along with the various Syntax, Examples, and Outputs. You can also go through our other suggested articles to learn more–

  1. Goto Statement in C
  2. Continue Statement in C++
  3. Control Statement in PHP
  4. Control Statement in JavaScript

C++ Training (4 Courses, 3 Projects, 4 Quizzes)

4 Online Courses

5 Hands-on Projects

37+ Hours

Verifiable Certificate of Completion

Lifetime Access

4 Quizzes with Solutions

Learn More

2 Shares
Share
Tweet
Share
Primary Sidebar
C plus plus Programming Tutorial
  • Control Statements
    • Control Statement in C++
    • if else Statement in C++
    • Else If in C++
    • Nested if in C++
    • Continue Statement in C++
    • Break Statement in C++
    • Switch Statement in C++
    • goto Statement in C++
    • C++ Struct
    • Loops in C++
    • Do While Loop in C++
    • Nested Loop in C++
  • Basic
    • Introduction To C++
    • What is C++
    • Features of C++
    • Applications of C++
    • Best C++ Compiler
    • C++ Data Types
    • C++ Double
    • C++ unsigned int
    • User Defined Data Types in C++
    • Variables in C++
    • C++ Keywords
    • Pointers in C++
    • C++ Void Pointer
    • Function Pointer in C++
    • Iterator in C++
    • C++ Commands
    • Object in C++
    • C++ Literals
    • C++ Reference
    • C++ Undefined Reference
    • String in C++
    • C++ Programming Language (Basics)
    • C++ Identifiers
    • C++ Header Files
    • Type Casting in C++
    • C++ Formatter
  • Operators
    • C++ Operators
    • Arithmetic Operators in C++
    • Assignment Operators in C++
    • Bitwise Operators in C++
    • Relational Operators in C++
    • Boolean Operators in C++
    • Unary Operators in C++
    • C++ Operator[]
    • Operator Precedence in C++
    • C++ operator=()
  • Functions
    • C++ String Functions
    • Math Functions in C++
    • Friend Function in C++
    • Recursive Function in C++
    • Virtual Functions in C++
    • strcat() in C++
    • swap() in C++
    • strcmp() in C++
    • ceil function in C++
    • C++ begin()
    • size() in C++
    • C++ test()
    • C++ any()
    • C++ Bitset
    • C++ find()
    • C++?Aggregation
    • C++?String append
    • C++ String Copy
    • C++ end()
    • C++ endl
    • C++ push_back
    • C++ shuffle()
    • malloc() in C++
    • C++ reserve()
    • C++ unique()
    • C++ sort()
    • C++ find_if()
    • Reflection in C++
    • C++ replace()
    • C++ search()
    • C++ Memset
    • C++ size_t
    • C++ Substring
    • C++ Max
    • C++ absolute value
    • C++ memcpy
    • C++ wchar_t
    • C++ free()
    • C++ sizeof()
    • C++ Move Semantics
  • Array
    • Arrays in C++
    • 2D Arrays in C++
    • 3D Arrays in C++
    • Multi-Dimensional Arrays in C++
    • C++ Array Functions
    • String Array in C++
    • C++ Length of Array
    • C++ arraylist
  • Constuctor and Destructor
    • Constructor and Destructor in C++
    • Constructor in C++
    • Destructor in C++
    • Copy Constructor in C++
    • Parameterized Constructor in C++
  • Overloading and overriding
    • Overloading and Overriding in C++
    • Overloading in C++
    • Overriding in C++
    • Function Overloading in C++
    • Function Overriding in C++
    • Method Overloading in C++
  • Inhertiance
    • Types of Inheritance in C++
    • Single Inheritance in C++
    • Multiple Inheritance in C++
    • Hierarchical Inheritance in C++
    • Multilevel Inheritance in C++
    • Hybrid Inheritance in C++
  • Sorting
    • Sorting in C++ 
    • Heap Sort in C++
    • C++ Vector Sort
    • Insertion Sort in C++
    • Selection Sort in C++
  • Advanced
    • C++ namespace
    • Encapsulation in C++
    • Access Modifiers in C++
    • Abstract Class in C++
    • C++ Class and Object
    • What is Template Class in C++?
    • C++ Algorithm
    • Data Structures and Algorithms C++
    • C++ Garbage Collection
    • Virtual Keyword in C++
    • Access Specifiers in C++
    • Storage Class in C++
    • Call by Value in C++
    • Multimap in C++
    • C++ Multiset
    • C++ Lambda Expressions
    • Stack in C++
    • C++ Static
    • C++ static_cast
    • Deque in C++
    • C++ Vector Functions
    • C++ 2D Vector
    • C++ List
    • C++ Mutable
    • Enum in C++
    • Abstraction in C++
    • Signal in C++
    • C++ Queue
    • Priority Queue in C++
    • Regular Expressions in C++
    • C++ Hash Table
    • File Handling in C++
    • C++ Stream
    • ifstream in C++
    • C++ ofstream
    • C++ fstream
    • C++ Read File
    • C++ iomanip
    • Macros in C++
    • Templates in C++
    • C++ setprecision
    • C++ Int to String
    • C++ thread( )
    • C++ Thread Pool
    • C++ thread_local
  • Programs
    • Patterns in C++
    • Star Patterns In c++
    • Swapping in C++
    • Reverse Number in C++
    • Palindrome Program in C++
    • Palindrome in C++
    • Factorial Program in C++
    • Fibonacci Series in C++
    • Square Root in C++
    • Random Number Generator in C++
    • Prime Number in C++
    • Leap Year Program in C++
    • Anagram in C++
    • Armstrong Number in C++
    • Reverse String in C++
    • Socket Programming in C++
    • Matrix Multiplication in C++
    • C++ using vs typedef
    • C++ vector vs list
    • C++ vector vs array
  • Interview question
    • C++ Interview Questions
    • Multithreading Interview Questions C++

Related Courses

C++ Training Course

Java Training Course

C Programming Course

Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

*Please provide your correct email id. Login details for this Free course will be emailed to you