4.1.4. The Switch Statement, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

4.1.4. The Switch Statement, Chapter - 4, Control Structures, HSSC-II (12th Class) Computer Science Key Point Notes

12th Class (HSSC-II) Computer SLO Based Key Point Notes

(National Book Foundation - As Federal Textbook Board, Islamabad 
Based on National Curriculum Pakistan 2023-2024 and Onward prescribed by Federal Board of Intermediate and Secondary Education, Islamabad, and All Pakistan Boards) 

{Contact WhatsApp 03339719149 for Class-XII Computer Science Complete SLO-Based Key Points notes in pdf format as well as in PowerPoint Presentation for preparing/delivering the Lectures}

************************************

👉👉👉 Computer Science 12th Class Notes (Main Page)

************************************

Unit 4: Control Structures


4.1.4. The Switch Statement:

  • The switch statement is another conditional structure that can be easily used when there are many choices available and only one should be executed.
  • Nested if becomes very difficult in such a situation.

General Syntax

switch(variable/expression)
{
        case val1:
                statements;
                break;
        case val2:
                statements;
                break;
                :
                :
        case valn:
                statements;
                break;
        default:
                statement;
}


Explanation

    • The switch statement starts with the keyword “switch”, followed by brackets containing an integer or character variable which is also known as a switch variable.
    • It can also be an expression giving an integer value.
    • Each “case” keyword is followed by a constant integer or character value which is terminated with a colon.
    • There can be one or more statements following each case keyword. 

    Working of Switch statement

    • The switch statement compares the value of the switch variable or the result of an expression with the constant values of each case.
    • If it matches with any case, the corresponding block of statements is executed.
    • The “default”, appears at the end of the switch statement, is executed only when the value of the switch variable or the result of an expression does not match with any case value. Its use is optional.
    • The break statement in each case is used to terminate the switch statement when the body of statements in the particular case has been executed.
    • If no break statement is in a case, then the control will fall through to all the following cases.

    Example

    Write a program that inputs two numbers and performs an arithmetic operation on those numbers by entering the choice. i.e.
    1. Addition
    2. Subtraction
    3. Multiplication
    4. Division

    #include <iostream.h>
    #include <conio.h>
    void main()
    {
            clrscr();
            int num1, num2;
            int option;
            cout<<“Enter 1st Number”;
            cin>>num1;
            cout<<“Enter 2nd Number”;
            cinn>>num2;
            cout<<“1. Addition \n 2. Subtraction \n 3. Multiplication \n 4. Division \n”;
            cout<<“Enter option 1-4: “;
            cin>>option;
            switch(option)
            {
                case 1:
                                cout<<“Sum of numbers is: “<<num1+num2;
                                break;
                case 2
                                cout<<“Difference of numbers is: “<<num1-num2;
                                break;
                case 3:
                                cout<<“Product of numbers is: “<<num1*num2;
                                break;
                case 4:
                                cout<<“Division of numbers is “<<(float) num1/num2;
                                break;
                default :
                                cout<<“INVALID OPTION \n“;
                }
                getch();
    }


    ************************************
    👉👉👉 Computer Science 12th Class Notes (Main Page)

    ************************************


    ************************************
    Shortcut Links For:

    1.  5th Class All Subjects Notes

    2.  8th Class All Subjects Notes

    3.  Easy English Grammar Notes



    1. Website for School and College Level Physics   
    2. Website for School and College Level Mathematics  
    3. Website for Single National Curriculum Pakistan - All Subjects Notes 

    © 2023 & onwards Academic Skills and Knowledge (ASK  

    Note:  Write me in the comments box below for any query and also Share this information with your class-fellows and friends.

    Post a Comment

    0 Comments

    cwebp -q 80 image.png -o image.webp