[{"data":1,"prerenderedAt":-1},["ShallowReactive",2],{"academy-blogs-en-1-1-all-rust-ep6-control-flow-if-else-loop-all--*":3,"academy-blog-translations-kc8u98bzglgzyfm":85},{"data":4,"page":84,"perPage":84,"totalItems":84,"totalPages":84},[5],{"alt":6,"collectionId":7,"collectionName":8,"content":9,"cover_image":10,"cover_image_path":11,"created":12,"created_by":13,"expand":14,"id":78,"keywords":79,"locale":54,"published_at":13,"scheduled_at":13,"school_blog":76,"short_description":80,"slug":81,"status":74,"title":82,"updated":83,"updated_by":13,"views":77},"Cover image for Rust The Series EP.6 tutorial focusing on Control Flow, if-else statements, and loops (loop, while, for).","sclblg987654321","school_blog_translations","\u003Cp>Welcome back to \u003Cstrong>Rust The Series\u003C\u002Fstrong>! After learning about variables and data types, in this EP.6, we will look at how to tell the program to \"think\" and \"decide\" what to do next in different situations using what we call \u003Cstrong>Control Flow\u003C\u002Fstrong>. In Rust, controlling the program's flow is divided into two main groups: \u003Cstrong>Conditions (\u003Ccode>if-else\u003C\u002Fcode>)\u003C\u002Fstrong> and \u003Cstrong>Repetitions (Loops)\u003C\u002Fstrong>. Let's look at them one by one.\u003C\u002Fp>\u003Ch2>1. Making Decisions with \u003Ccode>if\u003C\u002Fcode> and \u003Ccode>else\u003C\u002Fcode>\u003C\u002Fh2>\u003Cp>Using \u003Ccode>if-else\u003C\u002Fcode> in Rust looks similar to other programming languages, but with a distinct feature: you don't need parentheses \u003Ccode>()\u003C\u002Fcode> around the condition, but curly braces \u003Ccode>{}\u003C\u002Fcode> are strictly required, even if there is only a single line of code.\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let number = 7;\n\n    if number &lt; 5 {\n        println!(\"The number is less than 5\");\n    } else if number == 7 {\n        println!(\"The number is exactly 7!\");\n    } else {\n        println!(\"The number is greater than 5\");\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>🔥 \u003Cstrong>Rust Trick:\u003C\u002Fstrong> In Rust, \u003Ccode>if\u003C\u002Fcode> is an \u003Cstrong>Expression\u003C\u002Fstrong>. This means we can use \u003Ccode>if\u003C\u002Fcode> to assign a value directly to a variable! (Similar to the Ternary Operator \u003Ccode>? :\u003C\u002Fcode> in other languages).\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let condition = true;\n    \u002F\u002F Assign a value to `number` based on the `condition`\n    let number = if condition { 5 } else { 6 };\n    \n    println!(\"The value of number is: {}\", number); \u002F\u002F Output: 5\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003Cstrong>Caution:\u003C\u002Fstrong> The values returned from each block (\u003Ccode>if\u003C\u002Fcode> and \u003Ccode>else\u003C\u002Fcode>) must be of the exact same Data Type.\u003C\u002Fp>\u003Ch2>2. Infinite Looping with \u003Ccode>loop\u003C\u002Fcode>\u003C\u002Fh2>\u003Cp>The \u003Ccode>loop\u003C\u002Fcode> keyword is used to execute a block of code continuously and infinitely until you explicitly tell it to stop using the \u003Ccode>break\u003C\u002Fcode> command (you can also skip the current iteration and move to the next one using the \u003Ccode>continue\u003C\u002Fcode> command).\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut counter = 0;\n\n    loop {\n        counter += 1;\n        \n        if counter == 3 {\n            println!(\"Skipping iteration 3\");\n            continue; \u002F\u002F Skip immediately to the next iteration\n        }\n\n        println!(\"Iteration {}\", counter);\n\n        if counter == 5 {\n            println!(\"That's enough, stop!\");\n            break; \u002F\u002F Exit the loop immediately\n        }\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>Furthermore, we can use \u003Ccode>break\u003C\u002Fcode> to \u003Cstrong>return a value from a \u003Ccode>loop\u003C\u002Fcode>\u003C\u002Fstrong>. This is incredibly useful when you need to iterate to calculate or find a specific value. \u003Cem>(Note: Beginners often miss this. Notice that at the end of the loop block \u003Ccode>};\u003C\u002Fcode>, there must always be a closing semicolon \u003Ccode>;\u003C\u002Fcode> because, in this context, it is a \u003Ccode>let\u003C\u002Fcode> variable declaration statement.)\u003C\u002Fem>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut counter = 0;\n\n    let result = loop {\n        counter += 1;\n        if counter == 10 {\n            break counter * 2; \u002F\u002F Stop the loop and return counter * 2 as an expression\n        }\n    }; \u002F\u002F &lt;--- Don't forget the closing semicolon here!\n\n    println!(\"The result is {}\", result); \u002F\u002F Output: 20\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>🔥 \u003Cstrong>Rust Trick: Loop Labels\u003C\u002Fstrong> When writing nested loops and you want to \u003Ccode>break\u003C\u002Fcode> or \u003Ccode>continue\u003C\u002Fcode> pointing specifically to the \"outermost loop,\" Rust allows you to label your loops! You do this by using a single quote \u003Ccode>'\u003C\u002Fcode> before the label name. For example:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut count = 0;\n\n    'outer: loop { \u002F\u002F Name this loop as 'outer\n        println!(\"Starting outer loop, iteration {}\", count);\n        let mut inner_count = 2;\n\n        loop {\n            println!(\"  Inner loop inner_count = {}\", inner_count);\n            if inner_count == 1 {\n                break; \u002F\u002F Normal break, stops only the \"inner loop\"\n            }\n            if count == 2 {\n                break 'outer; \u002F\u002F Breaks entirely out of the 'outer loop immediately!\n            }\n            inner_count -= 1;\n        }\n        count += 1;\n    }\n    \n    println!(\"Escaped from all loops! (count = {})\", count);\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Ch2>3. Conditional Looping with \u003Ccode>while\u003C\u002Fcode>\u003C\u002Fh2>\u003Cp>If we want to loop by checking a condition first every time, \u003Ccode>while\u003C\u002Fcode> is the right answer. The loop will execute as long as the condition remains \u003Ccode>true\u003C\u002Fcode> and will stop immediately when the condition becomes \u003Ccode>false\u003C\u002Fcode>.\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let mut number = 3;\n\n    while number != 0 {\n        println!(\"{}!\", number);\n        number -= 1; \u002F\u002F Decrease the value of number by 1\n    }\n\n    println!(\"LIFTOFF!!!\");\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>🔥 \u003Cstrong>Rust Trick: \u003Ccode>while\u003C\u002Fcode> cannot return values like \u003Ccode>loop\u003C\u002Fcode>!\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>In the previous section, we saw the cool ability of \u003Ccode>loop\u003C\u002Fcode> to use \u003Ccode>break\u003C\u002Fcode> along with returning a value. But for \u003Ccode>while\u003C\u002Fcode>, we cannot do that. The reason is that a \u003Ccode>while\u003C\u002Fcode> loop has a chance of \"not executing even a single time\" (if the condition is false from the start). Rust, which prioritizes safety, does not allow extracting a value from a \u003Ccode>while\u003C\u002Fcode> loop (it will always return an empty value called the Unit Type \u003Ccode>()\u003C\u002Fcode>).\u003C\u002Fp>\u003Cblockquote>\u003Cp>👉 \u003Cstrong>So, a simple rule to remember:\u003C\u002Fstrong> If you just need to loop to check a condition, use \u003Ccode>while\u003C\u002Fcode>. But if you need to loop to \"find and return a value,\" use \u003Ccode>loop\u003C\u002Fcode>.\u003C\u002Fp>\u003C\u002Fblockquote>\u003Ch2>4. Looping Through Collections with \u003Ccode>for\u003C\u002Fcode>\u003C\u002Fh2>\u003Cp>The \u003Ccode>for\u003C\u002Fcode> loop is arguably the hero of looping in Rust because it's easy to use, safe, and reduces the chance of bugs (like accessing a non-existent index, or out-of-bounds errors). It is often used with Arrays, Vectors, or when creating a Range of numbers.\u003C\u002Fp>\u003Cp>\u003Cstrong>Using \u003Ccode>for\u003C\u002Fcode> with an Array:\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    let a = [10, 20, 30, 40, 50];\n\n    for element in a {\n        println!(\"The value is: {}\", element);\n    }\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003Cstrong>Using \u003Ccode>for\u003C\u002Fcode> with a Range (Number sequence):\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    \u002F\u002F 1..4 means 1 to 3 (excluding 4)\n    \u002F\u002F .rev() is used to reverse the sequence (counting backwards)\n    for number in (1..4).rev() {\n        println!(\"{}!\", number);\n    }\n    println!(\"LIFTOFF!!!\");\n}\n\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>🔥 \u003Cstrong>Rust Trick: Include the last number with an Inclusive Range (\u003Ccode>..=\u003C\u002Fcode>)\u003C\u002Fstrong>\u003C\u002Fp>\u003Cp>Sometimes using \u003Ccode>1..4\u003C\u002Fcode> can be confusing as to which number it ends on. Rust has another syntax, \u003Ccode>..=\u003C\u002Fcode> (adding an equal sign), which means \"include the last number too\". For example:\u003C\u002Fp>\u003Cp>Rust\u003C\u002Fp>\u003Cpre>\u003Ccode>fn main() {\n    \u002F\u002F 1..=3 means 1 to 3 (including 3!). Much easier to read.\n    for number in (1..=3).rev() {\n        println!(\"{}!\", number);\n    }\n}\u003C\u002Fcode>\u003C\u002Fpre>\u003Cp>\u003C\u002Fp>\u003Cdiv data-type=\"horizontalRule\">\u003Chr>\u003C\u002Fdiv>\u003Ch2>Summary\u003C\u002Fh2>\u003Cp>Control Flow in Rust is designed to be concise and highly safe. Having an accurate understanding of how \u003Ccode>if-else\u003C\u002Fcode>, \u003Ccode>loop\u003C\u002Fcode>, \u003Ccode>while\u003C\u002Fcode>, and \u003Ccode>for\u003C\u002Fcode> work will help you write your program's logic smoothly.\u003C\u002Fp>\u003Ch3>📌 In the next EP (EP.7):\u003C\u002Fh3>\u003Cp>We will step into the \"core\" that makes Rust completely different and the most powerful language: \u003Cstrong>Ownership\u003C\u002Fstrong>. It is the system used to manage memory without relying on a Garbage Collector and without slowing down the program! Get ready, and see you in the next article. 🦀\u003C\u002Fp>\u003Ch3>🎯 Stay Tuned with Superdev Academy:\u003C\u002Fh3>\u003Cp>Don't miss out on deep-dive technical articles and updates. Follow us on all platforms:\u003C\u002Fp>\u003Cul>\u003Cli>\u003Cp>🔵 \u003Cstrong>Facebook:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.facebook.com\u002Fsuperdev.academy.th\">Superdev Academy Thailand\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🎬 \u003Cstrong>YouTube:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.youtube.com\u002F@SuperdevAcademy\">Superdev Academy Channel\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>📸 \u003Cstrong>Instagram:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.instagram.com\u002Fsuperdevacademy\u002F\">@superdevacademy\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🎬 \u003Cstrong>TikTok:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener\" class=\"ng-star-inserted\" href=\"https:\u002F\u002Fwww.tiktok.com\u002F@superdevacademy?lang=th-TH\">@superdevacademy\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003Cli>\u003Cp>🌐 \u003Cstrong>Website:\u003C\u002Fstrong> \u003Ca target=\"_blank\" rel=\"noopener noreferrer nofollow\" href=\"http:\u002F\u002Fsuperdevacademy.com\">superdevacademy.com\u003C\u002Fa>\u003C\u002Fp>\u003C\u002Fli>\u003C\u002Ful>\u003Cp>\u003C\u002Fp>","12khckrcaf5i_c6rku00qdw.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclblg987654321\u002F0dggq3lmke6oav9\u002F12khckrcaf5i_c6rku00qdw.png","2026-04-08 10:27:48.005Z","",{"keywords":15,"locale":48,"school_blog":58},[16,23,28,33,38,43],{"collectionId":17,"collectionName":18,"created":19,"created_by":13,"id":20,"name":21,"updated":22,"updated_by":13},"sclkey987654321","school_keywords","2026-03-04 08:20:08.348Z","ux2m77tsy8v19bg","Rust Programming","2026-04-10 16:07:25.602Z",{"collectionId":17,"collectionName":18,"created":24,"created_by":13,"id":25,"name":26,"updated":27,"updated_by":13},"2026-03-23 03:26:43.307Z","pf009wnev852vvt","Rust tutorial","2026-04-10 16:14:45.235Z",{"collectionId":17,"collectionName":18,"created":29,"created_by":13,"id":30,"name":31,"updated":32,"updated_by":13},"2026-03-23 03:08:56.729Z","ttp78s7jayewgl4","Rust","2026-04-10 16:14:44.997Z",{"collectionId":17,"collectionName":18,"created":34,"created_by":13,"id":35,"name":36,"updated":37,"updated_by":13},"2026-04-08 10:27:24.442Z","e35582nfhmo63pj","learn Rust","2026-04-10 16:14:49.283Z",{"collectionId":17,"collectionName":18,"created":39,"created_by":13,"id":40,"name":41,"updated":42,"updated_by":13},"2026-04-08 10:25:37.373Z","uc1966vvgodzu1e","Rust Control Flow","2026-04-10 16:14:48.949Z",{"collectionId":17,"collectionName":18,"created":44,"created_by":13,"id":45,"name":46,"updated":47,"updated_by":13},"2026-04-08 10:27:45.474Z","r1y1o8ob6v734kl","Rust for beginners","2026-04-10 16:14:49.372Z",{"code":49,"collectionId":50,"collectionName":51,"created":52,"flag":53,"id":54,"is_default":55,"label":56,"updated":57},"en","pbc_1989393366","locales","2026-01-22 11:00:02.726Z","twemoji:flag-united-states","qv9c1llfov2d88z",false,"English","2026-04-10 15:42:46.825Z",{"category":59,"collectionId":60,"collectionName":61,"expand":62,"id":76,"views":77},"mfqe235iv6x1or8","pbc_2105096300","school_blogs",{"category":63},{"blogIds":64,"collectionId":65,"collectionName":66,"created":67,"created_by":13,"id":59,"image":68,"image_alt":13,"image_path":69,"label":70,"name":71,"priority":72,"publish_at":73,"scheduled_at":13,"status":74,"updated":75,"updated_by":13},[],"sclcatblg987654321","school_category_blogs","2026-03-16 04:40:07.104Z","47vt5s1gglj_tz0b90vnge.png","https:\u002F\u002Ftwsme-r2.tumwebsme.com\u002Fsclcatblg987654321\u002Fmfqe235iv6x1or8\u002F47vt5s1gglj_tz0b90vnge.png",{"en":71,"th":71},"Rust The Series",2,"2026-03-16 04:40:09.199Z","published","2026-04-25 02:32:15.619Z","kc8u98bzglgzyfm",111,"0dggq3lmke6oav9",[20,25,30,35,40,45],"Learn how to control program logic in Rust using Control Flow. Dive deep into if-else conditions and loops (loop, while, for) with safe coding tricks.","rust-ep6-control-flow-if-else-loop","Mastering Control Flow with if-else and Loops | Rust The Series EP.6","2026-04-20 03:13:22.531Z",1,{"th":81,"en":81}]