 Hello, hi everyone. I'm Sanjay Gupta. I welcome you on Sanjay Gupta Tech School. So today is day 39 and last session of this week. And in today's session, we are going to discuss about different type of loops that we can use in Apex programming. So first of all, we'll be discussing about for loop, then while loop, then for each. So whatever loops are available and how they works with different examples, so we'll be going to discuss them. Okay, so once again, welcome everybody. So I'm just waiting for a couple of more minutes so that folks can join and then we'll be starting. So till the time I'm just going to show you some of the slides. So if you are joining this session for the first time, so with this slide, you will be able to know about me. Then we are following all the best practices. I hope everybody is following. So if you're new to Salesforce ecosystem and you want to make a career in Salesforce ecosystem, so you can just go through with all these best practices. So I just say like do daily practice, right? So consistency is the key and this is the last session of this week. So you will be having Friday, Saturday and Sunday time before the next session because next week we will be having some complex topics. Those are very important to understand. So prior to that, you just need to complete all the exercises. Right? So consistency is the key. So you just need to follow that because without being consistent, you won't be able to become successful whatever you do. Right? So this bootcamp is totally free of cost. So just utilize it and learn as much as you can. Okay, so initially I will be explaining the topic and will give you lots of demonstrations today and then we'll take all the questions and answers. Right? And if you have not followed like Telegram group, so just follow that so that you can discuss the doubts there as well. So today week 10 will be completed. So like you can see how consistently week by week I was able to complete 10 weeks. So if you will be consistent enough, so I think total 20-25 weeks will be there and all the topics will be covered with the help of this bootcamp. Right? So I will just say you be consistent and if you are doing consistent practice, I really appreciate your efforts. So if you have not followed on YouTube, LinkedIn, Instagram or Telegram, just do follow so that you receive all the notification and share with others as well. And I would request to share some reviews because from this week and last week I am not receiving your reviews or feedback. So it is a humble request. Those who are joining the sessions receive or those who are just watching the recording. So please do share reviews and feedback in the comment section of this YouTube video so that I can know how you are experiencing this bootcamp. And if you have any suggestions, then please let me know so that I can improvise. Okay. So as in yesterday's session, I told you like today I will be first covering conditional operator and then we'll focus on the loop. So we have a conditional operator which is also known as ternary operator. So it is similar to if else condition, right? So here is the syntax that you can see. Here we have total three expressions, expression one, then question mark, then expression two, then colon, then expression three, right? So here you can see all the explanation is written. So expression one is basically known as condition expression two is basically known as true part and expression three is known as false part, right? So I'm going to implement an example for you. So yesterday we saw some examples related to if else. So I'm just going to open yesterday's apex file. And here we use lots of example. So what I'm going to do, I'm going to explain you a simple example of conditional operator. So here you can see we have this code, right? Now if you want to implement this code with the help of conditional operator, so how you will be doing that. So first of all, you need to apply a condition. So I'm writing num one greater than num two. So this first part is basically an expression. So num one greater than num two is basically an expression here. Now after this question mark, I just need to write true part. So I'm just copying and pasting it. And here I need to write colon. And then I just need to provide the false case. Okay, so this way you can see this conditional operator is implemented. So first we have this condition, then we have true part. And after that, we have false part, right? Now I'm just commenting this block. Okay, so if you pass two values into this num one and m two, so those two values will be compared with the help of this conditional operator. Okay, so I'm just saving it. And I'm going to call this max method so that we can see whether it will be working fine or not. So I can see it is showing an error message. So let me just see expression cannot be a statement. Okay. So what we can do, I'm just creating one integer result. And actually, we cannot write it like this. So I'm just changing it, removing these statements. And here I'm writing num one, then colon, I'm writing num two. And through system dot debug, I'm going to display value of result. So maximum equals two. And here I'm going to show result. Let's see it saves or not. Yes. Okay, so just forget what I explained earlier and focus on this. So you cannot use system dot debug directly in true and false block. So what we did here, this is the condition num one greater than num two is the condition. So if this condition is true, so this num one value will be assigned into this result variable. And if it is false, so num two value will be assigned into this result variable. And then we are going to display the value which is available in the result. So this way you can implement conditional operator, right? So you have both options. If you want to use if else condition, you can go with that. And if you want to implement conditional statement, so you can go with that as well. Okay, now I just saved this code, I'm going to run it. So I'm just going to copy the name of class. And the method name is max. So if I take you there, so our method name is max, that we need to execute, right? So this method name is max. And here I need to pass two values. So I'm passing 20 and 40. Right? Now I'm going to execute it so that we can see the results. So here you can see maximum equals to 40. So it is working fine with the help of conditional operator. So this way you can just practice it and generally we don't prefer conditional operator. But if you have the requirement like you don't want to use if else, because it is having lots of statements, so you can just summarize it basis on conditional statement. Okay, now moving to next part, that is loop statements, right? So I will be explaining you three types of loops that we can implement in apex. So in which first is for loop, right? So I'm just going to explain everything from the beginning. So if you are a beginner and you don't know how to implement a loop, how it actually executes, so you will be able to understand today. So here is the syntax. So syntax basically means how to write particular feature. If you don't know about, if you don't know the meaning of syntax or syntax means how to implement a particular feature, how to write it. So here you can see, first of all, we need to write for then we have parenthesis, then we have initialization, then semicolon, then termination condition, then again semicolon, then increment or decrement, right? So what happens here? So first of all, you need to provide initialization as a value, beginning value, then conditions will be checked. If condition is true, then the statements which you will be writing will be executed. Once these statements are executed, then control will go to increment or decrement, right? So increment and decrement will be done and basis on that again condition will be tested. If condition is true, then statements will be evaluated and this cycle happens until your condition is false, right? So here is the example. So this is a simple example. Here i equals to one is basically initialization. Initialization means the beginning value. So here we are declaring a variable named as i. Variable name can be anything. So right now I just used i of integer, right? I just use i of integer. So i is starting from one condition is i less than equals to 10. So if i is less than equals to 10, then loop will iterate. Otherwise it will be terminated and i plus plus means i will be incremented by one every time, okay? So what will happen? First of all, i will be equals to one, then i less than equals to 10. This condition will be tested. So if condition is true, then system.debug will display the value of i. Then i plus plus will perform. So i will become two and then again condition will be tested. So always remember this initialization will be done only once. After that condition statements and increment decrement always works, right? So the same example I'm going to implement here so that you can understand. So I'm going to create a class naming it as loop demo. And in this class, I will be implementing all the examples. So public static void for demo. So here I'm going to implement a loop. So integer i equals to one, i less than equals to 10 and i plus plus. Then here I'm writing system.debug i, right? So this is our loop. It will repeat 10 times. So I just told you this is initialization which will happen once and then this condition will be tested. If it is true, then system.debug will be executed then i plus plus and then i will become two and then again control will come to this, right? So now if I want to execute it, so I'm just saving this code. So class name is loop demo and here I'm writing for demo and I just clicked on execute so that I can see the debug. So here you can see in different lines, results are available. So in previous session, in few sessions, you asked me like how we can display the result in different lines. So if you have a collection, maybe list set map and you want to show the results in different lines, right? So this is the way you can just implement a loop and through that loop you will be able to display the results in different lines, right? So I will I will show you that today when we'll be discussing about S object. So I hope with this simple example you understood how loop works, okay? So basically what is happening this system.debug statement is executing 10 times. So the purpose of implementation of loop is if you want to repeat one or more than one statements, more than one time. So instead of writing them again and again, you can write them inside a loop, right? So this is the basic purpose of implementation of loop, right? So don't write statements again and again, just implement a loop so that they will be executed automatically, right? So now what I'm going to do, I'm going to implement one more example for you. So next example is we need to print table of a number, right? So user will be providing a number and on that number we'll be applying a loop, right? And we'll be displaying the result, right? So someone is asking can we initialize string? So we cannot because on a string you won't be able to apply proper condition. So generally if you want to implement a loop so we prefer integer numeric values so that we can increment or decrement those values. So now I'm going to create a method named as table and this method will be receiving a value, right? So here I'm going to receive a value from user so that value will be stored under n, right? And here what I'm going to do so I will be implementing a loop. So basically if you want to print table of a number, if you want to print table of a number, so what we need to do table basically contains 10 digits, right? Table basically contains 10 digits. So if you want to print a table of a number so loop will be repeating 10 times, right? So here what I'm going to do so here I just started loop from one and it will be repeating till 10. Okay, so here this is the condition. So what I'm doing here I'm multiplying n with i. So n is the number for which we need to display the table and these are the values like I will be having different values and basis on that it will be displaying the result. Okay, so guys I'm just coming back in two minutes hold on. Okay, so sorry guys there was some issue and I'm just back. So I was just explaining this example to you. So basically here in this example we are receiving a number from user into this n variable and this loop will be repeating 10 times because if you want to display any table so table basically contains 10 digits, right? So loop is starting from one and it will go up to 10 and here every time the number will be multiplied with i, right? So initially I will be one so whatever number is number will be multiplied with one then next time I will be incremented by one so it will become two. So our number will be multiplied with two, right? So this way the results will be available. So I'm going to just execute this so that you can understand the result. So I'm just entering five here. Okay, so this table function will be receiving five as input. So this five will go to n and basis on that result will be available. So here you can see the results it is showing five, 10, 15, 20, 25, 30, 35, 40, 45 and 50. So 10 times results are available. So what happened here I'm just explaining the process again. So i is initially one then condition is true i less than equals to 10 then we have system.debug. So here you just need to see we passed n as five. So what will happen first time it will be multiplied like this five into one because n is five and i is one. So initially it will be five into one. So the result will be displayed then I will be incremented. So next time the value of i will become two. Okay, so two is less than equals to 10. So now what you need to do here it will be calculating like five into two. So n will remain same throughout the loop but i will be incremented by one every time and this process will execute total 10 times. Okay, so this way basically it is working. Now we have next example like how to print even or odd numbers up to n. So I'm going to show that example to you. So I'm implementing it here so that you can see it properly. So first I'm going to show you how we can display all the even numbers up to n. So n will be a number it can be 20 30 50 any any number. So from the beginning like from let's say one one to that n number whatever even values are available we need to display them. Okay, so here what I can do I can just implement a loop integer i equals to one i less than equals to n i plus plus then if so I'm just going to use math function mod that we used yesterday. So here I'm going to use I then to if I is completely divisible by two so the remainder will be zero and here I'm going to write system dot debug i so if i is divisible by two then only value of i will be displayed. So this way inside for loop you can implement if condition as well. Okay, so I hope this way this example you understood and someone is asking to display the resultant table format. Okay, so for that what we can do you so you want five into one equals to five so n then you need to write plus then multiplication sign then again plus then I then plus then equals to then plus so this way you will be able to see the result right if you write it like this and will be displaying the value then plus multiply will display as is then I will display its value then equals to will be displayed as is then n into i so this way with this single statement you will be able to display table and table format. So I'm saving this code. So first I'm going to show you table format again. So this is a result you can see now result is in table format. Okay, and if I want to run this even number so here I'm passing even num and 50 and executing it so you will see all the even numbers in the result. So here you can see starting from two till 50 all the even numbers are displayed and all are displaying through line number 11 and if you want to see the odd number so you can just copy paste change the method number method name or num and here you can write not equals to zero instead of double equals to I am using not equals to write and I am just saving it and I can see people are asking questions so just ask your question after the explanation I will take your questions so here I'm just using odd num and I'm executing it so now you will see results adds odd numbers okay so this way I hope you are able to understand how we can implement different type of loops in apex and I just want to explain one more example then I will focus on the chat so that I can answer a few questions so next example is print some of natural numbers okay so I'm going to implement this as well so here method name is some of natural lumps and and will be receiving the and the limit so let's say if you are entering 25 into n so 1 to 25 all the digits should be added so here I am creating integer so someone is asking can we use this integer outside the loop so yes we can so this way you can write it so now what I can do I can write it i equals to 1 i less than equals to n then i plus plus and inside the loop you will be writing sum equals to sum plus i and after completion of loop you will be showing the sum right so I think this is very clear and easy to understand we cannot write system.debug inside the loop because if you want to add more than one numbers so inside loop first they will be added and once all the numbers are added then only one sum you want to display right so for that case what you need to do you need to write your system.debug outside the loop okay so if you if you write your system.debug outside the loop so it will execute once and this statement will be executed again and again so initially sum is zero and whatever value of i is it will be added into sum so sum will be containing all the submission and once this loop is completed so result of sum will be displayed okay so I think there is no more questions all the questions are already answered and I think this is very easy to understand as well now I'm just moving to next example which is related to while loop so just focus on this syntax so here in the syntax you can see we have initialization then we have while condition then statements increment decrement so the process is same the way of writing this loop is different right syntax is different but the overall process is same so initialization we need to do one time then with while we need to write the condition then statement will be evaluated and then increment or decrement whatever you want to do you can just write so this is the example so integer i equals to one then while i less than 10 or less than equals to 10 whatever you want to write then system.debug will display the value of i and then i plus plus okay so this way the same for loop you can implement with the while loop as well so there are two variations available it depends on you which type you want to use right okay so we have these examples like print table of number print sum of natural number so i'm just going to show you how we can implement them using while loop as well so i'm just copying it pasting it here table with while and i'm going to convert it into while loop so this way if you write your loop so it will become why okay so overall process is same first i will be starting with one then this condition will be tested i less than equals to 10 if it is true then system.debug will be doing this calculation and then i plus plus will increment value of i with one so the same results will be displayed so if you want i can just run it so we can see the table of 50 so whatever number you will be passing its table will be displayed like this okay so i hope this is also easy now let me take couple of questions okay narindra is appreciating my effort thank you this motivates me and like your feedback and motivation or your positive words is medicine for me because i am doing it continuously from last two months so thank you for appreciating the efforts pravin is asking in last example can we have to use this dot sum in loop we have already known we don't you we won't be able to use this dot sum if you declare the variable here outside the method then you can write this dot sum right now your variable is inside the method so it is local for this method only that's why you cannot use this dot sum okay so i hope your question is answered right pravin so i just explained for loop i just explained why loop now next and very important loop that you need to understand and in trigger in each trigger scenario we will be using this loop right so this loop is very much important to understand okay so i am going to explain it so listen it carefully if you are watching live then you can just ask the questions and if you are watching the recording so you can ask questions in the comment section or you can just ask questions in the telegram group as well so this loop is very much important so just understand the syntax here at right hand side you can see we need to use the collection variable so always remember this loop for each loop you can use with collection variable only so if you have list set or map then only this loop will be useful right so here you will be having the list set or map means collection one by one the values will be available into this variable okay and whatever type of collection you are using same collection type as data type you need to use it for this variable okay and whatever statements you want to execute you can write so no initialization no condition no increment decrement everything will be managed automatically with this first line okay here is the example simple example so ACC list is the list collection variable one by one each account record will go to this ACC variable so this ACC is a variable at one time it can store one account information and this account is the data type for this ACC then inside this loop we are using system.debug and here we are showing the ACC account information okay so in initial sessions people ask me how we can display IDs or account information in different different lines so this is the way you can display those results into different lines okay so i'm going to show you before that just focus on this slide so now the examples which i'm going to show you we'll be using everything together so you will see s object like account contact or other custom objects you will see list set map if else loop whatever we have learned so far in apex everything i will be combining and will give you examples right so now i'm jumping to these examples and you can see this last line is also important we need to apply null check that we understood yesterday okay so i will be covering everything together so i'm going example by example so first example is iterate on account list and display account information okay so requirement says you need to iterate on account list so before applying iteration we need to create account list as well and to create account list we need to fetch the data which is available under under account object so i'm going to show you everything okay so i'm creating a method public static void naming it as show list then here i'm creating list of account acc list then new list of account so here you can see i just created a variable of type s object so account is s object that i'm using here i'm using list as a collection so i hope everybody knows about this if you are following all the sessions then we need to query some data from org so what we can do we can write acc list equals to select id name from account and here i'm writing limit as five okay so remember carefully right now we have not discussed about soql in detail so there is another session planned for soql so you can see next week monday we'll be discussing soql in detail right so there are lots of examples available so i will be modifying this cheat as well or maybe it it it can take two sessions also because we have variety of variations in soql so i will try to explain each and every example to you right so just understand the simple soql which says i need to query five account records along with their id and name okay that's it and all those five account records will be stored in this acc list now i need to implement a loop so that one by one i can show the details but before implementing loop i need to know whether this acc list is null or not right it means it can happen account object is having no record there is no record available under account so what will happen acc list will be having null so before implementing loop what we need to do we need to apply a null check so here i'm writing if acc list dot size greater than zero so this is the one way this is the one way to check list for null if its size is greater than zero then you can implement a loop okay i will show you another way as well but before that i'm going to implement the loop so this is our for each loop so this is account list through this account list one by one account record will be available into this acc and it is the type so acc and acc list both will be of same data type okay but it is a collection and it is a single variable that is the difference now here i'm writing system dot debug so i will be showing id and name both so i am writing acc dot id then hyphen then plus acc dot name so this is the iteration okay so here null check is applied and the loop is implemented and result will be displayed okay so this is the one way of applying null another way is this so you can use is empty method and here you need to apply not this i explained yesterday as well so account list is empty if it is empty it will be true but if it is empty then we don't want to execute this loop so i just applied not it will convert true into false and if is empty is false means list is having some data so then we are applying not so that that false can become true and this loop will be executed okay so i'm making it comment so that only first block will be executed now i'm going to save and execute it so you will see the results will be available in different lines here you can see so id then hyphen then account name id hyphen account name so all the account information is available in different lines so this way you can just show the data which is available in single line you can just break and show them in different lines with the help of loop okay so this way i hope you understood how we can apply loop on list along with null check just prveen we can use soql here in place of list so this i will be covering in the next session when we will be understanding soql in a proper form right so i don't want to make it complex because my focus is to explain how loop works okay so that i will be covering in the next session for sure so next we have iterate on account list and create a set of account id's so i think this we already understood in previous sessions but again i'm going to show you so i'm creating a set of id and here along with displaying the result i'm going to add account id's into this set okay so this way i'm able to add account id's into set then we have another requirement iterate on set of account id's and display all the id's okay so i'm going to explain you requirement number three iterate on set of account id's and display all the id's so in the same method i'm going to do this operation now what i'm going to do i'm going to first apply null check for this acc id's i'm checking through is empty and here i am writing not so this is null check for set so here i am writing the comments now so this is iteration on account list to display data and create set of account id's right now here we are implementing a loop which will display account id's through set so here i'm going to write for id acc id and then acc id's so see it carefully why i used id data type here because this acc id's set is of type id okay so set is of type id that's why i used id data type here then acc id is just a variable and this acc id's is a basically set and now here i can just write system dot debug acc id so this loop will be displaying only id's of the account okay so here we have two things now this loop will display id and name through list and this loop will display only id's through set so i hope with this example you are able to understand how we can iterate on list how we can iterate on set later i will be showing you how you can iterate on map as well so i just saved it i'm going to execute it now you will see two different results so here line number 12 is showing list data through list and line number 20 is showing data through set only id's okay so this way i hope you are able to understand this now we have another requirement iterate on account list and create a map of account id's and account okay this also i think we understood earlier as well so i'm going to add this here so i'm creating account map okay and here inside this loop i'm going to fill that map so for map we need to use put method and we need to add key so keys will be id's and values will be account so this way we can add okay so i'm just modifying it create set and map so iteration on account list to display data and create set and map so here set is created here map is created so i'm just writing the comments set is having data map is having data okay this is another loop for set only now if i go further so i have two more requirements iterate on map created above using keys so in map we have two things keys and values so if you want to iterate on map so you need to decide you want to iterate basis on keys or basis on values right so i'm going to do both for you so iterate map based on keys so again i'm applying if condition so that i can check map for null so not accmap dot is empty right if map is not empty then we can apply a loop so first we are applying loop basis on keys now we need to check the data type of key so keys data type is basically map of id id is the key data type so here what i need to do i need to write id acc id and here i am writing accmap dot keyset so if you remember the method keyset keyset is the method where if you apply this method so it will give you all the keys so automatically all the keys will be retrieved and one by one those keys will go to this acc id variable and now here you can write system dot debug acc id so this way you can iterate a map basis on the keys right now we have next requirement we need to iterate on map created above using its values now map is having values as account so i'm going to show you that as well so now what we are going to do we are going to iterate map based on values so if so condition will be same if accmap dot is empty so if it is not null so here i am applying not so if map is not null so we can apply a for loop so this time data type will be account acc then accmap dot values so if you want to fetch all the values through map so you need to use this values method that we already discussed in map session so here i'm going to write system dot debug and i'm just going to show acc so acc will be showing account id and name together so this way i hope you understood how we can iterate on map and later on like whatever logic you want to apply inside this loop that depends on the real time scenario okay so now i'm going to show you this one by one so i'm going to have a few system dot debugs so display data through list so that we can identify so here display data through set then here display data through map keys and here display data through map values okay so all the different loops are implemented here now if i save it and execute it so you will see different results so first of all you can see display data through list so you have five results line number 14 then line number 21 says display data through set then we have five id's then display data through map keys and display data through map values so all the results are displayed here in separate lines okay so this way i hope you understood everything clearly like how we can use different collections how we can apply null check through conditional statements how we can implement loops on list set and map right so this is it for today's session this is all i prepared for today's demo and i hope you enjoyed the session you understood a lot today and whatever your reviews are or whatever your feedbacks are just share on the live chat also on the comment sections if you are watching the recording just share your reviews or feedback in the comment section so that it will give me motivation to do all sessions in the same way like i'm doing and just to tell you next week is very much important because we will be discussing about soql dml so these are the like key concepts of apics if you are master in soql and dml then you can implement your apics code properly right so thank you so much for joining today's session and i can see there is no much questions available right so i think there are no question that i need to answer if you have any question you can ask so that i can answer if i missed any question you can just ask so that i can answer that otherwise you can just leave and next session will be on next week monday so till then do lots of practice complete all the exercises which are available in the exercise sheet and we'll see more exercises in the next week so aparna we will be using map set list in trigger so basically right now we are learning how we can use apics so while learning trigger we will be actually using apics to solve the problems right so right now we are getting familiar how we can implement different features of apics thank you thank you everybody thank you sashi praveen pranay sunil yeah sunil i will be giving more assignments next week yeah debarshan if you if you see the session tracker so lwc is already mentioned there along with javascript yes satish i will be sharing admin interview question pdf after this session so just just join the telegram group if you have not joined so in the telegram group i will be sharing the pdf so i'm just sharing the link in the chat so that you can follow so this is this is the telegram channel you can just subscribe where everything i am going to share so uh sheik is asking for schedule so schedule is available under this video description so if you go to the description so you will find the session tracker if you don't have so let me just share it with you so i'm just pasting that in the chat so that you can have it amya happy round of me thank you navin thank you rajiv sanjith thank you akula sandeep yes bhanu if you see the session tracker everything i will be sharing everything i will be teaching these are like as in chrono sapix so we will be discussing them as well so ashok sub query we will be understanding first in upcoming sessions and then i will tell you how through loop we can show them in different lines thank you sashi thank you sunita thank you mahesh thank you nikhil thank you seconder happy weekend thank you sunil thank you praveen yes praveen we can have s object in for loop so i will be explaining that in next week thank you rivti thank you so green art you are asking a question which is related to c or c plus plus programming so i i we can do but i won't be able to show because this is logic based question so you can solve it through c or data structure knowledge okay thank you everybody so i think today's session was awesome and i'm happy like you shared all the positive feedbacks and i will do all the remaining sessions with the same enthusiasm thank you so much for joining today's session appreciate your time bye everyone see you next week