If and Loop in Power Automate for Desktop (Full Tutorial)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so we want to create a guessing game i admit this will not be a best-selling game but it will teach us the basics of if and loop so we'll create a game where we'll assign a variable between 1 and 100 and then we will let the user guess the number if the user is wrong then we want to give him or her another guess to guess it and we can give him a hint saying that guess higher or get slower let's see how we can build that and at the same time learn about even loop so go to microsoft power automate for desktop and then click new flow here and i will call it if and loop and i'll click create there you go first we will assign a variable with our secret number so if i go over here to input output variables i can create a variable i can also create it with set variable that just depends on what you want an input variable that can come from another place in the microsoft universe so let's practice using that there'll be no difference to it so if i click add argument input and then i'll click create it i'll say secret number like this and then i'll delete the rest here i'll give it a default value let's just pick 62 that's fine i'll also give it the same name as external name there's no difference to it we'll not use the external name here but it's just nice to do it if you want to do it in the future then i'll click create the only difference that is is to create it here then what we're used to in the flow is that when we don't map it to another place in the microsoft universe the power automate for desktop runner will ask us if we want to use this value that's fine for now don't worry about it just know that you can create your variables up here instead so now we will ask the user to pick in a number so i'll say input dialog here i'll find a display input dialog and we can call this guessing game and then we can say pick a number between 1 and 100 like this we can see that the user input that is the value that the user has been providing and remember that this even though the user provides a number it will get stored as a text that is fine and we can see that this user input that is the user input that the user will provide so let's rename this and this one will be the user number so that will be the guest number let's rename it you don't have to use the percentages sign if i do this and then i click back back power automate for desktop automatically adds these that is fine i'll click save so now we have a secret number and we let the user guess let me show you how that works so if i click run here and let me just drag in this guessing game input dialog i can give it a number so if i say 45 then i click ok nothing really happens because we haven't made any comparison yet but as you can see here we have the button pressed that was the okay we don't use that variable but the guess number that is 45 these flow variables only have values when the robot runs but we can inspect them afterwards that is nice when we want to debug or develop like here now use an if so go over to your actions and search for an if and drag it in here we will ask whether or not these two are equal to each other so i will say if i click this x here i will say secret number is that equal to guest number then we know that the user has guessed right i click save and if asks a question here in the condition and then if this is true the actions in between here that is here they will be performed so they will only be performed if the condition is true so now i can find the display message like this i drag it in here so here i can say congrats you guessed the secret number like this that is fine we click save and now when we run the flow let me try to run it once more i will drag in this input dialog i'll make a wrong guess first i'll take 12. nothing happened that is because this one is not true these ones are not equal to each other if i click run once more and if i guess the right number i'll pick 62 i will have the you guess the secret number this display message will be performed because the condition is true so far so good but this one will only one run once we also need to make it run over and over to make sure that the game runs until the user has picked the right number we will use a loop for that so i'll search for a loop like this and then i'll pick the loop condition i'll drag it in above the if because the if will come to the last that will be if our user has guessed correct then this one will go in now a loop will run it's a bit the same as an if and if we look at the condition like we had here then run this once a loop will also look at the condition and then run over and over as long as the condition is true let me show you so now i'll say if the user has guessed right wrong then i run this loop to run over and over until the user has picked the correct number that is the secret number at the value of the secret number so here again i'll say secret number like this and then i'll say not equal to that means that the user has guessed wrong because if i map it or if i compare it to the guest number here so i will say as long as these two are not equal that means that the guess number that is the one that the user puts in is not equal to this then the user has been guessing wrong then this loop will run over and over i can just put in another display dialog i'll copy this we'll make a little bit more advanced solutions in a few minutes but let's just pick this one first i'll drag it into ctrl v and drag it in here here i will just ask for the number again later on we will give hints so let me try to run it and see that it actually works we know that it is 62 let me drag it in so if i keep guessing wrong and i need to keep picking these input dialogs in then this flow will run over and over and first when i pick the correct number then this condition will not be true anymore we will jump outside the end and evaluate on here and now this one will be true so we will get this a congratulation message so far so good so now we we both used an if and a loop now we just need to give the users hints so when we are here and the user has guessed wrong we want to give him or her a hint so we will we need to evaluate whether the secret number is less than the guess number or if it's higher what do you think about the quality of this lesson please post it in the comment below that will help me a lot thank you so i first find an if we'll have an if in here that will ask is this secret number less than the guess number so if i find an if i will drag in the if here so now we will say is the secret number lower than the guest number that means that the guess number is too high the user has guessed too high so i find the variable here i'll say secret number is that less than the guess number here then the guess number is too high we will tell the user that so i'll click save and let's just drag in this display input dialog we'll use that one here so i'll double click to open it i drag it inside the if so right in front here i'll say you guessed too high pick a number between 1 and 100 again we will store it into the guest number so now we have it if the user has guessed too high then we'll tell him or her that now we'll just have an else so search for an else here and drag it in underneath the if and if we'll perform this action if this condition is true else it will do something else we can use that so again copy the display input dialog and paste it in here and then just drag it down here so we know that if this is not true that means that the guess number is lower than the secret number and we can say that you guessed too low pick a number between one and one hundred so we'll just fix this and then we'll click save now let's verify that this actually works so i drag in this so if i put in 12 that will definitely be lower than i want the message saying that the retractor is in you guessed too low that was fine let us just do once more 25 you guessed too low you can even see where we are we are in this branch here we guessed too low now let's guess too high to verify that this also work 75 you guess too high so this loop works it will ask us over and over because we have guessed wrong and then we'll get hints based on whether or not we have guessed too high or too low and let's have a 62 and we guess the secret number now we will add another thing because here we have created an infinite loop that will run over and over i know we can cancel but imagine a robot with a unlimited loop that will mean that we can create a robot that will run forever if we don't notice it that is very bad practice so we will say that this loop should only run like 10 times or a number that is suitable so to do so i'll have a variable that one would be a counter so i will start by one so i'll find a set variable this one will be set in the beginning up here i will call this counter so go into the name here call it counter and give it the value one so this is just a helping variable that we will use so this one will be 1 the first time it runs and then in the end of the loop we'll give it a 2 3 4 and then we'll say if this value is larger than 10 then we want the loop to stop running let me show you how to build it this is also a very nice exercise it's necessary in your loop so you don't create loops that runs forever so drag in another set variable in the end of the loop the name that will still be counter and here we will add 1 to that counter that will be in the end of the loop so click the x here find the counter to add 1 to this variable simply just go inside the percentage design and press plus one then click save so now we add one for each one of the rounds we don't do nothing with the value yet so now we want to make a condition say like 10 times or maybe 15 whatever just pick a number between 10 and 20 your lucky number for example and then we want the loop to run these times it's not that important it's just important that we'll create some sort of a border so if i go into the loop condition now we will change this a bit because when we need to have two conditions we will run this loop whether or not these two are not equal to each other that means that the user has guessed wrong but we also only want to run it when the counter is less than eg11 to do so we'll introduce the ant in the condition this is a bit more advanced but try to understand it or at least build it with me and then we will copy the guest number up here and we will say is this we'll do the exact same thing as before we'll say not equal to guest number that is done like this you can even see it here then this can be true or false and what i will do here is that i will say is this expression equals true that means that these two numbers are different different you can just repeat the video of a right to discover what's going on here because this is quite important so if this is true that means that the numbers are not the same the user has to get wrong so i'll do like this i will say true so if this expression is true then we want the loop to run once more let me show you that it actually works we have not implemented the counter yet but let me try to run it let me drag it in here so if i say 12 we will run once more it will work exactly like before and i can even guess too high that is fine and i will get the right number so now we just need to implement the counter and the reason we change this condition is so we can make an end up here to do so i'll say and and then we can use our another expression here i'll just say is our counter less than 11 then we want to run it so if i click save here and now this loop will run 10 times and if we haven't guessed the right number by then it will jump to the end and then it will say well you haven't guessed this one this condition is not right so we will go to the end no message will come because we haven't guessed the right number let me show you so i will just be making 10 random guesses either too high or too low to show you so the counter you can see it here do like this 79 that's my year of birth i'm not getting any longer unfortunately but that's okay pick one pick two so we will just need to have the counter up all the way so it will run whenever the count is 10 and then when it's 11 it will stop running so run two times more here and i'll pick number three now the loop stopped because the counter was 11 so it will not run the loop anymore because both conditions needs to be true we have an end here and we didn't perform this if that's right let's just add one other thing we have a static number here that is a secret number let's generate a random number instead couldn't that be nice so i'll delete my secret number up here delete it it'll give us an error that's fine because we use the secret number a couple of times but find a generate random number like this and drag it in in the end this one will generate a random number remember to have it 1 to 100 because we asked the user for a number between 1 and 100 the other one will be a bit tricky now rename the variables produce the name here to secret number and since i'm lazy i'll only take the start like this so now i'm signing the secret number randomly and if i run it in here from i can just remember to click save because we haven't have auto saved on and i can click run i can of course see it is being generated here so i can inspect it but and let me just guess right that's it we guess the secret number but the trick is if i'm minimizing this and if i go here and if i run the automation outside here we will not be able to see the debugging window so now i'm completely on my own i'll pick 675 i guess too high and i'll say 45 guess too high still 25 too low let me just i guess the correct number so that's it so we can use the random number and if we just start it from outside the user will not no now you learned about if and loop very important concepts you know about advanced conditions using the end and we talk about counter variables to make our loop not go on forever that is the best practice that you'll need to learn if you missed something just rewind the video and else the next lesson is here on the screen just go click it
Info
Channel: Anders Jensen
Views: 12,425
Rating: undefined out of 5
Keywords: anders jensen, power automate desktop, power automate for desktop, if and loop in power automate for desktop, loops in power automate for desktop, loop power automate desktop, if in power automate desktop, power automate for desktop full tutorial, if condition power automate for desktop, loops power automate for desktop, power automate desktop tutorial
Id: b_IfUrG79dY
Channel Id: undefined
Length: 17min 12sec (1032 seconds)
Published: Mon May 16 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.