Home Website & Development Control Flow Statements in C++ Programming

Control Flow Statements in C++ Programming

In a programming language, statements, or structures that are used to control the flow of execution of programs are called control statements (structures).

A program is a group of structures. Row Wise is written in the Structure program, but at the time of execution, their execution doesn’t need to be from top to bottom. The computer cannot make decisions on its own, gets it done as per the instructions of the programmer to take decisions.

The execution section of the Structure in the computer and the statements that are used for decisions are called Control-flow Statements.

INDEX

  1. Types of Control Statements in c++ programming
  2. Branching or Conditional Structure
  3. if
  4. if-else  statements
  5. Switch Statements
  6. Else if Statement
  7. Iterative or looping Structure
  8. While Loop
  9. Do-while loop
  10. For loop
  11. Nested loop
  12. Sequential Control Flow Structure
  13. jump statements
  14. Break statement
  15. Continue statement
  16. Go to statement
  17. Conclusion
  18. Frequently asked questions

Types of Control Statements in C++ programming

Control Flow Statements in C++ Programming

 1. Branching or Conditional Structure –

In the normal state, execution of the program is gradual execution. When the Sequential Execution of the program is blocked by the selected Statements, it is called Branches Execution.

Control of the program requires a condition to block selected statements. That is, the control of the program is done by blocking the selected statements based on a condition, then the statement used in it is called Conditional Statement.

C ++ consists of three Conditional Statements –

Branching or Conditional Structure

1. if statement –

The if statement is a control statement that is used to test a particular condition. In this, the condition is executed only once when the condition is true.

If the condition is true in the statement then the statement is Execute.

Syntax –
if(condition)
{
statements
}

Example –

#include <iostream>
using namespace std;
 
int main(){
 
int a=100, b=200;
  if (a < b)
  {
  cout<<“a is less than b”;
  }
return 0;
}
note – this program run in c++ IDE compiler

Output –

2. if else statements –

The if-else statement is used to test a particular condition. If the condition is true then the if statement is executed if the condition is false then the else statement is executed.

Syntax –
if(condition)
{
statement
}
else
{
statement
}

Example –

#include <iostream>
using namespace std;
 
int main(){
 
int a=10, b=10;
 
if( a == b )
{
           
    cout<<“a is equal to b”;
}
else
{
    cout<<“a is not equal to b”;
}
return 0;
note – this program run in c++ IDE compiler

Output –

3. switch statements –

This statement is also a selection statement that defines various paths for the execution of a program.

This serves as an alternative to the if-else statement.

Switch case statement has expression and some cases related to it. The case which matches that expression or declares variable is printed in the output.

If no case matches the expression then it will print the default statement in the output. You have to break after every statement, that means it will print only the statement before it.

If you do not break, then it will print both the first and the second statement. Do not break after the default case.

 syntax:-
switch(variable)
{
case constant 1;
statements(s);
break:
case constant 2;
statement(s);
break;
case constant 3;
statement(s);
break;
———–
default
statement(s);
}

Example –

#include <iostream>
using namespace std;
 
int main(){
 
char Day=’A’;
 
switch(Day){
 
            case ‘A’ :
            cout<<“Today is Sunday”;
            break;
 
            case ‘B’ :
            cout<<“Today is Monday”;
            break;
 
            case ‘C’ :
            cout<<“Today is Tuesday”;
            break;
 
            case ‘D’ :
 
            case ‘E’ :
            cout<<“Today is Wednesday”;
            break;
 
            case ‘F’ :
            cout<<“Today is Thurday”;
            break;
 
            case ‘G’ :
            cout<<“Today is Friday”;
            break;
 
            case ‘H’ :
            cout<<“Today is Saturday”;
            break;
 
            default :
            cout<<“Day is Not Found”;
}
return 0;
}

Output –

4. else if Statement –

the condition of if is true in else_if statement then statement of if is executed.

the condition of if is false, then it goes to the next condition and checks.

condition is true, then it executes its statement.

no condition is true, then it executes the statement of else.

 Syntax for else if Statement

if(condition){
  statement(s);
}else if(condition){
statement(s);
}else{
statement(s);
}

Example –

#include<iostream>
using namespace std;
 
int main(){
 
int a=100, b=20;
 
if( a < b ){
    cout<<“a is less than b”;
}else if( a > b ){
    cout<<“a is greater than b”;
}else{
    cout<<“a is equal to b”;
}
return 0;
}

Output –

2.  Iterative or looping Structure –

Looping or Reversible Statement Program sometimes requires the same statement to be used repeatedly, for this C ++ has three methods.

Using which we can execute statements repeatedly which are the following are also called Looping Statements.

Through loops, we can execute anyone’s statement or many abstract statements more than once until the condition is achieved.

loop

1. While loop –

while loop is an entry-controlled loop. In this, the statement keeps executing continuously until a condition is true. It first checks the condition and later executes the statement.

It is necessary to initialize the variable in While Loop.

If while Loop is to be repeated, the increment/decrement operator is used.

Repeats until the condition in While Loop is true.

syntax:-
while(condition)
{
statement1;
statement2;
————-
}

Example –

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int x = 41;
 
   // while loop execution
   while( x < 50 ) {
      cout << “value of x: ” << x << endl;
      x++;
   }
 
   return 0;
}

