Java Tutorial for Beginners #7 - Nested Statements
Key Takeaways
This video tutorial covers the basics of Java programming, specifically focusing on nested if/else statements and how to use them to classify users based on their age. The tutorial provides a step-by-step guide on how to create a simple program that asks for user input and then uses nested statements to determine whether the user is an adult, teenager, or younger than a teenager.
Full Transcript
in this video we're going to be kind of taking a step back and just going through everything that we've already learned summarizing that putting that into a program to make sure that you guys really understand all of that and we're also going to be talking about nested statements so how you can kind of add things within other things like so if statements within other if statements variables and the way I'm going to do this is just by creating a simple program and what it's going to do is it's going to ask the user to input their age and then based on that age we're going to do certain things with that we're going to tell them like I don't a good example is like on a roller coaster like if you're 13 or older you can ride otherwise you can't so we're going to do something like that okay so I'm going to keep these scanners up here that I have because we want to get the user's age but I'm first before I set up the scanner is I'm just going to print a uh line to the screen here so I'm just going to say system.out.print instead of print Ln and in this case I will just say input uh your page simply okay so this way it should uh if I'm doing this correctly make it so that uh we just get the age right after this line okay you you'll see when we do that in a second anyways what I'm going to do now is I'm just goingon to kind of start typing and we'll go through how this works after so I'm going to say if uh and we're actually just going to convert this to an INT first I think string to an INT yeah so using that parse int that I was talking about so say int of age is equal to S do or integer I always forget how to do this integer. parse in and then in here we need to type S so we're going to convert whatever they type in to an integer in this case so we're going to assume that they type in an integer so we'll say if age is greater than or equal to 13 then we will s simply print uh system.out.println you can ride okay exclamation point now if they're not older than 13 that must mean that they are less than 13 or younger than 13 so in this case we will simply print that they cannot ride system.out.println and you cannot ride exclamation point okay pretty basic program uh we kind of gone through how this works so let's test it out input your age five you cannot ride okay let's try this now input your age do 43 you can ride awesome okay so everything's working fine for right now now what I'm going to do is I am going to add a if here and I'm going to change kind of the problem that we're looking at so I'm going to say now I want to classify the person based on their age as either an adult a teenager or just like younger than a teenager okay I don't know what do you call those whatever younger than teenager is okay so if we are greater than or equal to 18 I want to print you are an adult okay now otherwise so if they are not greater than 18 we want to check if they are less than 18 so or if they're actually we'll do it in a in a a cool way that's going to make sense here in a second so else if age is not greater than or equal to 18 we know it's less than 18 so all we actually have to check is if they are older than 13 right or greater or older than or equal to 13 because that's what a teenager would be okay and the reason we don't have to check if they are uh what do you call it younger is or if they are older is because uh we already know that they're going to be younger than 18 so no we're they're within that range so I already butchered that explanation but that's okay so system.out.println and in this case we'll just say you are a teenager okay like that and then in this last case well we know they're not older than or equal to 18 we know they're not older than or equal to 13 that meaning they must be less than which is you are not a teenager or an adult okay like that so let's test our program out make sure everything is working so if we input an age of 14 we get you are a teenager awesome okay let's try it again if we input an age of 19 get you an adult and if we input an age of zero we get you are not a teenager or an adult now I want to show you what happens if I input like a negative number like what if I do like 98 you are not a teenager or an adult because right we have this lse statement and that's not greater than or equal to 13 it's not greater than or equal to 18 so we get you're are not a teenager or an adult okay now that is pretty straightforward what I'm going to show you now is how we can ask like another question or how we can check something else so what I'm going to do here is I want to say if they're 18 and only if they're 18 I want to ask them a question now what should the question be I want to ask them what their favorite food is okay so I'm simply going to do now exactly what I've done up here except just throw it um inside of this if statement so if this happens we're going to do this and this is going to show you nesting okay so we going to say SC or we'll say string I guess we can just do int because we're going to assume they're or string I guess cuz food yeah we'll say string food is equal to sc. next uh line like this okay and what I'm going to do is I'm going to print up here again what we're looking for so I'll just copy this save me a second and in this case I'll say input your fave food okay now what I want to do is I want to check uh what their favorite food is so I want to say their favorite food is pizza I'll say mine too otherwise I'll say that's not my favorite food or something like that okay so we'll say if and we have our uh condition in here we'll say if food dot equals and in this case we'll simply type Pizza all right then we will system.out.println and we'll say mine wow inside of the quotes hopefully say mine to and then otherwise so the else what we'll do is we'll simply type system.out.println and in this case we'll say not mine okay so this is showing you now it looks a bit more complicated but that we can actually put if statements and lse statements and we can do things with inside of the if statement and I'll show you that this works so if I type uh I'm 19 it's going to say input your fave food okay my favorite food let's try Pizza it says mine too awesome that's working now if I type something else I type like four it just tells me I'm not a teenager an adult because we don't have anything else happening inside of this if statement or this else over here okay now that's really cool and that allows us to do some neat things and you can continually nest and this is called nesting putting a statement inside of another statement you can continually do that as much as you want uh so I just wanted to show that to you because a lot of people think that that's not possible they don't understand that you're able to do that because they haven't been shown that right so again if I want to ask another question I could do that inside of here and I could just keep going but anyways I think that that probably gives you guys a good um explanation of how that works and shows you that I don't need to go any further so with that being said I'm going to end the video here in the next video we're going to be getting into looping which is some more advanced stuff some cooler stuff and we're continually moving through and we're going to be getting more advaned now uh getting out to kind of the more basic stuff of java that being said if you guys enjoyed the video please make sure you leave a like like And subscribe and I'll see you in the next one [Music]
Original Description
In this java tutorial I go over nesting if/else and elif statements combine everything we've learned so far into a simple program.
Java beginners programming tutorial. A complete java tutorial meant for absolute beginners. Absolutely no programming experience required. If you are new to programming and want to get started you are in the right place!
**************************************************************
One-Time Donations: https://goo.gl/pbCE9J
Support the Channel: https://www.patreon.com/techwithtim
Join my discord server: https://discord.gg/pr2k55t
**************************************************************
Please leave a LIKE and SUBSCRIBE for more content!
Tags:
- Tech With Tim
- Tech with Tim Java
- Java for beginners
- Java tutorial for beginners
- Beginner java tutorial
- Nesting in java
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from Tech With Tim · Tech With Tim · 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
A* Path Finding Algorithm(Visualization)
Tech With Tim
Python Programming Tutorial #1 - Variables and Data Types
Tech With Tim
Python Programming Tutorial #2 - Basic Operators and Input
Tech With Tim
Python Programming Tutorial #3 - Conditions
Tech With Tim
Python Programming Tutorial #4 - IF/ELIF/ELSE
Tech With Tim
Python Programming Tutorial #5 - Chained Conditionals and Nested Statements
Tech With Tim
Python Programming Tutorial #6 - For Loops
Tech With Tim
Python Programming Tutorial #7 - While Loops
Tech With Tim
Python Programming Tutorial #8 - Lists and Tuples
Tech With Tim
Python Programming Tutorial #9 - Iteration by Item (For Loops Continued...)
Tech With Tim
Python Programming Tutorial #10 - String Methods
Tech With Tim
How to Overclock a NVIDIA GPU
Tech With Tim
Python Programming Tutorial #11 - Slice Operator
Tech With Tim
Python Programming Tutorial #12 - Functions
Tech With Tim
Python Programming Tutorial #13 - How to Read a Text File
Tech With Tim
Python Programming Tutorial #14 - Writing to a Text File
Tech With Tim
Python Programming Tutorial #15 - Using .count() and .find()
Tech With Tim
Python Programming Tutorial #16 - Introduction to Modular Programming
Tech With Tim
Python Programming Tutorial #17 - Optional Parameters
Tech With Tim
Python Programming Tutorial #18 - Try and Except (Python Error Handling)
Tech With Tim
Python Programming Tutorial #19 - Global vs Local Variables
Tech With Tim
Python Programming Tutorial #20 - Classes and Objects
Tech With Tim
Cool VBS Script to Prank Your Friends!
Tech With Tim
How to Overclock an AMD GPU
Tech With Tim
Best GPU'S For Mining Ethereum (2018)
Tech With Tim
Recursion and Memoization Tutorial Python
Tech With Tim
Ethereum Mining Rig - Hardware Guide
Tech With Tim
Pygame Tutorial #1 - Basic Movement and Key Presses
Tech With Tim
How to Install Pygame (Windows 8/10)
Tech With Tim
How to Trade Your Cryptocurrency (Bitcoin, Ethereum etc.) For Cash!
Tech With Tim
How to Mine Ethereum 2018 - WORKING (Super-Easy)
Tech With Tim
Microphone Comparison - $10 Mic vs $150 Mic (Blue Yeti USB)
Tech With Tim
Pygame Tutorial #2 - Jumping and Boundaries
Tech With Tim
Pygame Tutorial #3 - Character Animation & Sprites
Tech With Tim
Pygame Tutorial #4 - Optimization & OOP
Tech With Tim
OBS Studio Tutorial - Best OBS Settings
Tech With Tim
Linear Search Algorithm - Python Example and Code
Tech With Tim
Make Any Mic Sound AMAZING! (WITH OBS)
Tech With Tim
Binary Search Algorithm - Python Example & Code
Tech With Tim
Pygame Tutorial #5 - Projectiles
Tech With Tim
Pygame Game - Mini Golf
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 1)
Tech With Tim
Pygame Tutorial - Projectile Motion (Part 2)
Tech With Tim
Pygame Tutorial #6 - Enemies
Tech With Tim
Pygame Tutorial #7 - Collision and Hit Boxes
Tech With Tim
Pygame Tutorial #8 - Scoring and Health Bars
Tech With Tim
Cloud Mining vs. Hardware Mining - 2018
Tech With Tim
How to Install Pygame on Mac OSX (Fast-Simple)
Tech With Tim
Pygame Tutorial #9 - Sound Effects, Music & More Collision
Tech With Tim
Pygame Tutorial #10 - Finishing Touches & Next Steps
Tech With Tim
How to Fade Your Screen in Pygame [CODE IN DESCRIPTION]
Tech With Tim
How to Create a Button in Pygame [CODE IN DESCRIPTION]
Tech With Tim
Pygame Side-Scroller Tutorial #1 - Scrolling Background/Character Movement
Tech With Tim
Pygame Side-Scroller Tutorial #2 - Random Object Generation
Tech With Tim
Pygame Side-Scroller Tutorial #3 - Collision
Tech With Tim
Pygame Side-Scroller Tutorial #4 - Scoring and End Screen
Tech With Tim
How to Create A Message Box in Python - Tkinter
Tech With Tim
Is Ethereum Mining Still Profitable - Is It Worth It (April 2018)
Tech With Tim
How to Run MAC OSX on a WINDOWS PC (Clover Boot-loader)
Tech With Tim
Programming Problem #1 - Alphabet Soup (Beginner/Novice)
Tech With Tim
More on: Tool Use & Function Calling
View skill →
🎓
Tutor Explanation
DeepCamp AI