diff --git a/branches/Cargo.toml b/branches/Cargo.toml new file mode 100644 index 0000000..6934aa4 --- /dev/null +++ b/branches/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "branches" +version = "0.1.0" +edition = "2021" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/branches/src/main.rs b/branches/src/main.rs new file mode 100644 index 0000000..a00f52f --- /dev/null +++ b/branches/src/main.rs @@ -0,0 +1,101 @@ +fn main() { + if_expressions(); + if_expressions_multiple_branches(); + if_expressions_one_line(); + loop_infinitely(); + loop_with_return_value(); + while_loop(); + for_loop(); +} + +fn if_expressions() { + let number = 3; + + if number < 5 { + println!("condition was true"); + } else { + println!("condition was false"); + } +} + +fn if_expressions_multiple_branches() { + 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"); + } +} + +fn if_expressions_one_line() { + let condition = true; + let number = if condition { 5 } else { 6 }; + + println!("The value of number is: {}", number); +} + +fn loop_infinitely() { + let mut count = 0; + 'counting_up: loop { + println!("count = {}", count); + let mut remaining = 10; + + loop { + println!("remaining = {}", remaining); + if remaining == 9 { + break; + } + if count == 2 { + break 'counting_up; + } + remaining -= 1; + } + + count += 1; + } + println!("End count = {}", count); +} + +fn loop_with_return_value() { + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + + println!("The result is {}", result); +} + +fn while_loop() { + let mut number = 3; + + while number != 0 { + println!("{}!", number); + number -= 1; + } + + println!("LIFTOFF!!!"); +} + +fn for_loop() { + let a = 1..5; + + for element in a { + println!("The value is -> {}", element); + } + + for number in (1..4).rev() { + println!("{}!", number); + } + + println!("LIFTOFF!!!"); +} \ No newline at end of file