Understanding Variables, Conditionals, Loops, and Arrays In C Programming
Tags: programming, beginners, tutorial, codenewbie
Published: 2023-07-16 14:51:59

[![GitHub Logo](https://res.cloudinary.com/bizstak/image/upload/v1685060686/github_f9ljwi.svg)](https://github.com/techstackmedia/software-engineering-series/tree/17-understanding-variables-conditionals-loop-and-arrays-in-c)

## Introduction

In this blog post, we'll go through the provided code in C step by step and explain each part in detail. This will help beginners understand the code and its logic. Let's get started!

## Code Snippet

{% embed https://gist.github.com/techstackmedia/5d16dcfe1cc0a94dd5f69a80196457b6 %}

## Understanding Variables and Constants

```c
const int firstNumber = 30;
const int secondNumber = 50;
const int day = 2;
const int myNumbers[] = { 4, 6, 9, 20, 5, 10 };
const char* myLanguages[] = { "C", "C++", "Java", "C#", "PHP", "Python", "Ruby", "JavaScript", "Rust", "Go", "Swift", "R", "Kotlin" };
const int numLanguages = sizeof(myLanguages) / sizeof(myLanguages[0]);
int counter = 1;
int counterIndex = 1;
int index = 0;
int i = 0;
```

In the above code snippet, we declare and initialize several variables and constants. Here's a breakdown:

- `firstNumber` and `secondNumber` are constants representing integer values.
- `day` represents the current day as an integer.
- `myNumbers` is an array of integers that stores a collection of numbers.
- `myLanguages` is an array of strings (character pointers) that stores a collection of programming language names.
- `numLanguages` is an integer that represents the number of languages in the `myLanguages` array.
- `counter`, `counterIndex`, `index`, and `i` are integer variables used for looping and counting purposes.

## Comparing Numbers using Conditionals

```c
const int firstNumber = 30;
const int secondNumber = 50;

if (firstNumber > secondNumber) {
  printf("%d is greater than secondNumber %d\n", firstNumber, secondNumber);
} else if (firstNumber < secondNumber) {
  printf("%d is less than secondNumber %d\n", firstNumber, secondNumber);
} else {
  printf("%d is equivalent to secondNumber %d\n", firstNumber, secondNumber);
}

(firstNumber > secondNumber) ? printf("%d is greater than secondNumber %d\n", firstNumber, secondNumber) : 
(firstNumber < secondNumber) ? printf("%d is less than secondNumber %d\n", firstNumber, secondNumber) :
printf("%d is equivalent to secondNumber %d\n", firstNumber, secondNumber);

/*
30 is less than secondNumber 50
30 is less than secondNumber 50
*/
```

These code blocks demonstrate how to compare two numbers (`firstNumber` and `secondNumber`) using conditional statements (`if-else` and the ternary operator `? :`). The code checks if `firstNumber` is greater than `secondNumber`, less than `secondNumber`, or equivalent to `secondNumber`. The appropriate message is then printed based on the comparison.

## Using a Switch Statement

```c
const int day = 2;

switch (day){ 
  case 1:
    printf("Monday\n");
    break;
  case 2:
    printf("Tuesday\n");
    break;
  case 3:
    printf("Wednesday\n");
    break;
  default:
    printf("Sunday\n");
}

/*
Tuesday
*/
```

This switch statement checks the value of the `day` variable and executes the corresponding code block. If `day` is equal to 1, "Monday" is printed. If it is 2, "Tuesday" is printed. If it is 3, "Wednesday" is printed. If none of these cases match, the `default` case is executed, and "Sunday" is printed.

## Looping with While and Do-While

```c
while (counter < 3) {
  printf("%d\n", counter);
  counter++;
}

printf("-----\n");

do {
  printf("%d\n", counterIndex);
  counterIndex++;
} while (counterIndex < 3);

/*
1
2
-----
1
2
*/
```

These code blocks demonstrate the usage of `while` and `do-while` loops. In the first block, the `while` loop executes as long as `counter` is less than 3. It prints the value of `counter` and increments it by 1 in each iteration. In the second block, the `do-while` loop executes at least once and continues until `counterIndex` is less than 3. It prints the value of `counterIndex` and increments it by 1 in each iteration.

## Looping with For

```c
while (index < 5) {
  if (index == 2) {
    index++;
    continue;
  }

  printf("%d\n", index);
  index++;
}

printf("-----\n");

for (; i < 5; i++) {
  if (i == 2) {
    continue;
  }
  printf("%d\n", i);
}

/*
0
1
3
4
-----
0
1
3
4
*/
```

These code blocks showcase the usage of `for` loops. In the first block, the `while` loop continues until `index` is less than 5. It skips the iteration when `index` is equal to 2 using the `continue` statement. In the second block, the `for` loop initializes `i` to 0, executes until `i` is less than 5, increments `i` by 1 in each iteration, and skips the iteration when `i` is equal to 2 using the `continue` statement. In both loops, the current value of the loop variable is printed.

## Array Manipulation

```c
const int myNumbers[] = { 4, 6, 9, 20, 5, 10 };

printf("%d\n", myNumbers[0]);

printf("-----\n");

for (int index = 0; index < numLanguages; index++) {
  printf("Language %d is %s\n", index + 1, myLanguages[index]);
}

printf("-----\n");

int matrix[2][3] = { {1, 9, 2}, {7, 6, 8} };
printf("%d\n", matrix[0][2]);
matrix[0][2] = 5;
printf("%d\n", matrix[0][2]);

printf("-----\n");

for (int i = 0; i < 2; i++) {
  for (int j = 0; j < 3; j++) {
    printf("%d\n", matrix[i][j]);
  }
}

/*
4
-----
Language 1 is C
Language 2 is C++
Language 3 is Java
Language 4 is C#
Language 5 is PHP
Language 6 is Python
Language 7 is Ruby
Language 8 is JavaScript
Language 9 is Rust
Language 10 is Go
Language 11 is Swift
Language 12 is R
Language 13 is Kotlin
-----
2
5
*/
```

These code blocks demonstrate array manipulation. The first block prints the value of the first element in the `myNumbers` array. The second block uses a `for` loop to iterate over the `myLanguages` array and prints each language's name along with its corresponding index. The third block initializes a 2-dimensional array called `matrix` and prints the value at a specific index. It then modifies that value and prints it again. The final block uses nested `for` loops to iterate over the `matrix` array and print each element.

## String Manipulation

```c
int x = 0;
char* name = "Osagie";

for (; x <= strlen(name); x++) {
  printf("%c\n", name[x]);
}

/*
1
9
5
7
6
8
-----
O
s
a
g
i
e
*/
```

This code block demonstrates string manipulation. It declares an integer variable `x` and a character pointer `name` that points to the string "Osagie". A `for` loop is used to iterate over the characters in the `name` string. It prints each character using the `%c` format specifier.

## Practice

Assuming you [have an account](https://replit.com/signup) or are already [logged in](https://replit.com/login) on Replit.

- [Try the Replit](https://replit.com/@OsagieNoah/advanced#advanced.c) Editor online.
- Click the Fork button.
- Edit the current code.

{% embed https://replit.com/@OsagieNoah/advanced#advanced.c %}

## Conclusion

In this blog post, we went through a C code snippet step by step, explaining each part and its logic for beginners. We covered variable declarations, comparisons, conditionals, loops, array manipulation, and string manipulation. Understanding these fundamental concepts will help you in your journey to becoming a proficient C programmer.

---

## References

Here are some reference links that can provide additional information and explanations on the topics covered in the blog:

1. **Variables and Constants**:
   - C Variable Types: [GeeksforGeeks](https://www.geeksforgeeks.org/variables-in-c/)
   - Understanding Constants in C: [Programiz](https://www.programiz.com/c-programming/c-variables-constants)

2. **Conditionals**:
   - If-Else Statements: [GeeksforGeeks](https://www.geeksforgeeks.org/decision-making-c-c-else-nested-else/)
   - Ternary Operator: [GeeksforGeeks](https://www.geeksforgeeks.org/conditional-or-ternary-operator-in-c/)

3. **Switch Statements**:
   - Switch Statement: [GeeksforGeeks](https://www.geeksforgeeks.org/switch-statement-cc/)

4. **Loops**:
   - C Loops: [GeeksforGeeks](https://www.geeksforgeeks.org/c-loops/)
   - While Loop: [GeeksforGeeks](https://www.geeksforgeeks.org/c-while-loop/?)
   - C while and do...while Loop: [GeeksforGeeks](https://www.programiz.com/c-programming/c-do-while-loops)

5. **Array Manipulation**:
   - Arrays in C: [GeeksforGeeks](https://www.geeksforgeeks.org/arrays-in-c-cpp/)
   - 2D Arrays in C: [GeeksforGeeks](https://www.geeksforgeeks.org/multidimensional-arrays-in-c/)
   - 2D Arrays in C: [Tutorials point](https://www.tutorialspoint.com/cprogramming/c_multi_dimensional_arrays.htm#:~:text=A%20two-dimensional%20array%20is%2C%20in%20essence%2C%20a%20list,type%20and%20arrayNamewill%20be%20a%20valid%20C%20identifier.)

6. **String Manipulation**:
   - Strings in C: [GeeksforGeeks](https://www.geeksforgeeks.org/strings-in-c-2/)
   - String Functions: [Programiz](https://www.programiz.com/c-programming/c-strings)

These references should provide additional explanations and examples to enhance the understanding of the concepts covered in the blog post.

---

**Happy Coding!**

---