From 2bfcae8bc57f0c4b2b6cec0691a9197ce5febcd8 Mon Sep 17 00:00:00 2001 From: nagrat Date: Wed, 15 Mar 2023 19:11:38 +0000 Subject: [PATCH] Update. --- Loops/do_while.cpp | 0 Loops/for.cpp | 32 ++++++++++++++++++++++++++++++++ Loops/while.cpp | 22 ++++++++++++++++++++++ README.md | 7 +++++++ 4 files changed, 61 insertions(+) create mode 100644 Loops/do_while.cpp create mode 100644 Loops/for.cpp create mode 100644 Loops/while.cpp diff --git a/Loops/do_while.cpp b/Loops/do_while.cpp new file mode 100644 index 0000000..e69de29 diff --git a/Loops/for.cpp b/Loops/for.cpp new file mode 100644 index 0000000..7a4f701 --- /dev/null +++ b/Loops/for.cpp @@ -0,0 +1,32 @@ +#include + +#ifdef _WIN32 // if os is windows +#include "Windows.h" // include Windows.h for sleep method +#endif // end the if + +#ifdef linux // if os is linux +#include "unistd.h" // include unistd.h for sleep method +#endif // end the if + +#define sleep_time 100 // define macro to sleep for 500 milliseconds (1/2 a second) + +using namespace std; + +/* + For loops. Iteration over a certain amount of times / elements. + They are used for a lot of shit, can be used for bad stuff too. +*/ + +void for_loop(int max) { + for (int x = 1; x <= max; x++) { // defining new int called x. stop when x is less than or equal to int max. increment x by 1 + cout << x << " "; // print out each number with a space "1 2 3 etc" + Sleep(sleep_time); // making use of a define variable at line 11 + } + cout << "\n"; // new line if you don't want the next lines of code on same line +} + +int main() { + for_loop(10); // calling the for loop and setting the max to be 10 in this example + for_loop(100); // calling the for loop and setting the max to now being 100! + return 0; +} \ No newline at end of file diff --git a/Loops/while.cpp b/Loops/while.cpp new file mode 100644 index 0000000..b234ce8 --- /dev/null +++ b/Loops/while.cpp @@ -0,0 +1,22 @@ +#include + +using namespace std; + +/* + While loops. Can also be known as a forever loop. + Breaks only when a condition is met; usually a boolean. +*/ + +void while_loop(int max) { + int y = 0; + while (y < max) { + cout << y << " "; + y++; + } +} + +int main() { + while_loop(10); + + return 0; +} \ No newline at end of file diff --git a/README.md b/README.md index 9aacade..300ac63 100644 --- a/README.md +++ b/README.md @@ -9,9 +9,16 @@ This is just how I have files and the [Makefile](makefile_tutorial.md) because I - Variables - [Variables](Variables/variables.cpp) - [Macros](Variables/macros.cpp) + - [Macros2](Variables/macros2.cpp) - [Pointers](Variables/pointers.cpp) - [Shared Pointers](Variables/shared_pointers.cpp) - [Smart Pointers](Variables/smart_pointers.cpp) + - [Vectors](Variables/vectors.cpp) + - [Arrays](Variables/arrays.cpp) +- Loops + - [For](Loops/for.cpp) + - [While](Loops/while.cpp) + - [Do While](Loops/do_while.cpp) - Functions - [Functions](Functions/functions.cpp) - [Templates](Functions/templates.cpp)