# Lecture 3: If-Else, While loop & Lots of Patterns (Part-1)

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

[00:03] Hello ji, how are you all?
[00:03] This is Love Babbar.
[00:05] And welcome to the channel CodeHelp.
[00:07] Till now we have covered many things.
[00:09] We have made our first program, we know what are operators, pseudocode, flowchart, data types, compilation process.
[00:17] We have covered all these concepts but we haven't done the coding.
[00:21] So in this 3rd lecture, we are going to code lots of solutions.
[00:29] We are going to code too many patterns.
[00:29] I will also give many questions as a homework to you.
[00:37] So let's start with conditionals.
[00:45] What are conditions?
[00:45] If you will give me 5 rupees, I will give you a pen, this is a condition.
[00:53] If you will give me 10k rupees, then only I will teach you (Joking😜).
[01:04] So now let's see how to ask computer for the condition.
[01:11] Now we will learn the syntax of conditional statements in CPP.
[01:21] We will make a simple program, where we will ask computer to print A if A is bigger than B, else print B.
[01:39] In the 1st lecture, we saw this decision making box for conditions.
[01:53] Is a>b, we get 2 outputs, YES or NO.
[02:01] So now how we will convert this conditions into code?
[02:11] We will make a simple program, where we will ask computer to print "Answer is A" if A is bigger than B, else print "Answer is B".
[02:42] Now we are going to talk about 'if' statements.
[02:53] If(condition) { output }.
[03:03] Whenever this condition will be true, this particular output will be printed.
[03:15] Before moving further in the video, let's talk about the Coding Ninjas.
[03:17] Coding Ninjas has supported us in providing this free DSA course, thanks to them.
[03:27] Coding Ninjas is one of the largest coding platforms in India.
[03:30] If you are interested in paid courses of development, ML, Android dev, Web Dev, System design, core subjects like OS, DBMS, DSA in CPP, Java, Python.
[03:45] You also get one on one doubt support.
[03:50] All your doubts will be resolved within 1-2 hours.
[03:53] The courses are really well structured.
[03:57] The faculties have cracked big tech companies.
[04:02] The courses are available in both English and Hindi.
[04:05] So if you are interested in buying their courses, then you can use the link given in the description to avail the 20% discount.
[04:17] if(a>b){ cout <<"Answer is a"; }
[04:38] if(b>a){ cout <<"Answer is b"; }
[05:00] So now we have understood the 'if' block and how to use it.
[05:14] If condition is true, then only if block will be executed.
[05:18] Let's say, a=5, b=14, then this if block condition is false, so all the statements inside this block will not be executed.
[05:43] Let's say you are given a number A, and you are told that print +ve if A is positive, otherwise, print not +ve.
[06:03] First we know what we have to do, if(a>0){ cout <<"+ve"}.
[06:17] But what we will write to print "not +ve"?
[06:20] For that case, we have else block.
[06:31] Else means (otherwise), if 'if' block is not executed, then 'else' block will be executed.
[06:40] If A is 5, then 'if' block will be executed, if A is -2, then 'else' block will be executed.
[06:55] So we have understood the 'if' and 'if else'.
[07:12] Let's code it once.
[07:14] Those we don't know what is this, you can watch our previous lectures, we have explained this there.
[07:30] And those we know this, write in comments why we use this.
[07:39] First we created a variable 'a' by writing 'int n', then cin n.
[07:51] What is this bhaiya?
[07:54] You know that cout << n; is used to print the value of n.
[08:15] Now cinn, helps in taking the input of n.
[08:19] 'int a' makes a memory block which is named 'n' which contains garbage value in the beginning.
[08:28] Then you have written, cin n, so now program is waiting for you to give an input.
[08:37] Program will not move to the next statement, until you give an input.
[08:45] What ever statements will be written after cin n, will not be executed, until we don't give any input.
[08:55] You can see the live example of this here.
[08:58] It is not printing anything if I am not giving any input.
[09:04] But if I will give some input, let's say 2, then it will start executing the next statements.
[09:12] So cin var_name; is used to take input of any variable from the user.
[09:24] After giving this value, we can do anything with it.
[09:28] Let's try to print the value of 'n' after taking it's input from the user.
[09:43] You can see that value of n is printed after taking input from the user.
[09:55] Now let's move further.
[09:55] So we were discussing this example, print "+ve", if a is postive, else print "not +ve".
[10:07] Let's see this in code.
[10:12] if(a>0){ cout <<"A is Positive"}.
[10:39] let's run it.
[10:40] I entered 5,it print A is positive.
[10:40] Now what if I enter -5, then it didn't print anything.
[10:56] Because if block is not executed this time.
[11:06] But if I add a 'else' block here, then this block will be executed for negative numbers.
[11:31] Incase of 0, it will print A is negative because we have written ' if A is greater than 0'.
[11:37] In the 1st question we wanted to print A is A is greater than B, otherwise print B.
[11:59] Now let's code it.
[12:06] Now first I will take A, B as inputs.
[12:16] When I write, cin a b, it means first we have to enter a, then b.
[12:30] Let's print a and b both.
[12:54] First I entered 'a', then pressed ENTER, and entered value of 'b'.
[13:02] But what if I add a SPACE instead of pressing ENTER, let's try.
[13:07] Still it is working fine.
[13:07] So we can do anything, either press ENTER or add a SPACE between the variable values.
[13:15] Now let's try it with TAB.
[13:23] To conclude, cin don't read SPACE, TAB, ENTER.
[13:39] These are some special characters for TAB, SPACE and ENTER.
[13:45] But we have an another method to take input, cin.get().
[13:45] Let's try out that too.
[14:19] It is printing 49, if I am entering '1'.
[14:25] Why so?
[14:31] cin.get() takes input in the form of characters.
[14:35] It took '1' as a character, but we are assigning this value to an integer, so that's why it printed ASCII value of that character.
[14:54] I asked you to look at ASCII table once, in the last video.
[15:00] But let's try to input SPACE, using this method.
[15:06] This time I just entered a SPACE, and pressed ENTER.
[15:12] It gave 32 as output.
[15:15] What if I press ENTER, I am getting 10.
[15:20] It is giving value 9 if I am giving TAB as input.
[15:34] So we can use cin.get(), to take the input of SPACE, ENTER and TAB.
[15:41] Now let's code that "if else" question.
[16:03] Okay so first let's take the input of a and b.
[16:15] Taking inputs here.
[16:33] Now if(a>b){ cout << " A is greater";}
[16:43] For the 2nd case, if(b>a){ cout << " B is greater";}
[17:08] Let's run it.
[17:10] I entered A.
[17:17] I entered B.
[17:19] So we have made a program which can tell us which number is greater.
[17:24] Let's make an another program which will tell us whether the number is positive, negative or 0.
[17:37] If(a>0){ print +ve}.
[17:54] Otherwise, we will come inside this 'else' block.
[17:58] We will write, if(a<0){ print negative}, otherwise print 0.
[18:15] So this is how we made a program.
[18:17] Let's code it.
[18:21] Took A as input.
[18:36] Asking the user to enter value of A.
[18:43] Now writing our main code.
[18:57] if(a>0){ print +ve} else {if(a<0){ print negative}, otherwise print 0}}.
[19:42] Let's run it.
[19:45] Oh sorry, we forgot a semicolon.
[19:45] Let's run it again.
[19:51] Entered A as 15.
[19:54] It printed A is positive.
[19:57] This time I entered -1.
[19:57] A is negative.
[19:57] Correct output.
[20:05] Now this time I entered 0, and it printed that A is 0.
[20:08] So we have understood this thing.
[20:13] But can we write it in some other way?
[20:17] We will try to omit this another if block inside the else.
[20:26] Because we can't keep on adding 'if' inside else block for some more conditions.
[20:34] Let's try to do it in a good way.
[20:38] We also have 'if else if'.
[20:41] We can write something like this.
[21:02] Last else condition is a choice, we can omit it too.
[21:22] So now we can change our original code with 'if else if'.
[21:47] Let's run till this much for now.
[21:50] Gave correct output for 9 and -8.
[22:01] But it printed nothin incase of 0, because we haven't mentioned any condition for 0.
[22:08] so for that, we can write a else condition here
[22:32] So you can see, that this code looks so clean as compared to the previous one
[22:45] Till now we have seen 'if' conditions, 'if else' conditions and 'if else if' conditions
[23:34] For eg - if you are age is above 50, you are old, if you age is below 20, you are a child, else you are young, So this last 'else' case is a default case
[23:44] It's code will go something like this
[23:58] so we have seen 'if' conditions, 'if else' conditions and 'if else if' conditions
[24:15] Try to guess the output of these questions
[24:34] You have to write the answer of these questions in comments. Top 3 students will get gift on their gmail id. Don't forget to add you Gmail id
[24:44] I am giving you 1 question to code
[24:49] I will give you a character, you have to tell me whether it is a lowercase character, or uppercase character or it is numeric
[25:28] If you have understood everything till now, you can easily solve this question. This is your homework
[25:40] No course can gurantee your success, if you yourself will not put any effort. So code this question
[26:01] So we also studied about looping when we discussed flowcharts
[26:43] Now how to write loops in code
[26:46] Problem - Print numbers from 1 to n. How will we write a code for this?
[26:58] Now we are going to study 'while' loops
[27:10] The code inside 'while' block will keep on executing, as long as this condition is true
[27:37] For eg - I can keep on buying bananas, as long as I have money in my pocket
[27:59] So these statements will keep on executing, as long as the condition is true
[28:09] So now let's see, how we will use this in our question
[28:26] This is the flowchart which we made earlier
[28:43] So took input, initialized i with 1, then checked a condition 'i <n', because we don't want to print numbers larger than 'n'
[29:14] But if condition is still valid, then we incremented 'i' and kept on executing these statements again and again
[29:42] This is how we printed numbers from 1 to 5
[30:03] This loop broke when i became 6 and our program ended here
[30:12] Now we will change it to code
[30:15] First we took the input of 'n'
[30:20] Then we made a variable 'int i=1'
[30:29] Then we will write 'while(i<=n){ cout << i ; i= i+1;}
[31:08] Now our code is ready
[31:15] Let's write it on our IDE
[31:37] First we took input, then initialized i with 1, because we have to print numbers from 1 to n
[31:53] Then we will write 'while(i<=n){ cout << i ; i= i+1;}
[32:11] Let's run it
[32:15] Gave input as 5
[32:25] So it printed 1 2 3 4 5
[32:29] So it has printed values from 1 to 5
[32:34] If I enter 10, it will print numbers from 1 to 10
[32:47] See, how easy it is. You just have to enter a condition and the statements which you want to execute
[33:04] Let's try another question
[33:11] Calculate the sum of all numbers from 1 to n
[33:19] It is very similar to previous question
[33:28] In the beginning, sum =0, i=1
[33:36] Then we added i to sum and incremented it. We repeated these steps until i becomes greater than N
[34:04] So got our answer as 15. Very simple, right?
[34:12] Let's code
[34:15] sum =0, i=1
[34:19] we know, in the while condition we have to write (i<=n)
[34:31] Inside this while loop, we added i to sum and then incremented i by 1
[34:40] So this is our code. Simple
[34:45] Took n in input
[35:06] int i=1, int sum =0
[35:23] while(i<=n){ sum = sum+i; i= i+1}
[35:45] Done. At last we just have to print this sum
[35:57] Let's run it
[36:01] Gave 5 in input
[36:04] You can see, it is giving correct output
[36:10] You can check your answer by applying this formula
[36:36] Bhaiya, can we use this formula to find the answer instead of while loop? YES, why not
[36:43] We solved it using loop, to understand loops clearly
[36:51] Here is your homework question.
[36:54] Q1- Find sum of all even numbers from 1 to N. We already discussed it's flowchart. It is very easy
[37:23] Q2 - Farhenheit to Celcius table
[37:44] Let's try to do that prime number question(discussed in lecture 1) using this while loop
[37:57] We have to tell whether n is prime or not
[38:04] prime number is a number which has only 1 and N as it's factor
[38:23] We will use this % operator for this question. % tells us remainder when we divide 2 numbers
[38:52] Let's start
[38:59] First we took the input of N
[39:10] So I will simply divide N by all numbers between 1 and 9, to check whether it is prime or not
[39:29] If any of these numbers gave modules as 0, then this N is not prime, otherwise it is prime
[39:56] So we have to divide N by all numbers between 1 and N, and check their modulus, if any number gives modulus 0 with N, it is not prime
[40:03] But if no such number exist, then it is a prime number
[40:08] Let's code
[40:09] int i=1, is it correct?
[40:12] No, we have to start with 2 this time, because every number gets divided by 1
[40:16] We already saw that we have to divide N with all numbers that comes BETWEEN 1 and N
[40:34] So inside condition w will print i<n, because we don't have to include N
[41:05] 7 can be called as prime if it does not give remainder 0 when divided by any number BETWEEN 1 and N
[41:35] so inside condition w will print i<n, because we don't have to include
[41:58] if(n%i==0) is true, it means, i divided n and gave remainder as 0, so N is not prime
[42:23] Let's run the code till here. I entered 14, and it gave me so many NOT PRIME in output
[42:28] We got so many NOT PRIME, because we forgot to increment 'i'
[42:42] So don't forgot to add this increment condition, otherwise our code will go in infinite state
[43:02] You can see, still it is printing twice
[43:14] Because there must be 2 numbers(2, 7) which divided N and gave remainder 0
[43:27] Bhaiya, what if I want to print only once
[43:33] We are going to learn one new concept in next lecture, that time we will see this code again
[43:44] I can also print something like this. You can see the output here
[44:24] For 11, you can see we got prime for all
[44:34] So in while statements, you have to keep conditions in your mind
[44:45] Now let's move further and do some questions on patterns
[44:57] There are very rare chance that someone will ask you a pattern question but it will increase your understanding a lot
[45:13] This is an example of pattern. Now how we will print this in code
[45:27] Bhaiya what will happen by printing these star patterns
[45:32] This will help you in your logic building part + you will get familiar with looping
[45:46] Pattern questions are really easy ones
[45:50] You just have to keep few things in mind. First count number of rows and number of columns in each row
[46:17] Then you have to find a relation
[46:22] You have to related your columns with rows somehow
[47:05] Don't directly write 3 there, write columns, because we don't know what input user will give
[47:19] Let's try to code
[47:23] We will do many patterns problems, so don't worry if you don't understand it properly at the first go
[47:41] Ok, so first we took our input n
[47:48] Now I took 'i=1'
[47:55] I am running this while loop until i<=n
[48:07] Either we can take (i=0 and i<n), or (i=1 and i<=n). In both of these cases, loop will be executed n times
[48:55] This while loop is going to be executed for every row. Now let's talk about the column
[49:12] For column, I took a variable j and initialized it with 1
[49:17] How many stars we have in each row
[49:25] In this case, columns = total number of rows
[49:35] so we wrote an another while loop to print stars for n columns. Don't forget to increment i and j
[49:59] Let's run it
[50:05] It printed in the same line. How we can correct it?
[50:09] we have to end the line once all columns of a row is printed, so we will write cout <<endl here
[50:17] Now let's run, you can see we got our output
[50:22] Let's understand the code again
[50:25] First we took our input for the number of rows
[50:32] We declared a variable i to keep the track of the row number
[50:42] This while loop will help us to execute these statements for all rows
[50:46] Then we declared a variable J with 1, to keep the track of the column number
[51:07] and then we are simply printing star in each column
[51:21] Then we wrote this cout <endl, to come to a new line, and after that this code will get executed for all rows
[51:31] If you have not understood it properly, then no need to worry, we are going to do many examples in the next video
[51:39] Let's solve one more question
[51:42] Let's try to print this pattern
[51:50] 1st observation - rows = n
[52:00] 2nd obs - Every row has row number printed in it, col = i
[52:35] Let's code it
[52:37] Taking input of N
[52:43] We will run a loop from 1 to n,(for all rows)
[52:54] Then we declared a variable J for columns
[53:08] J will also run from 1 to N
[53:33] and we simply have to print the row number
[53:41] Incrementing J inside the column loop
[53:53] and Incrementing i inside the row loop
[53:59] Also we have to end the line after each row
[54:07] We are done with our code, let's run it
[54:10] I gave 4 as input. Ooo we are not getting desired output, what mistake we made?
[54:14] Ooo, we intilized J with i, we have to do j=1 here
[54:33] Now let's run it again for 4
[54:35] See, we got the correct output
[54:37] We will code this triangular figure also in next video
[54:47] But first let's understand this code
[54:51] We will run a loop from 1 to n,(for all rows)
[55:02] Then we declared a variable J for columns. J will also run from 1 to N
[55:18] and we simply have to print the row number. Then we incremented J inside the column loop
[55:26] and Incrementing i inside the row loop after printing ENTER
[55:38] If still you haven't understood this properly, don't worry, we are going to do many questions on patterns
