Skip to content

Commit

Permalink
new post
Browse files Browse the repository at this point in the history
  • Loading branch information
spsanderson committed Nov 20, 2024
1 parent e6cc8cc commit 3c06722
Show file tree
Hide file tree
Showing 11 changed files with 1,833 additions and 476 deletions.
948 changes: 474 additions & 474 deletions docs/index.html

Large diffs are not rendered by default.

Binary file added docs/posts/2024-11-20/ex1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1,031 changes: 1,031 additions & 0 deletions docs/posts/2024-11-20/index.html

Large diffs are not rendered by default.

Binary file added docs/posts/2024-11-20/todays_post.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
77 changes: 77 additions & 0 deletions docs/search.json
Original file line number Diff line number Diff line change
Expand Up @@ -13214,5 +13214,82 @@
"title": "How to Combine Vectors in R: A Comprehensive Guide with Examples",
"section": "Example 2: Time Series Data",
"text": "Example 2: Time Series Data\nCombining vectors is useful for organizing time series data, where each vector represents a different variable.\n\n# Time series data\ndates <- as.Date(c(\"2024-01-01\", \"2024-01-02\", \"2024-01-03\"))\nvalues1 <- c(100, 105, 110)\nvalues2 <- c(200, 210, 220)\n\n# Create a data frame\nts_data <- data.frame(Date = dates, Series1 = values1, Series2 = values2)\nprint(ts_data) \n\n Date Series1 Series2\n1 2024-01-01 100 200\n2 2024-01-02 105 210\n3 2024-01-03 110 220"
},
{
"objectID": "posts/2024-11-20/index.html",
"href": "posts/2024-11-20/index.html",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "",
"text": "As a beginner C programmer, understanding conditional logic and small change operators is essential for writing efficient and dynamic code. In this in-depth guide, we’ll explore the power of the conditional operator (?:), increment (++), and decrement (–) operators, providing examples and best practices to level up your C programming skills."
},
{
"objectID": "posts/2024-11-20/index.html#table-of-contents",
"href": "posts/2024-11-20/index.html#table-of-contents",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Table of Contents",
"text": "Table of Contents\n\nIntroduction\nThe Conditional Operator\n\nSyntax\nExample\nAdvantages over if…else\n\nThe Increment and Decrement Operators\n\nPrefix vs Postfix\nExample\nEfficiency\n\nSizing Up the Situation with sizeof()\nYour Turn!\nQuick Takeaways\nFAQs\nConclusion\nReferences"
},
{
"objectID": "posts/2024-11-20/index.html#introduction",
"href": "posts/2024-11-20/index.html#introduction",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Introduction",
"text": "Introduction\nC offers a variety of operators that can streamline your code and improve performance. In this article, we’ll focus on three key operators:\n\nThe conditional operator (?:)\nThe increment operator (++)\nThe decrement operator (–)\n\nBy mastering these operators, you’ll be able to write more concise, efficient C programs. Let’s dive in!"
},
{
"objectID": "posts/2024-11-20/index.html#the-conditional-operator",
"href": "posts/2024-11-20/index.html#the-conditional-operator",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "The Conditional Operator",
"text": "The Conditional Operator\nThe conditional operator (?:) is a ternary operator, meaning it takes three arguments. It provides a shorthand way to write simple if…else statements, making your code more readable and compact.\n\nSyntax\nThe syntax for the conditional operator is:\ncondition ? expression1 : expression2\nIf condition evaluates to true (non-zero), expression1 is executed. Otherwise, expression2 is executed.\n\n\nExample\nConsider the following code that determines if a number is even or odd:\nint num = 7;\nchar* result = (num % 2 == 0) ? \"even\" : \"odd\";\nprintf(\"%d is %s\\n\", num, result);\nOutput:\n7 is odd\n\n\nAdvantages over if…else\nThe conditional operator offers several benefits over traditional if…else statements:\n\nConcise syntax: It reduces the amount of code you need to write.\nFewer braces: You don’t need to worry about mismatched or missing braces.\nImproved efficiency: The conditional operator compiles into more compact code, resulting in faster execution.\n\nHowever, for complex conditions or multi-line statements, if…else remains the better choice for readability."
},
{
"objectID": "posts/2024-11-20/index.html#the-increment-and-decrement-operators",
"href": "posts/2024-11-20/index.html#the-increment-and-decrement-operators",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "The Increment and Decrement Operators",
"text": "The Increment and Decrement Operators\nThe increment (++) and decrement (–) operators are unary operators that add or subtract 1 from a variable, respectively. They are commonly used for counting or iterating purposes.\n\nPrefix vs Postfix\nThese operators can be used in prefix or postfix form:\n\nPrefix: ++var or --var\nPostfix: var++ or var--\n\nThe placement of the operator determines when the increment or decrement occurs:\n\nPrefix: The variable is modified before being used in the expression.\nPostfix: The variable is modified after being used in the expression.\n\n\n\nExample\nint i = 5;\nint j = ++i; // j = 6, i = 6\nint k = i++; // k = 6, i = 7\n\n\nEfficiency\nThe ++ and – operators are highly efficient, often compiling into a single machine language instruction. They are preferred over using +1 or -1 for incrementing or decrementing variables."
},
{
"objectID": "posts/2024-11-20/index.html#sizing-up-the-situation-with-sizeof",
"href": "posts/2024-11-20/index.html#sizing-up-the-situation-with-sizeof",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Sizing Up the Situation with sizeof()",
"text": "Sizing Up the Situation with sizeof()\nThe sizeof() operator returns the size, in bytes, of a variable or data type. It’s useful for determining memory usage and portability across different systems.\nint i = 42;\nprintf(\"Size of int: %zu bytes\\n\", sizeof(int));\nprintf(\"Size of i: %zu bytes\\n\", sizeof(i));\nOutput (on a 64-bit system):\nSize of int: 4 bytes\nSize of i: 4 bytes\nNote: The %zu format specifier is used for size_t, the return type of sizeof()."
},
{
"objectID": "posts/2024-11-20/index.html#your-turn",
"href": "posts/2024-11-20/index.html#your-turn",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Your Turn!",
"text": "Your Turn!\nNow it’s time to practice what you’ve learned. Write a program that:\n\nPrompts the user to enter their age.\nUses the conditional operator to determine if they are a minor (age < 18) or an adult.\nPrints the result using the increment operator.\n\n\n\nClick to reveal the solution!\n\n#include <stdio.h>\n\nint main() {\n int age;\n printf(\"Enter your age: \");\n scanf(\"%d\", &age);\n\n char* status = (age < 18) ? \"minor\" : \"adult\";\n printf(\"You are a%s %s.\\n\", (status[0] == 'a') ? \"n\" : \"\", status);\n\n printf(\"In %d year%s, you will be %d.\\n\", 5, (5 == 1) ? \"\" : \"s\", age + 5);\n\n return 0;\n}\n\n\n\nExample in my Console"
},
{
"objectID": "posts/2024-11-20/index.html#quick-takeaways",
"href": "posts/2024-11-20/index.html#quick-takeaways",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Quick Takeaways",
"text": "Quick Takeaways\n\nThe conditional operator (?:) is a concise alternative to simple if…else statements.\nThe increment (++) and decrement (–) operators efficiently add or subtract 1 from a variable.\nPrefix and postfix forms of ++ and – determine when the modification occurs in an expression.\nThe sizeof() operator returns the size of a variable or data type in bytes."
},
{
"objectID": "posts/2024-11-20/index.html#faqs",
"href": "posts/2024-11-20/index.html#faqs",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "FAQs",
"text": "FAQs\n\nQ: Can the conditional operator be nested? A: Yes, you can nest conditional operators for more complex conditions, but it can reduce readability.\nQ: Is it possible to increment or decrement a constant? A: No, the ++ and – operators can only be used with variables, not constants or expressions.\nQ: Does sizeof() include the null terminator for strings? A: Yes, sizeof() includes the null terminator when used on character arrays (strings)."
},
{
"objectID": "posts/2024-11-20/index.html#conclusion",
"href": "posts/2024-11-20/index.html#conclusion",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "Conclusion",
"text": "Conclusion\nCongratulations on taking your C programming skills to the next level! By understanding and applying the conditional, increment, and decrement operators, you can write more efficient and expressive code. Remember to prioritize readability and use these operators judiciously. Keep practicing, and happy coding!"
},
{
"objectID": "posts/2024-11-20/index.html#references",
"href": "posts/2024-11-20/index.html#references",
"title": "Mastering Conditional Logic and Small Change Operators in C",
"section": "References",
"text": "References\n\nC Programming Exercises: Conditional Statement. W3Resource.\nC++ Operators. W3Schools.\nC++ Programming Language. GeeksforGeeks.\n\n\nHappy Coding! 🚀\n\n\n\nC Programming ++ –\n\n\n\nYou can connect with me at any one of the below:\nTelegram Channel here: https://t.me/steveondata\nLinkedIn Network here: https://www.linkedin.com/in/spsanderson/\nMastadon Social here: https://mstdn.social/@stevensanderson\nRStats Network here: https://rstats.me/@spsanderson\nGitHub Network here: https://github.com/spsanderson\nBluesky Network here: https://bsky.app/profile/spsanderson.com"
}
]
8 changes: 6 additions & 2 deletions docs/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -910,7 +910,7 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/index.html</loc>
<lastmod>2023-03-28T12:23:03.885Z</lastmod>
<lastmod>2022-11-16T15:17:41.340Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/about.html</loc>
Expand Down Expand Up @@ -1902,6 +1902,10 @@
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2024-11-19/index.html</loc>
<lastmod>2024-11-19T13:00:41.976Z</lastmod>
<lastmod>2024-11-20T02:28:12.988Z</lastmod>
</url>
<url>
<loc>https://www.spsanderson.com/steveondata/posts/2024-11-20/index.html</loc>
<lastmod>2024-11-20T02:48:11.264Z</lastmod>
</url>
</urlset>
Binary file added posts/2024-11-20/ex1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
226 changes: 226 additions & 0 deletions posts/2024-11-20/index.qmd
Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
---
title: "Mastering Conditional Logic and Small Change Operators in C "
author: "Steven P. Sanderson II, MPH"
date: "2024-11-20"
categories: [code, c]
toc: TRUE
description: "Unlock the power of C's conditional operator, increment/decrement operators, and sizeof() to write more efficient and expressive code. Explore practical examples and best practices for beginner C programmers."
keywords: [Programming, C conditional operator, C increment operator, C decrement operator, C sizeof operator, C operators for beginners, Ternary operator in C, Prefix vs postfix increment in C, Prefix vs postfix decrement in C, Memory size of data types in C, Efficient alternatives to if...else in C, How to use the conditional operator in C programming, Difference between ++i and i++ in C, Difference between --i and i-- in C, Finding the size of variables and arrays in C using sizeof, Best practices for using increment and decrement operators in]
draft: TRUE
---