Output –

2. Do while loop –

This loop is  exit-controlled. This loop is also like a while loop but it executes the first statement and checks the condition later.

Repeats until the condition in Do-While Loop is true.

 The specialty of Do-While is that, even if the condition is false, it prints a statement in the output.

This loop ensures that the program is executed at least once.

syntax:-
do
{
Statement1;
Statement2;
————–
}
While (condition);

 Example –

#include <iostream>
using namespace std;
 
int main () {
   // Local variable declaration:
   int x = 99;
 
   // do loop execution
   do {
      cout << “value of x12: ” << x << endl;
      x = x + 1;
   } while( x < 110 );
 
   return 0;
}

Output –

3. For loop –

For Loop only needs a Variable Declaration. Except for this statement, he does all the work inside himself.

Repeats until the condition in While Loop is true.

This is an entry controlled loop. In this, the statement is executed until the condition becomes true. This loop consists of three components initialization statement, Boolean statement, and increment/decrement statement.

syntax:-
for(initial condition, test condition; incrementor or decrement)
{
statement1;
statement2;
}

Example –

#include <iostream>
using namespace std;

int main () {
   int i, j;
  
   for(i = 2; i<20; i++) {
      for(j = 2; j <= (i/j); j++)
         if(!(i%j)) break; // if factor found, not prime
         if(j > (i/j)) cout << i << ” is prime\n”;
   }
  
   return 0;

Output –

4. Nested loop –

Nested loop in C ++ Programming is not a type of loop. Here, in a program, you use the same loop two or more times, it is called a nested loop.

For loop is used in this program. If you want, you can also use the if, while, switch case. It has no syntax, it depends on writing your program.

while loops are executed only. As the name suggests, another loop can be executed inside a loop.

Example –

#include <iostream>
using namespace std;
 
int main ()
{
   int i = 0;
  
   while(i < 3)
   {
       int j = 0;
       while(j < 5)
       {
           cout << “i = ” << i << ” and j = ” << j << endl;
           j++;
       }
       i++;      
   }
   return 0;
}

Output –

3. Sequential Control Flow Structure –

The statements written in the program are implemented one after the other.

The statements are executed in the order in which the compiler is received.

This execution of the program is called serial execution.

Jump statements –

Jump statements are used to interrupt the normal flow of program.

Jump statement

1. Break statement –

This statement is used to end a sequence of statements in a switch statement and to immediately exit a loop.

The execution of the loops and switch cases of the Break Statement Program stops at any condition.

syntax:- break;

Example –

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

Output –

2. Continue statement –

The continue statement is used when we want to run the loop continues with the next iteration and skip other statements in the loop for the current iteration.

Depending on the condition of the loops of the Continue Statement program, skip the middle statements, and execute the subsequent statements.

syntax:- continue;

Example –

#include <iostream> 
using namespace std; 
int main() 

     for(int i=1;i<=10;i++){     
            if(i==5){     
                continue;     
            }     
            cout<<i<<“\n”;     
        }       
}
note – this program run in c++ IDE compiler

