Skip to main content

7. Switch Statement in c++

 ❖ C++ switch Statement :-

            Consider a situation in which, only one block of code needs to be executed among many blocks. This type of situation can be handled using nested if...else statement but, the better way of handling this type of problem is using switch...case statement. 


Syntax of switch:-

    

Switch(expression)

{

           Case value1 : statement1; 

                     Break; 

           Case value2 : statement2; 

                    Break; 

           Default : default statements; 


           The expression is either an integer or a character in above syntax. If the expression matches constant in case, the relevant codes are executed and control moves out of the switch statement. If the expression doesn't matches any of the constant in case, then the default statement is executed and control moves out of switch statement.


Flowchart of switch Statement:-




Example Proogram:-

#include <iostream.h>
#include <conio.h>
void main() 
         int a; 
         clrscr(); 
         cout<<"Enter the no"; 
         cin>>a; 
         switch(a) 
         { 
                  case 1: 
                           cout<<"one"; 
                                   break; 
                  case 2: 
                           cout<<"two"; 
                                   break; 
                  case 3: 
                           cout<<"three"; 
                                   break; 
                  default: 
                           cout<<"no"; 
                                   break; 
          } 
          getch(); 
}

Output:-

Enter the no

2  

two


Comments

Popular posts from this blog

Remove specific item from Recyclerview in android studio

For Full video click on below link :- https://youtu.be/sUz4fqeanjI?si=A8AqBWCgu-5NnBOJ Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file.  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursenam...

Fingerprint Authentication in Android Studio Project

Implement the implementation : - implementation 'androidx.biometric:biometric:1.0.1' Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical" tools:context=".MainActivity" android:background="@color/white"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Boxcode!!" android:textSize=...

Sqlite with CRUD operation in Android studio

For Full video click on link :- https://youtu.be/QTvy8sJSiyI?si=dM1f98Mr7aFpJpqG Working with the activity_main.xml file. Navigate to the   app > res > layout > activity_main.xml  and add the below code to that file. Below is the code for the   activity_main.xml   file.  <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical" android:background="@color/white"> <EditText android:layout_width="350dp" android:layout_height="50dp" android:inputType="text" android:id="@+id/coursename...