00:00:00
good morning everyone and welcome back
00:00:01
to the study shoe project my name is
00:00:03
mani and for those of you that don't
00:00:04
know me I am a fourth year studying
00:00:06
computer science and philosophy at the
00:00:08
University of Oxford and today I'm going
00:00:10
to bring you a video on the basics of
00:00:12
coding I know that a lot of you are
00:00:20
probably thinking that coding is this
00:00:22
like crazy evil genius thing that just
00:00:26
requires you to be the most intensely
00:00:29
smart and academic person out there and
00:00:32
requires so much understanding of hoodie
00:00:36
things but that's just not true let's
00:00:39
just take all our preconceived ideas
00:00:40
about coding and put them on the side
00:00:41
just pop them down and today I'm gonna
00:00:45
spend a couple of minutes just telling
00:00:46
you the basics of some of the key ideas
00:00:48
that are involved in coding and by the
00:00:50
end of this video hopefully together we
00:00:52
would have written our first program 3 2
00:00:55
1 let's get into this so I'm gonna be
00:00:58
doing this sort of split screen style
00:01:00
thing cuz I'm trying to be such as a
00:01:04
professional youtuber why would you want
00:01:07
to start why do you want to learn how to
00:01:08
code coding is an invaluable skill and
00:01:11
as we move into the future of the world
00:01:13
whatever the future holds
00:01:15
coding is going to be a net almost a
00:01:18
necessary skill and I remind computer
00:01:21
science teacher in school telling me be
00:01:23
programming or coding whatever language
00:01:25
you want to learn is the English of the
00:01:27
future and it's one of those kind of
00:01:29
skills that you can learn to do it at
00:01:30
home not have a degree in it and get a
00:01:31
job as a software engineer or a back-end
00:01:33
stack engineer or whatever and without
00:01:35
having the formal qualification because
00:01:37
it is just one of those skills that they
00:01:38
don't require you to have a formal
00:01:40
qualification if you're good if you can
00:01:42
show them that you are skillful within
00:01:44
this domain then you'll get a job and
00:01:46
some of these jobs are ridiculously
00:01:48
high-paying so if by the end this video
00:01:50
if you do a drawing doing I'm gonna put
00:01:52
some links in the description box below
00:01:53
so you can check out some more resources
00:01:55
yourself it's what we're gonna cover
00:01:56
today is some initial ideas of what some
00:01:59
statements are how statements come
00:02:01
together to put a program together and
00:02:02
then we're gonna build our first basic
00:02:04
program so first we gonna look at what a
00:02:06
statement is now a statement can be any
00:02:08
sort of single line of code doesn't
00:02:10
necessarily even have to be on a single
00:02:11
line
00:02:12
that's just one way to sort of explain
00:02:13
it to you now the first kind of
00:02:15
statement we're going to look at is a
00:02:16
variable assignment so what we're doing
00:02:18
is let's say I type x equals 1 now it's
00:02:22
important to understand that in
00:02:23
programming the e is not thought of the
00:02:26
same way as we look at equals in
00:02:27
mathematics so in maths equals can mean
00:02:30
a 1 equals 2 that doesn't make sense to
00:02:32
us if you write a equals 1 that means
00:02:35
they're both exactly the same thing
00:02:36
however in computer science we have to
00:02:38
remember the equals is assigning
00:02:40
something when we have a equals 1 a is
00:02:43
an empty box that we've just got above
00:02:45
throwing one inside of it we're
00:02:46
assigning something to it equally we
00:02:49
could say a x equals hello
00:02:52
that means we're putting the word hello
00:02:56
inside this box X now the cool thing is
00:02:58
that when we create a variable like X
00:03:00
this variable can have any type so this
00:03:03
could be an integer a string which means
00:03:05
a series of letters put together so word
00:03:08
sensors etc it could be just a single
00:03:10
character there are many different
00:03:12
things and these are known as tough as
00:03:14
data types so let's say I just want to
00:03:16
have I want to have variable X and we're
00:03:18
going to make sure that it's an integer
00:03:20
so here I've declared that it's int X
00:03:22
and I'm going to give it the value to
00:03:24
now put the semicolon show that this
00:03:27
line is over now this kind of syntax
00:03:29
varies across programming languages but
00:03:31
here I'm just assuming that when using a
00:03:33
sort of Java like language so we've got
00:03:35
int x equals 2 now hopefully we've all
00:03:38
understood that we've created a variable
00:03:39
X and the data type of that variable is
00:03:42
int so that means we've specified that
00:03:44
this variable is going to be assigned a
00:03:46
number at some point may not be but
00:03:49
that's the type of data that we're going
00:03:51
to put inside the box that we've labeled
00:03:52
X so that's one kind of statement now
00:03:55
what we can do is we can control the
00:03:56
flow of data using other kinds of
00:03:59
statements one of them is known as an
00:04:01
if-then statement so if I write if and
00:04:05
what this does is allows me to perform
00:04:07
an action based on a certain condition
00:04:09
so I write if x equals 2 then and then I
00:04:20
can put inside these brackets what I
00:04:21
want to happen print yes
00:04:26
so that means when I run this program
00:04:29
it's gonna check the value of X and
00:04:32
that's done with two equal sorry he's
00:04:35
gonna check the value of X and then if
00:04:38
this is true it's gonna perform what's
00:04:40
in the brackets and if it isn't it's
00:04:42
gonna do nothing if we want we can add
00:04:45
an else clause which means that if that
00:04:47
isn't if if whatever's in those brackets
00:04:49
doesn't hold you can perform a different
00:04:52
action we can say print no so an if-then
00:05:02
statement is just one kind of way that
00:05:04
we can kind of control the flow of what
00:05:06
is happening based on variables that
00:05:09
we've created or different things that
00:05:11
are happening in the program now you
00:05:12
might be thinking ghost is great but how
00:05:14
does this help me make an application or
00:05:16
do things like that and these kind of
00:05:18
small things can be built up to create
00:05:20
more complex programs later on we're
00:05:23
going to be looking at how we can create
00:05:24
a program that will check whether a
00:05:26
number is divisible by another number
00:05:28
and it's really important to have these
00:05:30
kind of if clauses to help us with them
00:05:32
another type of way to control the flow
00:05:34
of data is a while Clause now some of my
00:05:37
guests it a wild statement works by
00:05:39
rather than checking if what Evers in
00:05:41
the bracket works is going to keep
00:05:43
looping while whatever is in the bracket
00:05:45
is still true so if I say while X is
00:05:49
greater than what X is less than 0
00:05:54
[Music]
00:05:55
keep performing what's in here x equals
00:05:58
X minus 1 now the first thing you're all
00:06:02
probably thinking is that makes no sense
00:06:04
whatsoever X doesn't equal X minus 1 it
00:06:07
can never equal X minus 1 and again we
00:06:10
have to remember here that equals is not
00:06:12
the same kind of equals as it is in
00:06:14
maths equals here is a variable
00:06:16
assignment so here what we're doing is
00:06:19
we're we're saying that whatever X held
00:06:21
it's now going to hold 1 less than that
00:06:25
we're going to look inside a box see
00:06:26
what value X had take 1 away from it and
00:06:29
then assign that to X and we're gonna
00:06:31
keep doing that until X is less than 0
00:06:34
we've learned how to make statements
00:06:35
that have variables in them we've
00:06:37
learned how we can flow the control of
00:06:40
data using if statement and while loops
00:06:42
now we're going to see if we can put
00:06:44
those together to create the basic
00:06:45
program checking whether one number is
00:06:48
divisible by another what I'm going to
00:06:50
do is we're first going to do this
00:06:51
without using any sort of specific
00:06:53
programming language and then we're
00:06:55
going to put this in a compiler big word
00:06:57
scary I know but we're gonna get through
00:07:00
it and we're going to see whether and
00:07:03
we're gonna write it in Java and then
00:07:05
see whether our program works and if we
00:07:06
have any issues when we think about
00:07:09
creating a funner program to do
00:07:12
something in this case we're going to
00:07:13
give it two numbers we're gonna have to
00:07:15
have an input we're going to make use of
00:07:17
a function now functions just like in
00:07:20
math we're going to map some inputs to
00:07:21
some outputs now in this case the
00:07:23
problem that we're solving is we're
00:07:25
giving two numbers or checking if
00:07:26
they're divisible so we're going to take
00:07:27
two inputs so let's say we're going to
00:07:29
create a function and we're going to
00:07:31
call it divisible this is simple in X
00:07:39
and in Y so a specifying that we're
00:07:42
gonna give it two integers look at the
00:07:44
body of this method and see what exactly
00:07:46
are we putting inside this method what
00:07:49
we do want this function to do because
00:07:51
if we just said return
00:07:55
X now this is just a function that is
00:07:58
taking two numbers of returning the
00:08:00
first one if we said return why it's
00:08:02
taking two numbers and returning the
00:08:03
second one so we can see how we can
00:08:06
actually build up programs like this so
00:08:08
what we want to do is we want to check
00:08:10
whether the first number can be
00:08:11
divisible by the second so the way that
00:08:14
we're going to do this is we're going to
00:08:16
use a while loop and the logic behind it
00:08:22
is we're going to take the first number
00:08:24
and we're going to keep subtracting the
00:08:26
second number as many times as we can
00:08:28
until that number so if we have 25 and
00:08:31
we have five we're going to keep
00:08:33
subtracting five from 25 until that
00:08:36
number is less than the number that
00:08:39
we're dividing by if that makes sense if
00:08:42
you haven't understood that take a
00:08:43
second to think about it we're going to
00:08:45
take our big number we're going to take
00:08:46
the number that we want to check whether
00:08:48
it's divisible by this other number
00:08:50
we're going to keep taking it away on
00:08:51
until it is until it's smaller than this
00:08:55
number then what we can do is you can
00:08:58
check if that leftover number is zero
00:09:00
that means we've been able to divide it
00:09:02
perfectly each that we've been able to
00:09:04
subtract that number perfectly each time
00:09:06
but if it's in between zero and that
00:09:08
number that means it's clearly not a
00:09:09
divisor now the way that we can
00:09:11
implement that note that logic into this
00:09:13
code is by checking so while X is
00:09:18
greater and then within the body of this
00:09:20
code we can say x equals X minus y now
00:09:26
do we remember why that equals is
00:09:29
totally okay and doesn't feel well
00:09:30
because it's assigning a new value what
00:09:32
X is greater than equal to Y we're going
00:09:35
to say x equals X minus y and that means
00:09:38
when this loop terminates and that means
00:09:41
this is no longer satisfied and so that
00:09:44
means X is a less than Y so now how how
00:09:47
is it we can check whether this is
00:09:49
divisible once that loop is over we're
00:09:51
going to say use our if statements that
00:09:53
we learned about if x equals zero for
00:09:56
checking so he's a double equals
00:10:00
then print yes else no okay so if you
00:10:17
haven't understood that rewind and see
00:10:19
whether you understand the logic behind
00:10:20
that again don't focus on the syntax of
00:10:23
what we're writing just focus on whether
00:10:24
you understand the logic of this program
00:10:26
so we're inserting two numbers I wrote
00:10:29
one out but yes or no depending on
00:10:31
whether the first number is divisible by
00:10:32
the second number we're going to keep
00:10:34
subtracting the second number from the
00:10:35
first number until that number is no
00:10:37
longer bigger than the first one that's
00:10:39
the underlying assumption that we've
00:10:41
made and then once that number has
00:10:45
become so small that it we can no longer
00:10:47
subtract that without it becoming
00:10:48
negative we'll check if that number is
00:10:49
zero or not now we're gonna do is go
00:10:51
onto an online Java compiler put all of
00:10:53
this in Java and see whether it works so
00:10:56
now what I'm doing is converting this
00:10:57
sort of pseudocode that we've created
00:10:58
which means code and programs aren't in
00:11:01
a specific language but are basically
00:11:02
showing us the logic so we easily
00:11:04
created that using just the logic and
00:11:06
now I'm gonna convert that into Java so
00:11:08
each language has its own syntax so in
00:11:10
some languages you put semicolons after
00:11:12
every line and something you don't in
00:11:14
some you write if-then and some you just
00:11:15
write if in brackets so there's a lot of
00:11:17
confusion amongst different languages I
00:11:20
would recommend you just sort of kick
00:11:21
one Java Python there generally a
00:11:24
simpler ones to begin with and sort of
00:11:27
take a go so I'm what I'm going to do is
00:11:29
compile this in Java so it's just an
00:11:31
online Java compiler so when you run a
00:11:33
program your main classes what sort of
00:11:34
runs anything that happens at the main
00:11:36
class going to be called so what I'm
00:11:37
going to do is call a function in the
00:11:39
main class and then create that function
00:11:42
that we just made below so we can refer
00:11:44
to it so to think about it as this the
00:11:47
main class is a to-do list of what's
00:11:49
going to happen the moment you execute a
00:11:50
program if you create a function that
00:11:53
isn't in the main class sort of down
00:11:54
here
00:11:55
but you haven't referred to in the main
00:11:56
class it's not going to run because it
00:11:58
wasn't on today's to-do list so if I
00:12:01
create a function call it huh so just
00:12:03
ignore all of this stuff for now
00:12:08
so we're going to call the function
00:12:10
divisible and we're going to say that it
00:12:13
takes two arguments so two inputs
00:12:16
arguments as a formal word for that and
00:12:18
we're going to say we're going to start
00:12:21
now looking at what looking at our
00:12:23
program so while X is greater than equal
00:12:28
to Y x equals X minus y so this is going
00:12:33
to end when that the condition no longer
00:12:36
hold so now we're going to check if X is
00:12:39
equal to zero then system
00:12:45
[Music]
00:12:46
dot out dot print line just e again this
00:12:52
is just Java syntax on how you print
00:12:54
something print and then we can add an
00:12:57
else say it's cool select run so now
00:13:03
again we've created this function but we
00:13:05
haven't called in the main class notes
00:13:06
and not nothing is going to happen so if
00:13:08
I go here and I say divisible I'm
00:13:10
calling the function divisible let's say
00:13:13
25 and 5 and divisible to make sure it's
00:13:18
not just giving us correct for
00:13:20
everything select execute and see what
00:13:24
happened so now we could do a bunch of
00:13:25
errors so clearly I have screwed up
00:13:27
somewhere it's always easy to see which
00:13:29
where these errors come from because
00:13:31
it'll show you there's a little arrow
00:13:32
pointing here there's an error here so
00:13:33
if ok this is me screwing up because
00:13:36
Java doesn't use thenns so Java you just
00:13:40
say if and you check let's try that okay
00:13:47
so I'm forgetting some semicolon so
00:13:49
there should be a semicolon there and
00:13:50
I'm forgetting see where no one is
00:13:53
perfect at all should be a semicolon in
00:13:56
there let's try that
00:13:59
okay what's wrong a hair stop divisible
00:14:02
incorrectly
00:14:03
so bit of an idiot there see perfect so
00:14:08
yes it is divisible and no it is not
00:14:11
divisible and there we go we've just
00:14:15
written a program on checking well the
00:14:16
two numbers are divisible and you didn't
00:14:18
even know how to program beginning of
00:14:19
this and if you did sorry it's not been
00:14:20
quite easy for you so I'm really sorry
00:14:21
that we do this but yeah so that was
00:14:24
such a simple way of putting a program
00:14:26
together I know that they've got the
00:14:28
sort of the syntax of Java may not be
00:14:29
that straightforward but I will put some
00:14:32
online courses underneath to kind of
00:14:34
give you and to see if that can help
00:14:37
yeah I'm very proud of us together we
00:14:40
have learnt again recapping we've learnt
00:14:42
how to create variables how to use if
00:14:44
statements how to use loops put them
00:14:46
together to create a function test that
00:14:48
function now debug it with some errors
00:14:51
you are basically a full page computer
00:14:52
scientists if you enjoyed this there's
00:14:54
probably a good chance that you will
00:14:55
enjoy programming in general so do check
00:14:59
out it's a valuable skill to pick up and
00:15:01
if you have some time this summer try it
00:15:04
out you never know you might be the next
00:15:06
Mark Zuckerberg you might rogram us out
00:15:09
of this mess of coronavirus yeah either
00:15:12
way I hope you enjoyed this video I
00:15:13
really tried to break it down as much as
00:15:15
I could I'm hope I hope you guys are
00:15:17
having a great day wherever you are and
00:15:19
that you found this video somewhat
00:15:20
useful if you haven't checked out my own
00:15:22
personal channel it is this is Manny and
00:15:24
I've linked it down below and yeah
00:15:26
thanks for watching guys and I will see
00:15:29
you guys soon bye don't forget to
00:15:31
subscribe if you haven't