From aa9b47d8f1a8b3fac4d80276401e6d0884edb86a Mon Sep 17 00:00:00 2001 From: Oskar Date: Fri, 16 Aug 2024 22:09:58 +0200 Subject: more --- 4p10.cpp | 21 +++++++++++++++++++++ 4p11.cpp | 25 +++++++++++++++++++++++++ 4p12.cpp | 23 +++++++++++++++++++++++ 4p8.cpp | 17 +++++++++++++++++ 4p9.cpp | 38 ++++++++++++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+) create mode 100644 4p10.cpp create mode 100644 4p11.cpp create mode 100644 4p12.cpp create mode 100644 4p8.cpp create mode 100644 4p9.cpp diff --git a/4p10.cpp b/4p10.cpp new file mode 100644 index 0000000..5e18c6b --- /dev/null +++ b/4p10.cpp @@ -0,0 +1,21 @@ +#include + +/* + * + * 4.10 + * + * + */ + +int main () { + + int val; + while(std::cin >> val && val != 42) { + } + + if(val == 42) { + std::cout << "We found 42!" << std::endl; + } + + return 0; +} diff --git a/4p11.cpp b/4p11.cpp new file mode 100644 index 0000000..308ef2c --- /dev/null +++ b/4p11.cpp @@ -0,0 +1,25 @@ +#include + +/* + * + * 4.11 + * + * + */ + +int main () { + + int a = 0; + int b = 0; + int c = 0; + int d = 0; + if(std::cin >> a >> b >> c >> d) {} else { return -1; } + + if(a > b && b > c && c > d) { + std::cout << "True!" << std::endl; + } else { + std::cout << "False!" << std::endl; + } + + return 0; +} diff --git a/4p12.cpp b/4p12.cpp new file mode 100644 index 0000000..74442e4 --- /dev/null +++ b/4p12.cpp @@ -0,0 +1,23 @@ +#include + +/* + * + * 4.12 + * + * + */ + +int main () { + + int i = 1; + int j = 9; + int k = 10; + + if(i != (j < k)) { // j < k is evaluated first + // because j < k now it will return true + // When 'i' is NOT true(aka NOT 0) then print below + std::cout << "..." << std::endl; + } + + return 0; +} diff --git a/4p8.cpp b/4p8.cpp new file mode 100644 index 0000000..46c402a --- /dev/null +++ b/4p8.cpp @@ -0,0 +1,17 @@ +#include + +/* + * + * 4.8 + * + * + */ + +int main () { + + // AND, OR + // AND's right operand is only evaluated if the left operand is True + // OR's right operand is only evaluated if the left operand is False + // Equality evaluates left and right and then checks if they are equal + return 0; +} diff --git a/4p9.cpp b/4p9.cpp new file mode 100644 index 0000000..101f1c9 --- /dev/null +++ b/4p9.cpp @@ -0,0 +1,38 @@ +#include + +/* + * + * 4.9 + * + * + */ + +int main () { + // True if: + // the address pointed to isnt null + // the first character isnt a null character + + // We can test this by making a string literal, for exmple "Hello world" + // We can also test by putting the null character \0 in the string literal + // And we can also test by pointing cp at nullptr + int choice; + const char *hw = "Hello world!"; + const char *np = nullptr; + const char nc = '\0'; + const char *cp; + std::cout << "1: Hello World | 2: nullptr | 3: null character" << std::endl; + if(std::cin >> choice) {} else { return -1; } + + if(choice == 1) { cp = hw; } + else if(choice == 2) { cp = np; } + else if(choice == 3) { cp = &nc; } + else { return -1; } + + if (cp && *cp) { + std::cout << "valid" << std::endl; + } else { + std::cout << "not valid" << std::endl; + } + + return 0; +} -- cgit v1.2.3