Switch Case - C++ Tutorial For Beginners #10
Key Takeaways
The video tutorial covers the switch case structure in C++ for beginners, providing a foundational understanding of the programming concept.
Full Transcript
it's not a game it's [Music] a what is going on guys welcome back to the C++ tutorial Series in today's video we're going to talk about switch case statements so let us get right into it so the switch case statement is just an additional decision making structure in C++ so in the last video we talked about if lse and what happens when we do if else statements so first of all we have if then some condition in the parenthesis above and if that condition is true so it's a Boolean value in the end so whatever we write in there a comparison a function whatever it is if it returns a Boolean value it can be put into that par into those parentheses there if the return value of that is true we're going to execute this block of code here otherwise we're going to go into the else block and of course we can also have else if with other conditions and so on we already talked about that in the last video and now we're going to introduce switch case statements and for those of you who only code in python or or half only coded in Python you probably don't know about about switch case statements because they're not supported in Python and how they work is you have to pass either an integer or a character so a Char to that switch case structure and then you can Define cases for the value so let me show you what I mean by that let's say we have uh some integer I don't know X and it has a certain value or actually let's let's uh say it's a user input what we can then do is we can say switch X and then we can just specify certain cases for example one case could be X is 8 and if x is 8 we use a colon here and you we use indentation here um if the case 8 happens so if the case 8 is uh the case that we have right now so if the value of x is 8 then we can do something for example I don't know C out of the value was eight or something and then we can break so we always need to end a case with a break because otherwise if we don't uh end the case with a break it's just going to continue into the next case and we can specify a bunch of cases here like 10 for example and we can say something else here I don't know do the action for input 10 just have to Define whatever we want to do here and when we're done with a code we specify a break statement again and then we can also specify I so-called default statement which is essentially if the input was not8 and was not 10 and not one of the other case options that we can Define here we're going to go to the default value which is something like everything else essentially and we can define an action here stuff like uh invalid input for example and then we can break this as well so now when we run this you can see what this does and we can enter something like zero for example it says invalid input we can enter eight and we get the value is eight and we can also enter 10 do the action for input 10 now in this case this is not really useful so we're going to change this right now into a simple calculator we're going to build a simple calculator here we're going to say int n one and two for number one number two and then we are going to say character operator and now we're going to say see out enter the first number and this is it then we're going to say enter the second number and in between we're going to say choose an arithmetic operation and of course we're going to ask for the inputs respectively there you [Music] go and then we can go ahead and say switch and we can pass the operator so switch op case one the operator that was specified is the addition operator if that is the case we're going to return uh N1 + N2 break and we can now copy this here for all the individual operators so we can say for minus for subtraction for [Music] multiplication for division last but not least then also for the modulus operator so for the remainder and don't confuse this with a percentage operation by the way and otherwise if we get something else and those five operators we're going to say operator not support it there you go break all right so this is theault structure here and it should work already so let's try it enter the first number let's say I want to say 22 * 88 and this is the result then let's do something else let's say six modulus or modulo uh four we get two as a result then I can say 88 divided by five and of course maybe it makes a little bit more sense to make this a float so that we can do division properly let's recompile this uh what do we have here oh okay we cannot use the modulus operation here but we can typ cast this into an integer here if we're calling the modulus operation and then it should work so now I can go ahead and say 88 divided by 7 for example oh sorry this was not the right thing 88 divided by 7 and you can see that we got a floating Point result at the same time I can do the modulus operation so I can say um 17 modulus 3 and I get two and I can also go ahead and say 17.65 modulus 3.89 but what's actually happening since I'm typ casting is 17 modulus 3 so I'm getting the same result here as you can see this is how you can do a simple calculator uh and of course you can do this with if else statements as well so you can just say if operator equals equals plus then do this otherwise else if if it equals this but this is just more convenient uh for the switch variable you can only use characters and integers you cannot use anything else you cannot use floats doubles or more complex objects or structures um but as you can see this is a way more convenient way to to go through the different options so that's it for today's video hope you enjoyed it I hope you learned something if so let me know by hitting a like button and leaving a comment in the comment section down below and of course make sure you subscribe to this Channel and you hit the notification Bell to not miss a single future video for free other than that thank you very much for watching see you in the next video and bye [Music]
Original Description
Today we talk about the switch case structure in C++.
◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾◾
📚 Programming Books & Merch 📚
💻 The Algorithm Bible Book: https://www.neuralnine.com/books/
🐍 The Python Bible Book: https://www.neuralnine.com/books/
👕 Programming Merch: https://www.neuralnine.com/shop
🌐 Social Media & Contact 🌐
📱 Website: https://www.neuralnine.com/
📷 Instagram: https://www.instagram.com/neuralnine
🐦 Twitter: https://twitter.com/neuralnine
🤵 LinkedIn: https://www.linkedin.com/company/neuralnine/
📁 GitHub: https://github.com/NeuralNine
🎵 Outro Music From: https://www.bensound.com/
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from NeuralNine · NeuralNine · 0 of 60
← Previous
Next →
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
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
Python Beginner Tutorial #5 - Loops
NeuralNine
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
Python Beginner Tutorial #7 - Functions
NeuralNine
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
Python Beginner Tutorial #9 - File Operations
NeuralNine
Python Beginner Tutorial #10 - String Functions
NeuralNine
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
Python Intermediate Tutorial #6 - Queues
NeuralNine
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
Python Intermediate Tutorial #9 - Recursion
NeuralNine
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
Python Intermediate Tutorial #11 - Logging
NeuralNine
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
Python Machine Learning #4 - Support Vector Machines
NeuralNine
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
Making Text Images Readable Again with Python and OpenCV
NeuralNine
Neural Networks Simply Explained (Theory)
NeuralNine
Motion Filtering with OpenCV in Python
NeuralNine
Top 5 Programming Languages To Learn in 2020
NeuralNine
Simple TCP Chat Room in Python
NeuralNine
Image Classification with Neural Networks in Python
NeuralNine
Edge Detection with OpenCV in Python
NeuralNine
S&P 500 Web Scraping with Python
NeuralNine
Simple Sentiment Text Analysis in Python
NeuralNine
Introduction - Algorithms & Data Structures #1
NeuralNine
Related AI Lessons
⚡
⚡
⚡
⚡
How to prepare TIC teacher exams in Spain with AI (oposiciones 2026)
Dev.to AI
Why I built a simple AI provider wrapper (and you might too)
Dev.to · zhongqiyue
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · AI
This ChatGPT Prompt Replaced 3 Hours of PowerPoint Work
Medium · ChatGPT
🎓
Tutor Explanation
DeepCamp AI