# Lecture 2: Write Your First Program in C++

https://www.youtube.com/watch?v=t6zLJOCVqD0

[00:02] Hello ji, how are you?
[00:04] This is Love Babbar.
[00:05] Welcome to the channel Codehelp.
[00:07] This is the 2nd lecture of our DSA series.
[00:09] Today we are going to create our first program.
[00:13] We will see how a program runs, what does each line mean.
[00:20] We are going to understand everything in this lecture.
[00:23] So let's get started.
[00:24] Getting started.
[00:30] We saw in the last lecture that we create our source code using a programming language.
[00:37] But we know that computer won't understand our code because it only knows binary (01) language.
[00:53] So need something which translate our source code into machine understandable language.
[01:01] Just like we use translator to convert Hindi to French, we have compiler which does the same thing here.
[01:13] It converts this source code into binary language.
[01:19] Now this executable file can be easily run by the computer to give an output.
[01:32] This is how the compilation process work.
[01:37] So it is compiler which makes it possible for us to run a source code.
[01:45] Let's show you an example.
[01:47] Suppose we have a=1, b=2, and do c= a+b.
[01:56] Computer will not be able to understand these lines of code.
[02:03] This source code will first be converted to an executable format by the compiler and then the computer will be able to run it.
[02:09] This is how a program works.
[02:16] So I hope you must have got a basic idea of what compiler does and how a program gets executed.
[02:27] Before moving ahead in the video, let's talk about the Coding Ninjas.
[02:30] Coding Ninjas has supported us in providing this amazing DSA course to you.
[02:37] Thanks to them.
[02:40] Coding Ninjas is one of the largest coding platforms in India.
[02:43] If you are interested in paid courses of development, machine learning, android dev, or core subjects like OS, DBMS,
[02:51] or DSA in Cpp, Java or Python,
[02:55] then you can get all the paid courses here.
[02:58] You also get 1 on 1 doubt support.
[03:03] Your doubt will be resolved in 2-3 hours.
[03:05] Courses are well structured.
[03:10] The teachers are also well educated and have cracked big tech companies.
[03:15] The courses are available in both Hindi and English.
[03:18] You will also get Hint videos for every question.
[03:20] If you are interested, then you can buy it through this link, you will get 20% discount.
[03:20] Link is there in description.
[03:29] Bhaiya, Compiler does only conversion?
[03:35] 1st - Compiler handles the translation part.
[03:45] 2nd - It will help you in finding the errors in your code.
[03:50] If you will have any compile time error or runtime error, you will come to know about it.
[03:54] When you will execute your code, you will come to know where your code is wrong and what is the error there.
[04:00] So we have talked about both uses of the compiler.
[04:05] So this was a quick overview of the compiler and compilation process.
[04:12] Now let's start with our first program.
[04:15] First you need an IDE to write, run and execute a code.
[04:15] IDE is integrated development environment.
[04:26] It is a place where you can write a code and then execute it.
[04:33] There are many IDEs out there - CodeBlocks, VsCode and many more.
[04:43] You can install any of them by watching a youtube video.
[04:49] There are many remote IDEs also, which you don't even need to install.
[04:53] Replit is one of them, you can directly create a C++ there, and run it.
[05:02] If you have watched recursion in 1 shot video, then you must be knowing about it.
[05:09] Let me show you once.
[05:14] Replit.
[05:17] Let's suppose I have to create a C++ file.
[05:23] I wrote, create a repl and selected C++, and then gave it a name and clicked on create.
[05:32] Here is your C++ file, you can also run it here only.
[05:43] I will be using VScode here.
[05:51] We are starting with our first program name NAMASTEY DUNIYA(Hello world).
[05:55] If you are facing any issue in installing IDE, you can use Replite in that case.
[06:11] So let's start with NAMASTEY DUNIYA.
[06:25] In flowchart, we saw that our program always starts with a START block.
[06:35] So compiler should also know from where the program is starting.
[06:40] So we always start our code from 'int main'.
[06:45] Let's simply run it.
[06:49] Done, it is executed without any error.
[06:53] Let's understand what we have done.
[06:55] We have written 'int main'.
[07:02] Just consider it as the START block of the flowchart.
[07:18] Compiler will also start execution from 'int main'.
[07:24] 'int main' shows the start of the program and these brackets is used to define the boundary for all the code lines which fall under this 'int main'.
[07:35] It is very simple to understand.
[07:52] Now let's compile at run it again.
[08:02] Done, it is executed.
[08:06] Our target is to print NAMASTEY DUNIYA.
[08:20] There is an inbuilt functionality in C++ which helps us in printing anything.
[08:31] We don't have to write any extra code.
[08:31] We just have to use that functionality and we will get our output.
[08:37] That functionality is 'cout'.
[08:41] We can print anything using 'cout'.
[08:46] How? Lets see.
[08:46] I wrote, cout << "NAMASTEY DUNIYA" <<endl;
[09:01] This should give us our desired output.
[09:01] Let' run it.
[09:08] You can see this error here.
[09:08] It is here because compiler don't know this cout.
[09:17] We need to add 2 more things to run this program, let's add them quickly.
[09:24] #include<iostream>
[09:35] So I have just added these 2 things.
[09:35] What are they?
[09:35] Will explain, but first run it.
[09:43] You can see the output NAMASTEY DUNIYA here.
[09:51] So we have executed our first program.
[09:57] Now let's understand how this program is working.
[09:58] First include<iostream>, it is file which have print functionality already added in it.
[10:13] So if we want to use cout, we have to add this file to our code.
[10:20] Now we can use 'cout' very easily after including this file.
[10:24] Now what does this 2nd line mean?
[10:28] We will not go into its depth right now, but I will give you a basic idea.
[10:39] In C++, many namespaces are there which have cout functions in them, so we have written that we wan't to use std namespace's cout.
[10:53] So here we included that file, and then told compiler that we want to use the cout from the standard namespace.
[10:58] And then I wrote that print NAMASTEY DUNIYA.
[11:02] Right?
[11:04] What are these operators Bhaiya?
[11:08] Now let's understand the meaning of these operators.
[11:18] We use these signs to output the result into standard namespace.
[11:35] If you will use by mistake, then it will give you compilation error.
[11:44] So we use this operator to display output.
[11:55] This is our text which we want to display.
[12:00] You can write anything here "I am Love Babbar"
[12:04] and you can see that we got the output here.
[12:09] Now what is this endl?
[12:21] It means new line, or ENTER.
[12:32] This endl will help you to move your cursor to the next line.
[12:40] Let me remove it, and run it.
[12:49] You can see there is no ENTER after Love Babbar, but if you will add endl and run it, you can see a new line here.
[13:08] So endl means you want to print a newline.
[13:10] Do we have any another way of doing so?
[13:12] YES.
[13:14] You can do it like this also.
[13:32] "\n" - This character is mapped with the newline.
[13:42] There is no difference between \n and endl.
[13:48] We have an another way of writing this.
[13:51] We can add it with our main statement too, it will work fine.
[14:03] You can see the output is same.
[14:06] So we can do it like this also.
[14:07] Will it work if I will write it something like this?
[14:20] Yes, it will work.
[14:22] You can see.
[14:25] So you got an idea that how to use this endl and \n.
[14:32] Majority people use endl, so you can also use the same.
[14:43] Bhiaya, why you have added these semicolons after endl?
[14:49] These semicolons are used to show that a line has ended here.
[14:59] It resembles with the full stop, which we use in English.
[15:03] It tells the compiler that a statement has ended here.
[15:16] So we have understood our first program.
[15:20] If you haven't understood anything completely, no need to worry.
[15:25] This is just 2nd lecture.
[15:25] It will be clear to you in the upcoming lectures.
[15:29] We will keep on understanding the concepts bit by bit.
[15:35] So here is our first program named NAMASTEY DUNIYA.
[15:42] This is the output.
[15:51] Include file -> using namespace std.
[15:55] int main is the START block of this program.
[16:05] Then we have written string inside cout.
[16:07] Now what is this string?
[16:07] We will see it soon.
[16:11] So then we got our desired output.
[16:14] We have written NAMASTEY DUNIYA inside double inverted commas, why not simple?
[16:14] Just wait, I will explain this too.
[16:18] endl means new line and ; means end of the statement.
[16:23] Now let's move ahead and try to understand things one by one.
[16:30] We are moving to some very interesting concepts now.
[16:35] We are going to talk about data types and variables.
[16:44] Whenever you have to story any information or data in the memory, you have to mention what type of data it is and how much memory it will take.
[16:53] For eg- int a = 5.
[17:01] This statement means, a is a variable in memory which is of int type and it has value 5 in it.
[17:17] Let's understand it once again.
[17:23] 'int a' tells compiler that there is a variable named 'a', and which is of 'int' (integer) type.
[17:44] So compiler will block a 4 byte memory for this variable, because integer takes 4 bytes in memory generally.
[17:59] And then compiler will story value 5 inside this assigned block of memory.
[18:05] So 'int' told compiler what type of data will be stored and how much memory it will take.
[18:22] Type of data + memory required.
[18:29] Bhaiya, do 'int' always takes 4 byte memory?
[18:32] No, it can take 2 bytes also.
[18:35] But that varies from compiler to compiler.
[18:38] Generally 4 bytes are allocated to integers.
[18:47] So this is how integers will be stored.
[18:51] Then we have 'char' which is used to store characters.
[18:59] char ch ='a'.
[19:01] We always show characters using single inverted commas.
[19:15] This statement means, declare a char variable named ch in memory and store 'a' in it.
[19:24] 'ch' is a variable name, char is a data type.
[19:28] Char takes 1 byte in memory.
[19:30] 1 Byte means 8 bits.
[19:49] We also have bool with us.
[19:55] Bool is used to store either true or false (1 and 0).
[20:06] Boolean is of 1 byte.
[20:16] If I will write bool b = 1, then 1 will be stored in memory.
[20:25] Then we have float f = 1.2.
[20:33] We use it when we have floating point numbers.
[20:38] It takes 4 bytes in memory.
[20:44] This statement means, store 1.2 inside a float variable named 'f'.
[20:47] Double d = 1.23.
[20:55] It takes 8 bytes.
[21:02] So we have these data types which helps us in storing data.
[21:11] We have certain rules which we have to follow when we are giving names to the variables.
[21:23] We can use uppercase, lowercase, numbers, underscore( _ ), in names.
[21:41] But we have to keep this thing in mind that we can't start the name of the variable with any number.
[21:44] We can write abc1 but can't write 1abc.
[21:47] _abc is also fine.
[21:53] So keep this simple rule in mind.
[22:00] Let's try it on code to understand properly
[22:10] int a =123, and then we are printing it
[22:26] So you can see the output as 123
[22:35] Now let's print a character by declaring it this way. But we got an error here. Now what is this error
[22:55] We have earlier declared 'a' as int and then we have declared it as char, so compiler got confused and threw an error
[23:14] So we have to change its name here. Lets give it name 'b'
[23:20] Now it worked fine
[23:24] There is a way to to convert this int 'a' into char 'a' but we will see that in upcoming lectures
[23:39] Now let's print a boolean value. We can write bl = true or bl =1, both are fine. You can see the output here
[24:03] Bool will print 1 if value is true and it will print 0 if value is false
[24:07] Here we printed a float value
[24:24] Now double
[24:38] Everything is working fine
[24:41] There is a function named sizeof, which will tell you about the space being used by any variable
[24:50] We know that int takes 4 bytes, let's check it
[24:57] We made a variable named size and stored sizeof(a) in that
[25:09] Let's print this value here
[25:20] We got size as 123, we made a mistake, let's find out where ?
[25:28] We have to print size, not a
[25:36] So now it should work fine, let's run it
[25:40] Size of a is 4, so it is working fine
[25:43] This way we can find the size of any variable, of any type
[25:56] So we have seen the data types
[26:00] Can we write char b = 'ab'?
[26:08] No. I can only store single character in char
[26:25] We have a way to story multiple characters, but we will see that later
[26:31] Now let's see how things are stored in memory
[26:40] How data is stored?
[26:51] int a = 8, I know that int takes 4 bytes
[27:02] So 4 bytes of memory will be allocated to this block
[27:08] First this 8 is converted in binary
[27:21] 8 in binary is 1000, but here we have 32 bits in total, so it will be stored something like this
[27:27] it will store 0 everywhere and at the end, it will store, 1000
[27:35] This is how integers are stored in memory
[27:42] Now let's take, int a = 5, in binary 5 is 101
[27:52] So in memory, first 29 bits will have 0, and last 3 bits will have 101
[28:06] Now you must be thinking, how to store negative numbers
[28:27] We will understand this properly, but before that let's do this one first
[28:35] char ch='a', bhiaya how you will change 'a' into binary
[28:46] For this we have to look for ASCII table
[28:55] In c++, all the characters are mapped to some ASCII value
[29:01] For eg - 'a' is mapped to 97 in memory
[29:12] So we can convert this 97 in memory and store it. Simple, right?
[29:20] Now char is of 1 byte, means 8 bits
[29:27] We will just store binary form of ASCII value of any character
[29:44] Now your homework is, you have to look at ASCII table once
[29:51] Now what if we have 4 different bytes with some binary value, how will compiler know that whether it is a single Int value of 4 separate char values?
[30:42] The answer is, from data types
[30:51] So compiler will come to know from the data types which we used when we declared any variable
[31:11] Can we convert any variable from one data type to another?
[31:18] This process of converting one data type into another is known as type casting
[31:29] What will this statement do, int a = 'a'
[31:51] Let's try it on compiler
[31:53] Let's comment the previous code. By commenting, we can convert any text into simple text which compiler will ignore
[32:08] Now let's print int a = 'a'
[32:19] You can see that, it gave use 97
[32:25] Here this 'a' has been type casted to an integer
[32:31] Can we do this the other way?
[32:37] Let's see
[32:40] char ch = 98
[32:49] Will it also get type casted from int to char? Lets see
[33:02] And we got our answer as b
[33:05] Here, when I wrote char ch = 98, then this integer is Type casted into this char
[33:14] Got it, right?
[33:19] let's see one more thing here
[33:25] We can store a maximum value of 2^32 -1, in this 4 byte(32 bit) memory block
[33:43] max val is 2^32 -1 and min value is 0
[33:51] But in case of char, max value will be 2^8 -1, because char only has 8 bits
[34:04] Now what will happen, if I will assign an integer value which is larger than this max value,
[34:22] Let's see what will happen
[34:26] char ch1 = 123456, cout << ch1;
[34:43] But it gave us a warning that our 123456 will be treated as 64, because that is the largest value which can be stored in char
[35:16] and this 64 is mapped to @, so we got @ as our output
[35:27] Got this, right?
[35:31] So we understood that if we will try to assign some big value of any char variable, only its last bit values will be assigned and then specific mapped character will be printed
[35:43] Now let's come to that question, How negative numbers a stored
[35:53] It is very simple. The very first bit will tell whether the number is positive or negative
[36:00] If it is a positive number, then the first bit will be 0, otherwise it will be 1 incase of negative numbers
[36:09] There are few steps which I have to follow while storing a negative number. Let's say the number is -5
[36:26] 1st - Ignore the negative sign
[36:33] 2nd - Convert into binary representation = 101
[36:47] 3rd - Take 2's complement and store it
[37:01] Now what is this 2's complement, let's understand
[37:07] For taking 2's complement, first I have to take 1's complement, that is very simple. Just change 1 to 0 and 0 to 1
[37:29] Now to take 2's complement, we just have to add 1 to 1s complement
[37:37] Now this first integer shows that this number is a negative number
[37:44] So we have store negative numbers here
[37:47] Now how to print -ve integers
[38:03] This is 2's complement of -5
[38:24] Now let's take 1's complement of it, so I will reverse every bit
[38:44] Now lets add 1 to it to find 2s complement, now this way we will get the same number again
[38:56] So at the end you will print -5, because it had 1 in the first bit of 2's complement and 101 in its binary representation
[39:05] I hope you must have understood
[39:15] Till now, you have understood the compilation process, how to print NAMASTE DUNIYA,
[39:32] data types, binary representation, variables, convention of naming variables, and how value of any variable is stored in memory
[39:51] Now we can store negative number simply too, by just using their first bit as their sign, but with this our maximum value will be limited to 2^31 -1 instead of 2^32 -1
[40:27] Since -0 is also 0, so we are using 2 representation to show a single number, can we save this 1 representation?
[40:57] YES we can do that by the above method of 2s complement
[41:05] Now we our new range is -(2^31) to (2^31 -1)
[41:23] So we have written this whole thing to save 1 representation
[41:30] Is it important? Not from interview point of view. But if you have understood, then its good
[41:38] So this is how you store negative integers
[41:42] Bye default, int is signed, you can store both negative+ positive integers
[41:59] But if you want to just store positive integers and increase you range, then you can write it something like this, unsigned int
[42:15] Let's try to code it
[42:21] unsigned int = 112, cout << a;
[42:37] It is printing 112, but what if I will store -112 in unsigned int, then I will get a very large number
[43:02] But why so, let's see
[43:06] Compiler will try to print its 2's complement but since the value is unsigned, then it will print it as a positive integer which will be very large in value
[44:12] So we have to keep this in mind that we don't have to assign negative value to unsigned variables
[44:23] By default, an integer is a signed integer
[44:28] Now we will talk about operators
[44:44] % - Modules operator
[44:53] Then we have Arithmetic operators (+, - ,*, / )
[45:03] Let's start with these operators
[45:11] Few things to keep in mind - 2/5 will give 0 and not 0.4, why?
[45:40] Let's see why this happens
[45:45] Let's see what output we get here. See we got 0
[46:04] Whenever you will divide int by int, you will get your answer in integer
[46:16] Can we prevent this ?
[46:25] But if I will do float/int , then I will get my answer as float
[46:33] Double/int will give answer in double
[46:39] Let's try it
[46:42] Still it is giving 0, why?
[46:50] because you are storing your answer in a int variable, to get answer in float, store it in a float variable. Let's do that
[47:24] See, now it is working fine when I put it inside float or double variable
[47:44] So you have to keep this thing in mind whenever you are using a divide operator
[47:56] Let's see some more operators
[48:05] We have relational operators
[48:16] =, >, < , !=
[48:27] If you want to check whether A is equal to B or not
[48:40] I can do that using this (a==b)
[48:45] I can make use of these operators to compare two variables
[48:58] Let's implement them
[49:00] Here we are checking there equality
[49:27] See we got 0 means false, means not equal
[50:01] Now we will check whether A is greater than B or not
[50:13] then A less than B
[50:16] Then less than equal to B
[50:25] A not equal to B
[50:37] Let run and print answer
[50:40] A is not equal to B, A is not greater than B, B is greater than A, A is not less than equal to B
[50:55] B is less than equal to A
[50:58] A and B are not equal to each other
[51:02] So this way, we have understood relation operators too
[51:07] We have some other operators too
[51:12] When we say a=3, here = is an assignment operator, because we are assigning 3 to a
[51:22] Then we have logical operators, which have &&, ||, !
[51:32] What does they do bhaiya?
[51:34] && is used to check multiple conditions at the same time.
[51:37] For eg, you are fit if age=23 && height>6 feet && weight is 80kg
[51:48] So here in &&(AND), all these conditions should be true if you want to be called as fit
[51:57] || is known as OR operator, which checks multiple conditions but it returns true if minimum 1 condition is true
[52:13] && = All conditions should be true || = Atleast 1 condition should be true
[52:21] ! will reverse the condition, it will change false to true, and true to false
[52:33] Let's run it once
[52:44] ! of 23 is 0
[52:53] ! of 0 is 1
[52:58] Now let's move ahead towards bitwise operators
[53:10] I will teach these when we will use them in upcoming sessions
[53:18] We have got the basic idea of operators, we know what things to keep in mind while dividing, how to store negative numbers
[53:32] && operators and ! operators
[53:34] So we learnt compilation process, then we made our first program, Then we understood the meaning of each line in code
[53:57] then we studied data types and variables, along with their naming conventions
[54:13] How +ve and -ve numbers are stored in memory.
[54:16] What are signed and unsigned data types
[54:23] Then we read about the operators
[54:27] We did Arithmetic , relational, logical operators. We will do Bitwise operators later
[54:35] So this is what we studied in today's session
[54:39] Now you have to tell in comments, how was the today's session?
[54:43] Did you get the concept or not?
[54:48] You feedback will provide motivation to me
[54:55] Now we will study about conditions, loops and patterns in the upcoming sessions
[55:02] Don't forget to provide your feedback in the comment section
[55:08] As a part of your homework- You have to practice all these things,
[55:22] You have to point one thing, which we missed in Typecasting. I will provide gift voucher to those who will point this out
[55:41] Also you have to revise everything
[55:52] So this was for the today's video. Will see you in the next one. Bye
