diff options
author | Jordan Gong <jordan.gong@protonmail.com> | 2020-08-10 21:57:04 +0800 |
---|---|---|
committer | Jordan Gong <jordan.gong@protonmail.com> | 2020-08-10 21:57:04 +0800 |
commit | 4385163bf4809b27cf2576ad5cc379571d4264cf (patch) | |
tree | 1d649010f12c08c5136f56a60c84cdee3394a92d /control-flow/src | |
parent | 2dea1b048e0637346956b979f902dca88c48b616 (diff) |
Control the flow
Diffstat (limited to 'control-flow/src')
-rw-r--r-- | control-flow/src/main.rs | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/control-flow/src/main.rs b/control-flow/src/main.rs new file mode 100644 index 0000000..d3200f0 --- /dev/null +++ b/control-flow/src/main.rs @@ -0,0 +1,63 @@ +fn main() { + /* if */ + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } + + let number = 6; + + if number % 4 == 0 { + println!("number is divisible by 4"); + } else if number % 3 == 0 { + println!("number is divisible by 3"); + } else if number % 2 == 0 { + println!("number is divisible by 2"); + } else { + println!("number is not divisible by 4, 3 or 2"); + } + + let condition = true; + let number = if condition { 5 } else { 6 }; + + println!("The value of number is: {}", number); + + /* loop */ + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is: {}", result); + + /* while */ + let mut number = 3; + + while number != 0 { + println!("{}!", number); + + number -= 1; + } + + println!("LIFTOFF!!!"); + + /* for */ + let a = [10, 20, 30, 40, 50]; + + for element in a.iter() { + println!("the value is: {}", element); + } + + for number in (1..4).rev() { + println!("{}!", number); + } + println!("LIFTOFF!!!"); +} |