Switch Case - C++ Tutorial For Beginners #10

NeuralNine · Beginner ·🛠️ AI Tools & Apps ·5y ago

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 Visualizing Stock Data With Candlestick Charts in Python
Visualizing Stock Data With Candlestick Charts in Python
NeuralNine
2 Python Beginner Tutorial #1 - Installation and First Program
Python Beginner Tutorial #1 - Installation and First Program
NeuralNine
3 Python Beginner Tutorial #2 - Variables and Data Types
Python Beginner Tutorial #2 - Variables and Data Types
NeuralNine
4 Python Beginner Tutorial #3 - Operators and User Input
Python Beginner Tutorial #3 - Operators and User Input
NeuralNine
5 Python Beginner Tutorial #4 - If Statements and Conditions
Python Beginner Tutorial #4 - If Statements and Conditions
NeuralNine
6 Python Beginner Tutorial #5 - Loops
Python Beginner Tutorial #5 - Loops
NeuralNine
7 Python Beginner Tutorial #6 - Sequences and Collections
Python Beginner Tutorial #6 - Sequences and Collections
NeuralNine
8 Python Beginner Tutorial #7 - Functions
Python Beginner Tutorial #7 - Functions
NeuralNine
9 Python Beginner Tutorial #8 - Exception Handling
Python Beginner Tutorial #8 - Exception Handling
NeuralNine
10 Python Beginner Tutorial #9 - File Operations
Python Beginner Tutorial #9 - File Operations
NeuralNine
11 Python Beginner Tutorial #10 - String Functions
Python Beginner Tutorial #10 - String Functions
NeuralNine
12 Python Intermediate Tutorial #1 - Classes and Objects
Python Intermediate Tutorial #1 - Classes and Objects
NeuralNine
13 Python Intermediate Tutorial #2 - Inheritance
Python Intermediate Tutorial #2 - Inheritance
NeuralNine
14 Python Intermediate Tutorial #3 - Multithreading
Python Intermediate Tutorial #3 - Multithreading
NeuralNine
15 Python Intermediate Tutorial #4 - Synchronizing Threads
Python Intermediate Tutorial #4 - Synchronizing Threads
NeuralNine
16 Python Intermediate Tutorial #5 - Events and Daemon Threads
Python Intermediate Tutorial #5 - Events and Daemon Threads
NeuralNine
17 Python Intermediate Tutorial #6 - Queues
Python Intermediate Tutorial #6 - Queues
NeuralNine
18 Python Intermediate Tutorial #7 - Sockets and Network Programming
Python Intermediate Tutorial #7 - Sockets and Network Programming
NeuralNine
19 Python Intermediate Tutorial #8 - Database Programming
Python Intermediate Tutorial #8 - Database Programming
NeuralNine
20 Python Intermediate Tutorial #9 - Recursion
Python Intermediate Tutorial #9 - Recursion
NeuralNine
21 Python Intermediate Tutorial #10 - XML Processing
Python Intermediate Tutorial #10 - XML Processing
NeuralNine
22 Python Intermediate Tutorial #11 - Logging
Python Intermediate Tutorial #11 - Logging
NeuralNine
23 Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
Python Data Science Tutorial #1 - Anaconda and PyCharm Setup
NeuralNine
24 Python Data Science Tutorial #2 - NumPy Arrays
Python Data Science Tutorial #2 - NumPy Arrays
NeuralNine
25 Python Data Science Tutorial #3 - Numpy Functions
Python Data Science Tutorial #3 - Numpy Functions
NeuralNine
26 Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
Python Data Science Tutorial #4 - Plotting Functions With Matplotlib
NeuralNine
27 Python Data Science Tutorial #5 - Subplots and Multiple Windows
Python Data Science Tutorial #5 - Subplots and Multiple Windows
NeuralNine
28 Python Data Science Tutorial #6 - Matplotlib Styling
Python Data Science Tutorial #6 - Matplotlib Styling
NeuralNine
29 Python Data Science Tutorial #7 - Bar Charts with Matplotlib
Python Data Science Tutorial #7 - Bar Charts with Matplotlib
NeuralNine
30 Python Data Science Tutorial #8 - Pie Charts with Matplotlib
Python Data Science Tutorial #8 - Pie Charts with Matplotlib
NeuralNine
31 Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
Python Data Science Tutorial #9 - Plotting Histograms with Matplotlib
NeuralNine
32 Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
Python Data Science Tutorial #10 - Scatter Plots with Matplotlib
NeuralNine
33 Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
Python Data Science Tutorial #11 - 3D Plotting with Matplotlib
NeuralNine
34 Python Data Science Tutorial #12 - Pandas Series
Python Data Science Tutorial #12 - Pandas Series
NeuralNine
35 Python Data Science Tutorial #13 - Pandas Data Frames
Python Data Science Tutorial #13 - Pandas Data Frames
NeuralNine
36 Python Data Science Tutorial #14 - Pandas Statistics
Python Data Science Tutorial #14 - Pandas Statistics
NeuralNine
37 Python Data Science Tutorial #15 - Pandas Sorting and Functions
Python Data Science Tutorial #15 - Pandas Sorting and Functions
NeuralNine
38 Python Data Science Tutorial #16 - Pandas Merging Data Frames
Python Data Science Tutorial #16 - Pandas Merging Data Frames
NeuralNine
39 Python Data Science Tutorial #17 - Pandas Queries
Python Data Science Tutorial #17 - Pandas Queries
NeuralNine
40 Python Machine Learning Tutorial #1 - What is Machine Learning?
Python Machine Learning Tutorial #1 - What is Machine Learning?
NeuralNine
41 Python Machine Learning Tutorial #2 - Linear Regression
Python Machine Learning Tutorial #2 - Linear Regression
NeuralNine
42 Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
Python Machine Learning Tutorial #3 - K-Nearest Neighbors Classification
NeuralNine
43 Python Machine Learning #4 - Support Vector Machines
Python Machine Learning #4 - Support Vector Machines
NeuralNine
44 Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
Python Machine Learning Tutorial #5 - Decision Trees and Random Forest Classification
NeuralNine
45 Python Machine Learning Tutorial #6 - K-Means Clustering
Python Machine Learning Tutorial #6 - K-Means Clustering
NeuralNine
46 Python Machine Learning Tutorial #7 - Neural Networks
Python Machine Learning Tutorial #7 - Neural Networks
NeuralNine
47 Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
Python Machine Learning Tutorial #8 - Handwritten Digit Recognition with Tensorflow
NeuralNine
48 Generating Poetic Texts with Recurrent Neural Networks in Python
Generating Poetic Texts with Recurrent Neural Networks in Python
NeuralNine
49 Stock Portfolio Visualization with Matplotlib in Python
Stock Portfolio Visualization with Matplotlib in Python
NeuralNine
50 Analyzing Coronavirus with Python (COVID-19)
Analyzing Coronavirus with Python (COVID-19)
NeuralNine
51 Making Text Images Readable Again with Python and OpenCV
Making Text Images Readable Again with Python and OpenCV
NeuralNine
52 Neural Networks Simply Explained (Theory)
Neural Networks Simply Explained (Theory)
NeuralNine
53 Motion Filtering with OpenCV in Python
Motion Filtering with OpenCV in Python
NeuralNine
54 Top 5 Programming Languages To Learn in 2020
Top 5 Programming Languages To Learn in 2020
NeuralNine
55 Simple TCP Chat Room in Python
Simple TCP Chat Room in Python
NeuralNine
56 Image Classification with Neural Networks in Python
Image Classification with Neural Networks in Python
NeuralNine
57 Edge Detection with OpenCV in Python
Edge Detection with OpenCV in Python
NeuralNine
58 S&P 500 Web Scraping with Python
S&P 500 Web Scraping with Python
NeuralNine
59 Simple Sentiment Text Analysis in Python
Simple Sentiment Text Analysis in Python
NeuralNine
60 Introduction - Algorithms & Data Structures #1
Introduction - Algorithms & Data Structures #1
NeuralNine

This video tutorial teaches the switch case structure in C++ for beginners, covering its syntax, usage, and best practices. Viewers will learn how to apply the switch case structure in their own C++ programs. The tutorial is part of a series of C++ tutorials by NeuralNine.

Key Takeaways
  1. Declare a variable to store the value
  2. Use the switch keyword followed by the variable in parentheses
  3. Add cases for each possible value
  4. Specify code to execute for each case
  5. Use the break keyword to exit the switch block
  6. Optionally add a default case
💡 The switch case structure allows for more efficient and readable code when dealing with multiple possible values, making it a fundamental control structure in C++ programming.

Related AI Lessons

Up next
AI in Care - Katie Furey, Pairly.com
The Access Group
Watch →