diff --git a/common-programming-concepts/src/control_flow.rs b/common-programming-concepts/src/control_flow.rs new file mode 100644 index 0000000..0afea31 --- /dev/null +++ b/common-programming-concepts/src/control_flow.rs @@ -0,0 +1,103 @@ +pub fn run() { + println!("\n=== 2.5 control flow ===\n"); + + // The most commonly used control flows in rust are IF, expressions and loop + // Here is an example of a really simple if control flow + + let number = 3; + let number2 = 6; + + if number < 5 { + println!("Condition was true"); + } else { + println!("Condition was false"); + } + + if number != 0 { + println!("The number was not 0"); + } + + // handling multiple conditions with if + if number2 % 4 == 0 { + println!("The number is divisible by 4"); + } else if number2 % 3 == 0 { + println!("The number is divisible by 3"); + } else if number2 % 2 == 0 { + println!("The number is divisible by 2"); + } else { + println!("The number is not divisible by 4, 3, or 2."); + } + + // You can also use if in a let statements, as such + let condition = true; + let number = if condition {5} else {6}; + println!("The value of the conditional number is {number}"); + + // repitition with loops + // An infitie loop looks like this + // loop { + // println!("again!"); + // } + + // next up is a loop which breaks when a certain condition is met + let mut counter = 0; + + let result = loop { + counter += 1; + + if counter == 10 { + break counter * 2; + } + }; + println!("the loop counter is at {result}"); + + // when using nested loops, you can give a label to where to go to if you encounter a break I + // guess. Let's go slowly through this + + 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}"); + + // streamlining conditional loops with while + // Here rust uses the while loop, so this is typically something like while this is true, then + // loop. When this is not true any more, then break the loop. Such as this example: + let mut while_number = 3; + + while while_number != 0 { + println!("{while_number}"); + while_number -= 1; + } + println!("While loop number LIFTOFF!!!"); + + // Looping through a collection with a for loop + // you could use a while loop for this, but it is error prone, and you would typically8 reach + // for the for loop at this point. So we range through an array like this + let for_array = [10, 20, 30, 40, 50]; + + for item in for_array { + println!("the value is {item}"); + } + + // So actually, in rust the for loop is almost the only kind of loop that rustecians would use. + // Also in the while loop example, consider this: + for number_for in (1..4).rev() { + println!("{number_for}"); + } + println!("for loop LIFTOFF!!!"); + +} diff --git a/common-programming-concepts/src/main.rs b/common-programming-concepts/src/main.rs index 28bdf15..db500ee 100644 --- a/common-programming-concepts/src/main.rs +++ b/common-programming-concepts/src/main.rs @@ -1,6 +1,7 @@ mod variables; mod data_types; mod functions; +mod control_flow; fn main() { variables::run(); @@ -8,5 +9,6 @@ fn main() { data_types::run(); println!("\n#####################\n"); functions::run(); - + println!("\n#####################\n"); + control_flow::run(); }