{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Control flow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Statements & Expressions\n", "\n", "### Statements\n", "\n", "* Statements must end with a semicolon" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "y = 30\n" ] } ], "source": [ "let x = 10;\n", "let mut y = 20;\n", "y += x;\n", "println!(\"y = {y}\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Expressions\n", "\n", "* Rust is an **expression-oriented language**, which means that most pieces of code are in fact expressions\n", "* A code block is also an expression, which returns the value of its last expression if we omit the semicolon" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "result = 30\n" ] } ], "source": [ "let result = {\n", " let a = 10;\n", " let b = 20;\n", " a + b // no semicolon before end of the block\n", "};\n", "\n", "println!(\"result = {}\", result);" ] }, { "cell_type": "code", "execution_count": 5, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "data": { "text/plain": [ "()" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let result = {\n", " let a = 10;\n", " let b = 20;\n", " a + b; // now result of an expression is ()\n", "};\n", "\n", "result" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Conditional Statements with `if` and `else`" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "The number 7 is odd.\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let n = 7;\n", "\n", "if n % 2 == 0 {\n", " println!(\"The number {n} is even.\");\n", "} else {\n", " println!(\"The number {n} is odd.\");\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `if-else` as an expression\n", "\n", "* if else condition is an expression that returns a value\n", "* all branches always must return a value of the same type" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "data": { "text/plain": [ "\"adult\"" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let age = 44;\n", "let is_adult = if age >= 18 { true } else { false };\n", "let description = if is_adult { \"adult\" } else { \"minor\" };\n", "description" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `while` loop" ] }, { "cell_type": "code", "execution_count": 33, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Current count: 0\n", "Current count: 1\n", "Current count: 2\n", "Current count: 3\n", "Current count: 4\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 33, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let mut count = 0;\n", "\n", "while count < 5 {\n", " println!(\"Current count: {}\", count);\n", " count += 1;\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Infinity `loop`\n", "\n", "Use loop to write infinite loops. It executes the block repeatedly forever (or until a break or return is reached, or the thread panics)." ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 * 1 = 1\n", "3 * 3 = 9\n", "5 * 5 = 25\n", "7 * 7 = 49\n", "9 * 9 = 81\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let mut n = 0;\n", "let mut power: u32;\n", "let max_power = 100;\n", "\n", "loop {\n", " n += 1;\n", " power = n * n;\n", "\n", " if power >= max_power {\n", " break; \n", " }\n", "\n", " if power % 2 == 0 {\n", " continue;\n", " }\n", "\n", " println!(\"{} * {} = {}\", n, n, power); \n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Nested loops\n", "\n", "* When loops are nested inside each other, the `break` and `continue` statements apply to the immediate enclosing loop. \n", "* Any loop statement (also the while and for loop) can be preceded by a label (denoted as labelname:) to allow us to jump to the next or outer enclosing loop" ] }, { "cell_type": "code", "execution_count": 43, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Enter the outer loop\n", "Enter the inner loop\n", "Exited the outer loop\n" ] } ], "source": [ "'outer: loop {\n", " println!(\"Enter the outer loop\");\n", " 'inner: loop {\n", " println!(\"Enter the inner loop\");\n", " break 'outer; // breaks the outer loop\n", " }\n", " println!(\"This will never be printed...\");\n", "}\n", "println!(\"Exited the outer loop\");" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## `for` loops" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* looping where variable `i` begins from a start value `a` to an end value `b` (exclusive)\n", "* each subsequent value is bound to the variable `i` and used in the next loop iteration" ] }, { "cell_type": "code", "execution_count": 45, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i = 0\n", "i = 1\n", "i = 2\n", "i = 3\n", "i = 4\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 45, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for i in 0..5 {\n", " println!(\"i = {}\", i);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* inclusive version of `for`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i = 1\n", "i = 2\n", "i = 3\n", "i = 4\n", "i = 5\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 56, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for i in 1..=5 {\n", " println!(\"i = {}\", i);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* if we don't need the value of the variable `i` in the loop, we can replace it with an `_` (underscore):" ] }, { "cell_type": "code", "execution_count": 55, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Do it again!\n", "Do it again!\n", "Do it again!\n", "Do it again!\n", "Do it again!\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 55, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for _ in 0..5 { println!(\"Do it again!\"); } " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "* iteration in reverse using `(a..b).rev()`" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "i: 10\n", "i: 9\n", "i: 8\n", "i: 7\n", "i: 6\n", "i: 5\n", "i: 4\n", "i: 3\n", "i: 2\n", "i: 1\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "for i in (1..=10).rev() {\n", " println!(\"i: {}\", i);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Looping through the collection with `for`" ] }, { "cell_type": "code", "execution_count": 4, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "item: 1\n", "item: 64\n", "item: 42\n", "item: 665\n", "item: -123\n", "item: -2\n", "item: 6\n", "item: 0\n", "item: 57\n", "item: -1\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let data: [i32; 10] = [1, 64, 42, 665, -123, -2, 6, 0, 57, -1];\n", "\n", "for item in data {\n", " println!(\"item: {}\", item);\n", "}" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Example" ] }, { "cell_type": "code", "execution_count": 11, "metadata": { "vscode": { "languageId": "rust" } }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 is prime!\n", "3 is prime!\n", "5 is prime!\n", "7 is prime!\n", "11 is prime!\n", "13 is prime!\n", "17 is prime!\n", "19 is prime!\n", "23 is prime!\n", "29 is prime!\n", "31 is prime!\n", "37 is prime!\n", "41 is prime!\n", "43 is prime!\n", "47 is prime!\n", "53 is prime!\n", "59 is prime!\n", "61 is prime!\n", "67 is prime!\n", "71 is prime!\n", "73 is prime!\n", "79 is prime!\n", "83 is prime!\n", "89 is prime!\n", "97 is prime!\n" ] }, { "data": { "text/plain": [ "()" ] }, "execution_count": 11, "metadata": {}, "output_type": "execute_result" } ], "source": [ "let max_prime = 100;\n", "\n", "for n in 2..=max_prime {\n", " let mut is_prime = true;\n", " for d in 2..=((n as f64).sqrt() as i32) {\n", " if n % d == 0 {\n", " is_prime = false;\n", " break;\n", " }\n", " }\n", "\n", " if is_prime {\n", " println!(\"{} is prime!\", n);\n", " }\n", "}" ] } ], "metadata": { "kernelspec": { "display_name": "Rust", "language": "rust", "name": "rust" }, "language_info": { "codemirror_mode": "rust", "file_extension": ".rs", "mimetype": "text/rust", "name": "Rust", "pygment_lexer": "rust", "version": "" } }, "nbformat": 4, "nbformat_minor": 2 }