{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Branches (if)\n", "\n", "Watch the full [C# 101 video](https://www.youtube.com/watch?v=y4OTe8LSokg&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=8) for this module." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Basic If\n", "\n", "If statements makes decisions.\n", "\n", "> Try out our first if statement:" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer is greater than 10.\r\n" ] } ], "source": [ "int a = 5;\n", "int b = 6;\n", "if (a + b > 10)\n", " Console.WriteLine(\"The answer is greater than 10.\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "In words, this if statement is saying \"if a plus b is greater than 10, write the line 'The answer is greater than 10'\".\n", "> What would happen if a + b is less than 10? Try editing the previous code to see.\n", "\n", "Did nothing happen? That's great! Since the the **conditions** (a + b is greater than 10) of the if statement weren't met, the code didn't go into the if statement, and therefore had nothing to print." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## What's a condition?\n", "\n", "The `condition` is the statement in parentheses after the `if`. A `condition` is a boolean, which means it has to return a true or false. that means using symbols such as `>`, `<`, `<=`, `>=` or `==`.\n", "> Practice boolean statements. Try out some different symbols and numbers to see the answer." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "This condition is False\r\n" ] } ], "source": [ "bool outcome = 3 > 5;\n", "Console.WriteLine(\"This condition is \" + outcome);" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Else\n", "\n", "Before, if the conditions of the `if` statement weren't met, the entire if statement was skipped. But what if you want something to happen in both cases? `else` is what happens if the conditional comes out false.\n", "\n", "> Run this code and change the conditional a couple times to see the different outcomes." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer is not greater than 10\r\n" ] } ], "source": [ "int a = 5;\n", "int b = 3;\n", "if (a + b > 10)\n", " Console.WriteLine(\"The answer is greater than 10\");\n", "else\n", " Console.WriteLine(\"The answer is not greater than 10\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Multi-line If statements\n", "\n", "What if you want more complex code in your `if` statements? That's great, just add curly braces around what you want done.\n", "\n", "> Try it out! Run the following code." ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The answer is not greater than 10\r\n", "Or the first number is not equal to the second\r\n" ] } ], "source": [ "int c = 4;\n", "if ((a + b + c > 10) && (a == b))\n", "{\n", " Console.WriteLine(\"The answer is greater than 10\");\n", " Console.WriteLine(\"And the first number is equal to the second\");\n", "}\n", "else\n", "{\n", " Console.WriteLine(\"The answer is not greater than 10\");\n", " Console.WriteLine(\"Or the first number is not equal to the second\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`&&` means \"and\". It's a way to link up multiple conditionals. You can also use `||` as \"or\".\n", "\n", " The `if` conditional above checks that adding a, b, and c up is greater than 10 AND that a equals b. If both are true, it goes into the `if` statement; otherwise, it goes into the `else` part." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Playground\n", "\n", "Play around with what you've learned! Here's some starting ideas:\n", "> Try out the \"or\" symbol in place of the \"and\" in the previous code. What do you need to do to get true vs. false?\n", ">\n", "> What if you put if statements inside of if statements (a nested if statement)?\n", ">\n", "> Can you draw out the flow of an if statement? A nested if statement?" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "dotnet_interactive": { "language": "csharp" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Playground\r\n" ] } ], "source": [ "Console.WriteLine(\"Playground\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Continue learning\n", "\n", "There are plenty more resources out there to learn!\n", "> [⏩ Next Module - What Are Loops](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/08-What%20Are%20Loops.ipynb)\n", ">\n", "> [⏪ Last Module - Numbers and Decimal](https://raw.githubusercontent.com/dotnet/csharp-notebooks/main/csharp-101/06-Numbers%20and%20Decimals.ipynb)\n", ">\n", "> [Watch the video](https://www.youtube.com/watch?v=KT2VR7m19So&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=2)\n", ">\n", "> [Documentation: Branches and Loops in C#](https://docs.microsoft.com/dotnet/csharp/tour-of-csharp/tutorials/branches-and-loops-local?WT.mc_id=Educationalcsharp-c9-scottha)\n", ">\n", "> [Start at the beginning: What is C#?](https://www.youtube.com/watch?v=BM4CHBmAPh4&list=PLdo4fOcmZ0oVxKLQCHpiUWun7vlJJvUiN&index=1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Other resources\n", "\n", "Here's some more places to explore:\n", "> [Other 101 Videos](https://dotnet.microsoft.com/learn/videos?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [Microsoft Learn](https://docs.microsoft.com/learn/dotnet/?WT.mc_id=csharpnotebook-35129-website)\n", ">\n", "> [C# Documentation](https://docs.microsoft.com/dotnet/csharp/?WT.mc_id=csharpnotebook-35129-website)" ] } ], "metadata": { "kernelspec": { "display_name": ".NET (C#)", "language": "C#", "name": ".net-csharp" }, "language_info": { "file_extension": ".cs", "mimetype": "text/x-csharp", "name": "C#", "pygments_lexer": "csharp", "version": "8.0" } }, "nbformat": 4, "nbformat_minor": 4 }