As a beginner C programmer, understanding conditional logic and small change operators is essential for writing efficient and dynamic code. In this in-depth guide, we'll explore the power of the conditional operator (?:), increment (++), and decrement (--) operators, providing examples and best practices to level up your C programming skills.

## Table of Contents
- [Introduction](#introduction)
- [The Conditional Operator](#the-conditional-operator)
- [Syntax](#syntax)
- [Example](#example)
- [Advantages over if...else](#advantages-over-ifelse)
- [The Increment and Decrement Operators](#the-increment-and-decrement-operators)
- [Prefix vs Postfix](#prefix-vs-postfix)
- [Example](#example-1)
- [Efficiency](#efficiency)
- [Sizing Up the Situation with sizeof()](#sizing-up-the-situation-with-sizeof)
- [Your Turn!](#your-turn)
- [Quick Takeaways](#quick-takeaways)
- [FAQs](#faqs)
- [Conclusion](#conclusion)
- [References](#references)

## Introduction

C offers a variety of operators that can streamline your code and improve performance. In this article, we'll focus on three key operators:

1. The conditional operator (?:)
2. The increment operator (++)
3. The decrement operator (--)

By mastering these operators, you'll be able to write more concise, efficient C programs. Let's dive in!

## The Conditional Operator

The conditional operator (?:) is a ternary operator, meaning it takes three arguments. It provides a shorthand way to write simple if...else statements, making your code more readable and compact.

### Syntax

The syntax for the conditional operator is:

```c
condition ? expression1 : expression2
```

If `condition` evaluates to true (non-zero), `expression1` is executed. Otherwise, `expression2` is executed.

### Example

Consider the following code that determines if a number is even or odd:

```c
int num = 7;
char* result = (num % 2 == 0) ? "even" : "odd";
printf("%d is %s\n", num, result);
```
Output:
```
7 is odd
```
### Advantages over if...else
The conditional operator offers several benefits over traditional if...else statements:
1. **Concise syntax**: It reduces the amount of code you need to write.
2. **Fewer braces**: You don't need to worry about mismatched or missing braces.
3. **Improved efficiency**: The conditional operator compiles into more compact code, resulting in faster execution.
However, for complex conditions or multi-line statements, if...else remains the better choice for readability.
## The Increment and Decrement Operators
The increment (++) and decrement (--) operators are unary operators that add or subtract 1 from a variable, respectively. They are commonly used for counting or iterating purposes.
### Prefix vs Postfix
These operators can be used in prefix or postfix form:
- **Prefix**: `++var` or `--var`
- **Postfix**: `var++` or `var--`
The placement of the operator determines when the increment or decrement occurs:
- **Prefix**: The variable is modified before being used in the expression.
- **Postfix**: The variable is modified after being used in the expression.
### Example
```c
int i = 5;
int j = ++i; // j = 6, i = 6
int k = i++; // k = 6, i = 7
```

### Efficiency

The ++ and -- operators are highly efficient, often compiling into a single machine language instruction. They are preferred over using +1 or -1 for incrementing or decrementing variables.

## Sizing Up the Situation with sizeof()

The `sizeof()` operator returns the size, in bytes, of a variable or data type. It's useful for determining memory usage and portability across different systems.

```c
int i = 42;
printf("Size of int: %zu bytes\n", sizeof(int));
printf("Size of i: %zu bytes\n", sizeof(i));
```
Output (on a 64-bit system):
```
Size of int: 4 bytes
Size of i: 4 bytes
```
Note: The `%zu` format specifier is used for `size_t`, the return type of `sizeof()`.
## Your Turn!
Now it's time to practice what you've learned. Write a program that:
1. Prompts the user to enter their age.
2. Uses the conditional operator to determine if they are a minor (age < 18) or an adult.
3. Prints the result using the increment operator.
<details>
<summary>Click to reveal the solution!</summary>
```c
#include <stdio.h>
int main() {
int age;
printf("Enter your age: ");
scanf("%d", &age);
char* status = (age < 18) ? "minor" : "adult";
printf("You are a%s %s.\n", (status[0] == 'a') ? "n" : "", status);
printf("In %d year%s, you will be %d.\n", 5, (5 == 1) ? "" : "s", age + 5);
return 0;
}
```
![Example in my Console](ex1.png)

</details>

## Quick Takeaways

- The conditional operator (?:) is a concise alternative to simple if...else statements.
- The increment (++) and decrement (--) operators efficiently add or subtract 1 from a variable.
- Prefix and postfix forms of ++ and -- determine when the modification occurs in an expression.
- The `sizeof()` operator returns the size of a variable or data type in bytes.

## FAQs

1. **Q: Can the conditional operator be nested?**
A: Yes, you can nest conditional operators for more complex conditions, but it can reduce readability.

2. **Q: Is it possible to increment or decrement a constant?**
A: No, the ++ and -- operators can only be used with variables, not constants or expressions.

3. **Q: Does `sizeof()` include the null terminator for strings?**
A: Yes, `sizeof()` includes the null terminator when used on character arrays (strings).

## Conclusion

Congratulations on taking your C programming skills to the next level! By understanding and applying the conditional, increment, and decrement operators, you can write more efficient and expressive code. Remember to prioritize readability and use these operators judiciously. Keep practicing, and happy coding!

## References

1. [C Programming Exercises: Conditional Statement. W3Resource.](https://www.w3resource.com/c-programming-exercises/conditional-statement/index.php)
2. [C++ Operators. W3Schools.](https://www.w3schools.com/cpp/cpp_operators_logical.asp)
3. [C++ Programming Language. GeeksforGeeks.](https://www.geeksforgeeks.org/c-plus-plus/)

------------------------------------------------------------------------

Happy Coding! 🚀

![C Programming ++ --](todays_post.png)

------------------------------------------------------------------------

*You can connect with me at any one of the below*:

*Telegram Channel here*: <https://t.me/steveondata>

*LinkedIn Network here*: <https://www.linkedin.com/in/spsanderson/>

*Mastadon Social here*: [https://mstdn.social/\@stevensanderson](https://mstdn.social/@stevensanderson)

*RStats Network here*: [https://rstats.me/\@spsanderson](https://rstats.me/@spsanderson)

*GitHub Network here*: <https://github.com/spsanderson>

*Bluesky Network here*: <https://bsky.app/profile/spsanderson.com>

------------------------------------------------------------------------

```{=html}
<script src="https://giscus.app/client.js"
data-repo="spsanderson/steveondata"
data-repo-id="R_kgDOIIxnLw"
data-category="Comments"
data-category-id="DIC_kwDOIIxnL84ChTk8"
data-mapping="url"
data-strict="0"
data-reactions-enabled="1"
data-emit-metadata="0"
data-input-position="top"
data-theme="dark"
data-lang="en"
data-loading="lazy"
crossorigin="anonymous"
async>
</script>
```
Binary file added posts/2024-11-20/todays_post.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions site_libs/bootstrap/bootstrap.min.css

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions site_libs/bootstrap/bootstrap.min.js

Large diffs are not rendered by default.

0 comments on commit 3c06722

Please sign in to comment.