prev
Or press ESC to close.
To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content. To continue enjoying our content, please consider disabling your ad blocker. Your support helps us continue providing quality content.

C++ Programming Part 7

Rajjit June 3rd, 2023 5 mins read
post-16

TOPIC: C++ Control Structures

Control Structures are used to simplify code structures as well as shorten the code lengths. They are used so that there is no complexity in the format of the code and make it easy to understand. They are also used in order to perform manipulative tasks, repetitive actions and make decisions. So the basic Control Structures are as follows:

1. SEQUENCE STRUCTURE

  • In sequence structure, a set of actions are performed one-by-one in a sequence after getting an entry from the user.
  • Then the program exits after performing all these actions.
  • This structure is not often used and other structures are more preferred.

2. SELECTION STRUCTURE

  • In selection structure, a condition is checked after an entry, whether the entry is true or false.
  • Then the program exits with the output.
  • This structure is also not often used.

3. LOOP STRUCTURE

  • This structure is most often used and more preferred than the previous structures.
  • In loop structure, a condition is checked using a continuous loop after an entry.
  • Then the program gets exited when the condition is false giving the required output.
  • Here, we will discuss about the types of loop structure and understand more about the uses in real-time program structures using examples.

Types of Loop Structure:

  • if statement: The if statement tests a condition and returns an output if the condition is true; if false ignored.
    Syntax:
    if(expression)
       statement;
    

    Let us look at the code about if statement

    // Program to illustrate if statement
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
              int a, b;
              cout << "Enter the value of a and b: " << endl;
              cin >> a >> b;
              if (a < b)
              {
                        cout << b << " is the biggest";
              }
              getch();
              return 0;
    }
    
    Output:
    Enter the value of a and b:
    3
    4
    4 is the biggest
    
  • if-else statement: The if-else statement tests a condition and returns an output if the condition is true and also returns a different output for the false condition. The condition is like either-or, providing an else condition.
    Syntax:
    if(expression)
       statement1;
    else
       statement2;
    

    Let us look at the code about if-else statement

    // Program to illustrate if-else statement
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
              int age;
              cout << "Enter your age: " << endl;
              cin >> age;
              if (age < 18)
              {
                        cout << "Your age is " << age << " and you are minor";
              }
              else
              {
                        cout << "Your age is " << age << " and you are not minor";
              }
              getch();
              return 0;
    }
    
    Output:
    Output 1:
    Enter your age:
    15
    Your age is 15 and you are minor
    
    Output 2:
    Enter your age:
    45
    Your age is 45 and you are not minor
    
  • ladder if-else: The ladder if-else acts like a ladder of conditions to be checked and then give the output. It is also called if-else-if staircase because of its appearance. There are many syntax or general format for ladder if-else.
    But there is one format (syntax) which is widely used:
    if(expression1)
       statement1;
    else if(expression2)
       statement2;
    else if(expression3)
       statement3;
       .
       .
       .
    else
       statement;
    

    Let us look at the code about ladder if-else statement

    // Program to illustrate ladder if-else statement
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
              int age;
              cout << "Tell me your age:" << endl;
              cin >> age;
              if ((age < 18) && (age > 0))
              {
                        cout << "You cannot come to vote" << endl;
              }
              else if (age == 18)
              {
                        cout << "You will get a pass to vote" << endl;
              }
              else if (age < 1)
              {
                        cout << "You are not yet born!!!" << endl;
              }
              else
              {
                        cout << "You can come to vote" << endl;
              }
              getch();
              return 0;
    }
    
    Output:
    Output 1: 
    Tell me your age:
    18
    You will get a pass to vote
    
    Output 2:
    Tell me your age:
    0
    You are not yet born!!!
    
    Output 3:
    Tell me your age:
    45
    You can come to vote
    
    Output 4:
    Tell me your age:
    12
    You cannot come to vote
    
  • switch statement: The switch statement provides a multiple-condition selection code. The conditions are successively tested and then the matched output is shown when the program is executed. The tests occurs for the values of an expression against a list of integer or character constants.
    Syntax:
    switch (expression)
    {   
              case n:
              {
                        action1;
                        break;
              }
              .
              .
              .
              default: 
                        action;
              break;
    }   
    

    Let us look at the code about switch statement

    // Program to illustrate switch statement
    #include<iostream>
    #include<conio.h>
    using namespace std;
    int main()
    {
              int age;
              cout << "Tell me your age:" << endl;
              cin >> age;
              switch (age)
              {
              case 18:
                        cout << "You are 18." << endl;
                        break;
              case 22:
                        cout << "You are 22." << endl;
                        break;
              case 2:
                        cout << "You are 2." << endl;
                        break;
              default:
                        cout << "No special cases." << endl;
                        break;
              }
              getch();
              return 0;
    }
    
    Output:
    Output 1: 
    Tell me your age:
    18
    You are 18.
    
    Output 2:
    Tell me your age:
    22
    You are 22.
    
    Output 3:
    Tell me your age:
    2
    You are 2.
    
    Output 4:
    Tell me your age:
    45
    No special cases.
    

TO BE NOTED:

  • ; // semi-colon - it is a null statement
  • { statement 1; statement 2; } // {} - it is called as compound statement

More blog posts coming soon!

Coding is the language that empowers us to speak to machines, creating a symphony of possibilities.
- Unknown