Output –

3. Go to  statement –

Go to is the statement of C ++ Programming. Labels are used in this.

There are two types of Go to Statements.

Forward

Backward

When a goto statement executes its next statement except for some statement, it is called Forward goto statement and goes to its previous label to execute any previous or executed statement again, it is called Backward goto statement.

Syntax for Forward and Backward goto Statement

Syntax for Forward

goto label ;
statement ;
———–
label ;

Syntax for Backward

label ;
statement ;
———–
goto label ;

Example –

#include <iostream>
using namespace std;
int main()
{
   int num; cout<<“Enter a number: “;
    cin>>num;
   if (num % 2==0){
      goto print;
   }
   else {
      cout<<“Odd Number”;
   }
 
   print:
   cout<<“Even Number”;
   return 0;
}

Output –

source – www.cplusplus

Conclusion –

Types of Control Statements -Branching or Conditional Structure,Iterative or looping Structure, Sequential Control Flow Structure all of them Syntax, examples, outputs I gave you above.

Friends, reading this post completely, you must have understood Control Statements in c++ programming. you must have liked it. I try to describe to you in simple language, maybe you must have understood it in this post. I have covered Topics you do not need to read any other post. If you liked this article, then definitely share it with your friends. And there is some doubt. Then comment I will definitely answer your comment.

Frequently asked questions

1. What are the control statements in C++?

Control statements are elements in the source code that control the flow of program execution.

2. What is Compound Statement?

 A compound statement is a grouping of statements in which each statement ends with a semi-colon. The group of statements is called a block.
Compound statements are enclosed between the pair of braces ( { } ). The opening brace( { )
signifies the beginning and closing brace ( } ) signifies the end of the block.

3. What is the Null Statement?

Writing only a semicolon indicates a null statement. Thus ‘;’ is a null or empty statement.
This is quite useful when the syntax of the language needs to specify a statement but the logic of the program does not need any statement. This statement is generally used for and while looping statements.

4. What is Branching or Conditional Structure?

We can control the flow of a program using branching and looping statements. Branching statements give us code that is optionally executable, depending on the outcome of certain tests which that we can define.

5. What is the loop statement?

A loop statement allows us to execute a statement or group of statements multiple times and following is the general form of a loop statement in most of the programming languages − C++ programming language provides the following type of loops to handle looping requirements.

7. What is a sequential statement?

Sequential statements are. assignment statements that assign values to variables and signals. flow control statements that conditionally execute statements (if and case), repeat statements (for…loop), and skip statements (next and exit).

RELATED ARTICLES

How an Emergency Line of Credit Can Aid Your Financial Situation?

Whenever there is a financial emergency, people want quick funds to deal with the emergency. In order to cater to this, banks...

Things That Are Harming Your Credit Score

A credit score is a vital parameter to getting the best loan offers. A score of 630 or above is good enough...

The 10 Most Common Mistakes Made When Applying for a Business Loan

A business loan can be extremely useful for your business venture. Whether you are looking to obtain a working capital loan or...

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

How an Emergency Line of Credit Can Aid Your Financial Situation?

Whenever there is a financial emergency, people want quick funds to deal with the emergency. In order to cater to this, banks...

Things That Are Harming Your Credit Score

A credit score is a vital parameter to getting the best loan offers. A score of 630 or above is good enough...

The 10 Most Common Mistakes Made When Applying for a Business Loan

A business loan can be extremely useful for your business venture. Whether you are looking to obtain a working capital loan or...

Top Trading Techniques & Strategies Traders Should Know

There are a number of effective trading tactics you will come across when trading on the financial markets...

Recent Comments