Switch Case statement in C++ - learnit

Home Top Ad

Post Top Ad

Monday, May 11, 2020

Switch Case statement in C++

Switch Case statement in C++

C++ switch statement

Learn C++ Programming

C++ is a powerful general-purpose programming language. It can be used to develop operating systems, browsers, games, and so on. C++ supports different ways of programming like procedural, object-oriented, functional, and so on. This makes C++ powerful as well as flexible.


Why Learn C++?

C++ is used to develop games, desktop apps, operating systems, browsers, and so on because of its performance.


After learning C++, it will be much easier to learn other programming languages like Java, Python, etc.


C++ helps you to understand the internal architecture of a computer, how computer stores and retrieves information.


switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.


Syntax

The syntax for a switch statement in C++ is as Follows

Switch(expression)
{
Case Constant –expression:
Statement(s);
Break; //Optional
Case Constant –Expression:
Statement(s);
Break;//Optional
Default: //Optional
Statement(s);
}


The following rules apply to a switch statement

-The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type

-You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon


-Theconstant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal


-When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.


-When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.


-Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.


-A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.


Examle

#include <stdio.h>
#include < conio.h>
Int main()
{
Int i,id,unit,price,total,discount;
Char item;
h:
Printf(“Please Choose Option is Below\n”);
Printf(“1) Customer is busy\n”);
Printf(“2) Display Result \n”);
Printf(“3) Exit()\n”);
Scanf(“%d”, &i);
Switch(i)
{
Case 1:
Printf(“Please input ID\n”);
Scanf(“%d”,&id);
Printf(“Please input item\n”);
Scanf(“%c”,&item);
Printf(“Please input Unit\n”);
Scanf(“%d”,&unit);
Printf(“Please input Price\n”);
Scanf(“%d”,&price);
Printf(“Please input Discount\n”);
Scanf(“%d”,&discount);
Total=(unit*price)-discount;
Break;
Case 2:
Printf(“ ID\t Name\t Unit\t Price\t Discount\t total”);
Printf(“……………………………………….”);
Printf(“%d \t %c \t %d \t %d \t \%d \t %d\n",id,item,unit,price,discount,total);
break;
case 3:
exit(10);
break;
default:
printf("Please Choose Option againt\n");
goto h;
break;
}
}

Please Watching My Video is Below

div class='clearfix'/>

No comments:

Post a Comment

Post Top Ad