04. Control Structure in C++ | IndianTechnoEra - IndianTechnoEra
Latest update Android YouTube

04. Control Structure in C++ | IndianTechnoEra

What is control structure in c++?
Admin

    Control Structure

    • Conditional Statements
    • Alternative or Looping Statements

    Conditional Statements

    1 If statement

    It is used to execute an instruction or sequence / block of instruction only if condition is fulfilled. if statement, expression is evaluated first and then, depending on whether the value of the expression (relation or condition) is true or false, transfers the control to the particular statement or group of statements.


    Different forms of implementation of if-statement are :

    • Simple if statement
    • if-else statement
    • Nested if-else statement
    • Else if statement
    • Simple if statement

    This is used to executed the statements only if the condition is true.

    Syntax :

        if(condition)
        {
            block of statement;
        }


    Example :

    #include <iostream>
    using namespace std;
    int main()
    {
        int number = 0;
        cout<<"\nEnter an integer : ";
        cin>>number;
        if (number > 10)
        {
            cout<<"\nYou entered "<<number<<" which is greater than 10\n";
        }
        if (number < 10)
        {
            cout<<"\nYou entered "<<number<<" which is less than 10\n";
        }
        return 0;
    }


    Output :

     Enter an integer : 8

     You entered 8 which is less than 10

    2 If-else statement

    If..else statement is used when different block of statement is to be executed on condition true and false.

    Syntax :

        if(condition)
        {
            block 1 of statement;
        }
        else
        {
            block 2 of statement;
        }

    if condition is true then block 1 statement will be executed and if condition is false block 2 statement will execute.


    Example :

    #include <iostream>
    using namespace std;
    int main()
    {
        int number = 0;
        cout<<"\nEnter an integer : ";
        cin>>number;
        if (number > 10)
        {
            cout<<"\nYou entered "<<number<<" which is greater than 10\n";
        }
        else
        {
            cout<<"\nYou entered "<<number<<" which is less than 10\n";
        }
        return 0;
    }


    Output :

     Enter an integer : 8

     You entered 8 which is less than 10


    3 Else-if statement

    To show a multi-way decision based on several conditions, we use the else if statement.

    Syntax :

     if(condition_2)
     {
        block 1 statement;
     }
     else if (condition_2)
     {
        block 2 statement;
     }
     else if(condition_n)
     {
        block n statement;
     }
     else
     {
        block x statement;
     }


    here, the conditions are evaluated in order from top to bottom. As soon as any condition evaluates to true, then statement associated with the given condition is executed and if all condition are false, then control is transferred to statement_x skipping the rest of the condition.

    Example :

    #include <iostream>
    using namespace std;
    int main ()
    {
        int mark;
        cout<<"Enter total marks of student : ";
        cin>>mark;
        if(mark <= 50)
        {
            cout<<"\nGrade D";
        }
        else if (mark <= 60)
        {
            cout<<"\nGrade C";
        }
        else if (mark <= 70)
        {
            cout<<"\nGrade B";
        }
        else
        {
            cout<<"\nGrade A";
        }
        return 0;
    }

    Output :

     Enter total marks of student : 80

     Grade A


    4 Nested If-else statement

    You can combine multiple if / if-else / if-else-if ladders when a series of decisions are involved. So you can make sure that your program executes certain instructions when a series of conditions are met.

    Some use case :

    Use case: 1

        if(condition)
        {
            //black of statement

            if(condition)
            {
                //block of statement
            }
        }

    Use case: 2

        if(condition)
        {
            //black of statement
        }
        else
        {
            if(condition)
            {
                //block of statement
            }
            else
            {
                //block of statement
            }
        }


    Use case: 3

        if(condition)
        {
            //black of statement
        }
        else if(condition)
        {
            if(condition)
            {
                //block of statement
            }
            else
            {
                //block of statement
            }
        }

    Example :

    #include <iostream>
    using namespace std;
    int main()
    {
        int numb1, numb2;
        cout<<"Enter two integers : ";

        cin>>numb1>>numb2;
        //checking whether two integers are equal.
        if(numb1==numb2)
        {
            cout<<"\nResult: "<<numb1<<" = "<<numb2;
        }
        else
        {
            //checking whether numb1 is greater than numb2.
            if(numb1>numb2)
            {
                cout<<"\nResult: "<<numb1<<" > "<<numb2;
            }
            else
            {
                cout<<"\nResult: "<<numb2<<" > "<<numb1;
            }
        }
        return 0;
    }

    Output :

     Enter two integers : 7 9

     Result: 9 > 7

    5. Switch

    Switch statements are also used when we need our program to make a certain decision based on a condition and then execute accordingly.

    Syntax :

    switch (<variable>)
    {
        case a-constant-expression :
            //Code to execute if <variable> == a-constant-expression
            break;
        case b-constant-expression  :
            //Code to execute if <variable> == b-constant-expression
            break;
        .
        .
        .
        case n-constant-expression :
            //Code to execute if <variable> == n-constant-expression
            break;
        default:
            //Code to execute if <variable> does not equal the value following any of the cases
    }
    (We will learn about break keyword in looping statement section)

    Example :
    #include <iostream>
    using namespace std;
    int main()
    {
        int input;
        cout<<"1. Play game\n";
        cout<<"2. Load game\n";
        cout<<"3. Play multi-player\n";
        cout<<"4. Exit\n";
        cout<<"Selection: ";
        cin>>input;
        cout<<"\n";
        switch ( input )
        {
            case 1: //Note the colon, not a semicolon
                cout<<"Play game called";
                break;
            case 2:
                cout<<"Load game called";
                break;
            case 3:
                cout<<"Play Multi-player game called";
                break;
            case 4:
                cout<<"Thanks for playing!\n";
                break;
            default:
                cout<<"Bad input, quitting!\n";
        }
        return 0;
    }

    Output :
        1. Play game
        2. Load game
        3. Play multi-player
        4. Exit
        Selection: 1
        Play game called



    Important points

    • The default case is optional.
    • case constant-expression can be int or char (no other datatypes are allowed).
    • Alternative or Looping Statements
    • For loop
    • Loops are used to repeat a block of code.

    Syntax of for Loop :

     for (init; condition; increment)

     {

        // block of statement.

     }

    Example :

    #include <iostream>
    using namespace std;
    int main()
    {
        for(int i = 0; i < 10 ; i++)
        {
            cout<<i<<" ";
        }
        return 0;
    }


    Output :

     1 2 3 4 5 6 7 8 9 10


    Explanation :

    init - Initializes the variable at the beginning of the loop to some value. This value is the starting point of the loop.

    condition - Decides whether the loop will continue running or not. While this condition is true, the loop will continue running.

    increment - The part of the loop that changes the value of the variable created in the variable declaration part of the loop. The increment statement is the part of the loop which will eventually stop the loop from running.


    While loop

    while loop statement in C++ programming language repeatedly executes a target statement as long as a given condition is true.

    Syntax :

    while( condition )

    {

        statement(s);

    }


    Example :

    #include <iostream>
    using namespace std;
    int main ()
    {
        // local variable definition
        int a = 1;

        // while loop execution
        while( a < 5 )
        {
            //loops comes inside this body, until condition is true
            cout<<"Value of a: "<<a<<"\n";
            a++;
        }

        return 0;

    }


    Output :

     Value of a: 1

     Value of a: 2

     Value of a: 3

     Value of a: 4


    do-while loop

    A do-while loop is similar to a while loop, except that a do-while loop is guaranteed to execute at least one time. The conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

    Syntax :

    do

    {

        statement(s);

    } while( condition );


    Example :

    #include <iostream>

    using namespace std;

    int main ()

    {

        // declared local operand (variable)

        int a = 1;

        // do-while loop

        do

        {

            cout<<"value of a: "<<a<<"\n";

            a = a + 1;

        } while( a < 5 );

        return 0;

    }


    Output :

     value of a: 1

     value of a: 2

     value of a: 3

     value of a: 4

    One more Example where condition is false :

    #include <iostream>

    using namespace std;

    int main ()

    {

        // declared local operand (variable)

        int a = 1;

        //here, Condition is false. a is not equals to zero

        do

        {

            cout<<"value of a: "<<a<<"\n";

            a = a + 1;

        } while( a == 0 );

        return 0;

    }


    Output :

     value of a: 1


    break, continue, goto keyword

    break keyword

    • break statement neglect the statement after it and exit compound statement. in the loop and transfer the control outside the loop
    • Break it's sole purpose to passes control out of the compound statement i.e. Loop, Condition, Method or Procedures.

    Example :

    while(a)
    {
        while(b)
        {
            if(b == 10)
            {
                break;
            }
        }
        // break will bring us here.
    }


    continue keyword

    Similar,To break statement continue statement also neglect the statement after it in the loop and send control back to starting point of loop for next iteration instead of outside the loop.


    Example :

    #include <iostream>
    using namespace std;
    int main ()
    {
        int a = 10;
        while(a < 20)
        {
            if( a == 15)
            {
                // skip the iteration
                a = a + 1;
                continue;
            }
            cout<<"value of a: "<<a<<endl;
            a++;
        }
    return 0;
    }

    Output :
     value of a: 10
     value of a: 11
     value of a: 12
     value of a: 13
     value of a: 14
     value of a: 16
     value of a: 17
     value of a: 18
     value of a: 19


    goto

    • goto statement transfer the control to the label specified with the goto statement
    • label is any name give to particular part in program
    • label is followed with a colon (:)

    Syntax :

    label1:

    -

    -

    goto label1;


    Example :

    #include <iostream>
    using namespace std;
    int main()
    {
        int i, j;
        for ( i = 0; i < 10; i++ )
        {
            cout<<"Outer loop executing. i = "<<i<<"\n";
            for ( j = 0; j < 3; j++ )
            {
                cout<<" Inner loop executing. j = "<<j<<"\n";
                if ( i == 5 )
                {
                    goto stop;
                }
            }
        }
        // This message does not print.
        cout<<"Loop exited. i = "<<i<<endl;
        stop:
            cout<<"Jumped to stop. i = "<<i<<"\n";
    }


    Output :
    Outer loop executing. i = 0
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 1
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 2
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 3
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 4
     Inner loop executing. j = 0
     Inner loop executing. j = 1
     Inner loop executing. j = 2
    Outer loop executing. i = 5
     Inner loop executing. j = 0
    Jumped to stop. i = 5

    إرسال تعليق

    Feel free to ask your query...
    Cookie Consent
    We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
    Oops!
    It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
    AdBlock Detected!
    We have detected that you are using adblocking plugin in your browser.
    The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
    Site is Blocked
    Sorry! This site is not available in your country.