{"id":"IJQMnTV7Us0","title":"Lesson 9: Conditional Control Flow","description":"00:14 If-then-else\n01:10 Guards\n02:11 Case statements\n02:44 Either\n\nWe look at constructs that can be used to alter program flow based on conditions, including if-then-else statements, guards, and case statements. The if-then-else statement in Daml is similar to other languages, but requires an else statement to cover both sides of the boolean condition. Guards can be used to make code more concise and readable, while case statements use pattern matching instead of boolean conditions to branch. We also introduces the either data type, which represents a type with two possible data types and is commonly used for error handling.\n\nFor a better learning experience, sign up and enroll in this free course at: https://daml.talentlms.com/plus/catalog/courses/141","channel_name":"Digital Asset Training","published_at":"2025-02-19T00:00:00.000Z","thumbnail_url":"https://i.ytimg.com/vi/IJQMnTV7Us0/hqdefault.jpg","transcript_text":"There are several constructs that help us alter the program flow based on some conditions. We'll look at if-then-else statements, case statements, guards, and conditional return types. Let's start with if-then-else. The if then else statement in Daml is quite similar to what you may have seen in other languages. Here in the example, we have a function ifOddOrEven that uses an if statement to print even or odd for a given number. The signature says that it takes one int argument and returns text. If X modula two is zero, then it returns even or else it returns odd. As usual, you can add more if statements to the block if you have more conditions to test. An important point to note here is that unlike some other languages where you may write an if statement without and else, Daml requires, you always provide an else statement to cover both sides of the boolean condition. Another way of thinking about an if-then-else statement is that it's not just a statement, but also an expression that must return a value. If you have too many if conditions to check making your if-else block too bulky, then you have an option to use guards to make your code a little more concise and readable. Like if statements you check for boolean conditions, but instead of if-else, you separate them using a vertical pipe. This example, findBmi, takes two decimal inputs and returns the text based on the BMI value. BMI stands for body mass index and is computed as weight divided by height squared; where weight and height are in kilograms and meters, respectively. If the computed value is less than 18.5, then the function returns the text \"underweight\". If less than 25; than normal, and so on. The last guard \"otherwise\" is a catch all. Now, you may notice that this is not a good idea to repeat the formula for every condition; and to address that, we use what is called \"where binding\". Using the same example as before, you can use a variable BMI in each guard condition and then bind that variable to the formula in the where block below. This makes your code much more readable and efficient. You can combine as many variables as needed in this where block. Another similar construct is case statement, which works the same as an if statement, but uses pattern matching instead of boolean conditions to branch. Taking the example of odd or even the caseOddOrEven function takes an int and returns a text. It computes X modulo two and then matches the result with the patterns listed below. If the result matches zero, then the function maps to return value even. If it matches one, the odd. The underscore in the last pattern is a catch-all, which is taken if none of the patterns listed above match. There is a data type called either, which represents a type with values of two possible data types. It's written as either A, B, which means either a value of type A or a value of type B. This data type has two constructors left and right. The left constructor constructs a value of type A, and the right constructor constructs the value of type B. Let's look at an example of how this works. We have a function called someFunction, which takes an int and returns either text or an int. The function definition shows that if the input X is less than zero, then it uses the left constructor and returns a text with value error. This is the type on the left of the two possible types. If X is not less than zero, then the right constructor is used, which returns x plus two, which is of type int which is the type on the right of the two possible types in the function signature. Then in the script, when you apply the function as some function negative two, it uses the left constructor and returns the text error. When you apply the function as some function two, it uses the right constructor and returns an int as four. The most common use of the either data type is an error handling; whereby convention, the left constructor holds the error value and the right container holds the correct or the right value. Let's go over what we've covered. We've learned about three techniques of altering the control flow based on some conditions, if-then-else, guards, and case statements. While if then else and guards use boolean conditions to branch, case statements use pattern matching. We then looked at a special data type called either, which represents a type with two possible data types, one for the left and right constructors to construct the value of these two types.","transcript_synced_at":"2026-10-26T04:03:12.573Z","segments":[{"start_seconds":1,"text":"There are several constructs that help us alter the program flow based on some conditions.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=1s"},{"start_seconds":6,"text":"We'll look at if-then-else statements, case statements, guards, and conditional return types. Let's start with if-then-else.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=6s"},{"start_seconds":14,"text":"The if then else statement in Daml is quite similar to what you may have seen in other languages.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=14s"},{"start_seconds":19,"text":"Here in the example, we have a function ifOddOrEven that uses an if statement to print even or odd for a given number.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=19s"},{"start_seconds":26,"text":"The signature says that it takes one int argument and returns text. If X modula two is zero, then it returns even or else it returns odd.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=26s"},{"start_seconds":36,"text":"As usual, you can add more if statements to the block if you have more conditions to test.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=36s"},{"start_seconds":42,"text":"An important point to note here is that unlike some other languages where you may write an if statement without and else, Daml requires, you always provide an else statement to cover both sides of the boolean condition.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=42s"},{"start_seconds":54,"text":"Another way of thinking about an if-then-else statement is that it's not just a statement, but also an expression that must return a value.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=54s"},{"start_seconds":61,"text":"If you have too many if conditions to check making your if-else block too bulky, then you have an option to use guards to make your code a little more concise and readable.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=61s"},{"start_seconds":70,"text":"Like if statements you check for boolean conditions, but instead of if-else, you separate them using a vertical pipe.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=70s"},{"start_seconds":78,"text":"This example, findBmi, takes two decimal inputs and returns the text based on the BMI value.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=78s"},{"start_seconds":85,"text":"BMI stands for body mass index and is computed as weight divided by height squared; where weight and height are in kilograms and meters, respectively.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=85s"},{"start_seconds":94,"text":"If the computed value is less than 18.5, then the function returns the text \"underweight\". If less than 25; than normal, and so on.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=94s"},{"start_seconds":103,"text":"The last guard \"otherwise\" is a catch all.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=103s"},{"start_seconds":107,"text":"Now, you may notice that this is not a good idea to repeat the formula for every condition; and to address that, we use what is called \"where binding\".","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=107s"},{"start_seconds":115,"text":"Using the same example as before, you can use a variable BMI in each guard condition and then bind that variable to the formula in the where block below.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=115s"},{"start_seconds":125,"text":"This makes your code much more readable and efficient. You can combine as many variables as needed in this where block.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=125s"},{"start_seconds":131,"text":"Another similar construct is case statement, which works the same as an if statement, but uses pattern matching instead of boolean conditions to branch.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=131s"},{"start_seconds":139,"text":"Taking the example of odd or even the caseOddOrEven function takes an int and returns a text.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=139s"},{"start_seconds":146,"text":"It computes X modulo two and then matches the result with the patterns listed below. If the result matches zero, then the function maps to return value even.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=146s"},{"start_seconds":156,"text":"If it matches one, the odd.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=156s"},{"start_seconds":158,"text":"The underscore in the last pattern is a catch-all, which is taken if none of the patterns listed above match.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=158s"},{"start_seconds":164,"text":"There is a data type called either, which represents a type with values of two possible data types.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=164s"},{"start_seconds":171,"text":"It's written as either A, B, which means either a value of type A or a value of type B. This data type has two constructors left and right.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=171s"},{"start_seconds":180,"text":"The left constructor constructs a value of type A, and the right constructor constructs the value of type B. Let's look at an example of how this works.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=180s"},{"start_seconds":189,"text":"We have a function called someFunction, which takes an int and returns either text or an int.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=189s"},{"start_seconds":196,"text":"The function definition shows that if the input X is less than zero, then it uses the left constructor and returns a text with value error.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=196s"},{"start_seconds":204,"text":"This is the type on the left of the two possible types. If X is not less than zero, then the right constructor is used, which returns","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=204s"},{"start_seconds":212,"text":"x plus two, which is of type int which is the type on the right of the two possible types in the function signature.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=212s"},{"start_seconds":221,"text":"Then in the script, when you apply the function as some function negative two, it uses the left constructor and returns the text error.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=221s"},{"start_seconds":228,"text":"When you apply the function as some function two, it uses the right constructor and returns an int as four.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=228s"},{"start_seconds":235,"text":"The most common use of the either data type is an error handling; whereby convention, the left constructor holds the error value and the right container holds the correct or the right value.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=235s"},{"start_seconds":244,"text":"Let's go over what we've covered.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=244s"},{"start_seconds":246,"text":"We've learned about three techniques of altering the control flow based on some conditions, if-then-else, guards, and case statements.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=246s"},{"start_seconds":255,"text":"While if then else and guards use boolean conditions to branch, case statements use pattern matching.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=255s"},{"start_seconds":261,"text":"We then looked at a special data type called either, which represents a type with two possible data types, one for the left and right constructors to construct the value of these two types.","url":"https://www.youtube.com/watch?v=IJQMnTV7Us0&t=261s"}],"youtube_url":"https://youtube.com/watch?v=IJQMnTV7Us0","url":"https://ccpedia.xyz/videos/IJQMnTV7Us0"}