New Ethereum talks, every Monday. The week's conference uploads by event, in your inbox.

Loading player…

Control Structures: Standard Control Structures

BaseMon, Oct 9, 2023, 12:00 AM

Control the flow of your Solidity programs with conditional structures! 🚦📝 Master IF, else if, and else statements to create dynamic smart contracts. Watch as we compare numbers and return customized messages based on different conditions. Enhance your Solidity skills with this straightforward yet valuable lesson! 🧠💻 📚 Head to Base Camp for full courses on smart contract development: https://docs.base.org/base-camp/docs/welcome ✅ Subscribe to our channel here: https://www.youtube.com/@BuildonBase ► Build on Base: https://base.org ► Follow us on Twitter: https://twitter.com/buildonbase ► Join our Discord: https://base.org/discord

Transcript

in this lesson we are gonna learn about conditional control structures in solidity we are gonna cover if else if and else let's start with a simple smart contract called control structures and we are going to create a function that we are going to call compare numbers what this function is going to do is essentially receive a number one and a number two is going to be external and we are going to return a string based on different conditions the first condition is if number one is greater than number two then we're gonna return a message let's say number one is greater than number two okay is number one is less than number two we say something similar but in this case is less than number two and finally if none of these conditions are true it means the number one is equals to number two okay so this is star coding we'll start with the first condition we use the if keyword and we say if number one is greater than number two we simply say return and we can just copy this message okay now we can announce if with the second condition I will say if number one is less than number two we simply return this message okay and finally if this condition is false and also this condition is false then it means that number one and number two are equal so we simply return a message that says number one and number two are equal okay let's change this to be up your function because we are not accessing the state at all okay so this is a pure function and let's test it let's deploy the contract and now let's call compare numbers and let's pass let's say one and two we call it and we get that number one is less than number two now let's call it with number two and one in the opposite and now we can see the number one is greater than number two okay and then let's pass two one two so we would expect this part to be executed and we get number one and number two are equals okay while this example is very simple I hope it gives you a better understanding of the possibilities to manage the control flow of your solidity program by using IF else if and else

Automatic transcript — names and jargon may be misspelled.