-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathswitch.cpp
80 lines (64 loc) · 1.89 KB
/
switch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/*Lecture 3.2: Switch Case Statement */
/* Program to implement multiple fruit item labeled,
when user input number upon his/her choice */
#include <iostream>
using namespace std;
int main()
{
char fruit;
cout << "Input A Character: ";
cin >> fruit;
/*1. First way to do it using ifelseif condition */
// if(fruit == 'a') {
// cout<<"Mango"<<endl;
// } else if (fruit == 'b'){
// cout<<"Banana"<<endl;
// } else if (fruit == 'c'){
// cout<<"Grapes" <<endl;
// } else if (fruit == 'd') {
// cout<<"Orange" <<endl;
// } else if (fruit == 'e') {
// cout<<"Gavava" <<endl;
// } else {
// cout<<"I am still searching!!!" <<endl;
// }
/*2. Second way to do it using switch statement, isme hoga ki humko baar baar apne fruit ko different values
se compare nhi karna hoga, switch automatically fruit ko different values se compare kar dega and uske corressponding
jo bhi code hoga usko execute kar dega
*/
switch (fruit) //yaha pe mention karna hoga kis variable ko compare karna hai
{
case 'a':
cout << "Mango" << endl;
/* hum break isliye use karte hai, ki aik case value ki
statement execute hone ke baad hum switch se bahar aajaye.*/
break;
case 'b':
cout << "Banana" << endl;
break;
case 'c':
cout << "Grapes" << endl;
break;
case 'd':
cout << "Orange" << endl;
break;
case 'e':
cout << "Gavava" << endl;
break;
default:
/* agar variable ki value kisi bhi cases se match nhi hota ,
too default ke corrosponding ke jo bhi statement hai usko execute kar dega.
statement */
cout << "I am still searching!!" << endl;
break;
}
return 0;
}
/* Output:
Input A Character: b
Banana
Input A Character: d
Orange
Input A Character: f
I am still searching!!!
*/