Skip to main content

Overview

The 0x01-variables_if_else_while module covers fundamental programming concepts: declaring variables, making decisions with conditionals, and implementing loops. You’ll learn to control program flow and work with different data types.

Variables and Random Numbers

Positive or Negative

This program demonstrates variable declaration, random number generation, and conditional statements.
0-positive_or_negative.c
#include <stdlib.h>
#include <time.h>
#include <stdio.h>
/**
   * main - Entry point
   *Description: This program outputs positive if greater than zero,
   *negative if less than zero, and zero if equal to zero
   *Return: 0 on success
*/
int main(void)
{
int n;

srand(time(0));
n = rand() - RAND_MAX / 2;
if (n > 0)
{
printf("%d is positive\n", n);
}
else if (n < 0)
{
printf("%d is negative\n", n);
}
else
{
printf("%d is zero\n", n);
}
return (0);
}
srand(time(0)) seeds the random number generator with the current time, ensuring different values on each run.
Key concepts:
  • Variable declaration: int n;
  • Random number generation: rand()
  • Conditional logic: if-else if-else

Conditional Statements

If-Else Chain

The last digit program shows complex conditional logic:
1-last_digit.c
#include <stdlib.h>
#include <time.h>
#include<stdio.h>
/**
   * main - Entry point
   *Description: This program outputs is greater tahn 5 if greater than five,
   *less than 6 and not if less than 6 and not 0
   *0 if equal to 0
   *Return: 0 on success
*/
int main(void)
{
int n;
int last_digit;

srand(time(0));
n = rand() - RAND_MAX / 2;
last_digit = n % 10;
if (last_digit > 5)
{
printf("Last digit of %d is %d and is greater than 5\n", n, last_digit);
}
else if (last_digit < 6 && last_digit != 0)
{
printf("Last digit of %d is %d and is less than 6 and not 0\n", n, last_digit);
}
else if (last_digit == 0)
{
printf("Last digit of %d is %d and is 0\n", n, last_digit);
}
return (0);
}
The modulo operator % gets the remainder. n % 10 extracts the last digit of any number.
  • && : Logical AND (both conditions must be true)
  • || : Logical OR (at least one condition must be true)
  • ! : Logical NOT (negates the condition)
  • == : Equality comparison
  • != : Not equal comparison

Loops

For Loop - Printing the Alphabet

2-print_alphabet.c
#include <stdio.h>
/**
   * main - Entry point
   *Description: This program prints the alphabet in lowercase
   *Return: 0 on success
*/

int main(void)
{
char alphabet;
for (alphabet = 'a'; alphabet <= 'z' ; alphabet++)
putchar(alphabet);
putchar('\n');
return (0);
}
For loop structure:
  1. Initialization: alphabet = 'a' - sets starting value
  2. Condition: alphabet <= 'z' - continues while true
  3. Increment: alphabet++ - executes after each iteration
In C, characters are actually integers (ASCII values). You can increment them: 'a' becomes 'b', etc.

For Loop - Printing Numbers

5-print_numbers.c
#include <stdio.h>
/**
 * main - Entry point
 *Description: This program prints all single digit numbers
 *of base 10 starting from 0
 *Return: 0 on success
 */

int main(void)
{
int number;
for (number = 0; number <= 9; number++)
{
printf("%d", number);
}
printf("\n");
return (0);
}

While Loop - Combinations

9-print_comb.c
#include <stdio.h>
/**
 * main - Entry point
 * Description: Write a program that prints all possible combinations
 * of single-digit numbers.
 * Return: 0 on success
 */

int main(void)
{
int number = 0;

while (number < 10)
{
putchar('0' + number);
if (number != 9)
{
putchar(',');
putchar(' ');
}
number++;
}
putchar('\n');
return (0);
}
'0' + number converts an integer to its character representation. '0' + 5 gives '5'.

Loop Types Comparison

Best when you know how many iterations you need.
for (i = 0; i < 10; i++)
{
    /* code */
}

putchar() vs printf()

Both functions output characters, but with different use cases:
/* Outputs a single character */
putchar('A');
putchar('\n');
putchar() is more efficient for single characters. Use printf() when you need formatting or multiple values.

Common Patterns

Printing with Separators

A common pattern is printing items with commas between them (but not after the last item):
for (i = 0; i < 10; i++)
{
    printf("%d", i);
    if (i != 9)  /* Not the last item */
        printf(", ");
}

Infinite Loop Prevention

Always ensure your loop condition will eventually become false:Bad:
int i = 0;
while (i < 10)
{
    printf("%d", i);
    /* Forgot to increment i! */
}
Good:
int i = 0;
while (i < 10)
{
    printf("%d", i);
    i++;  /* Loop will terminate */
}

Compilation and Testing

# Compile with all warnings
gcc -Wall -Werror -Wextra -pedantic -std=gnu89 0-positive_or_negative.c -o positive

# Run multiple times to see different random values
./positive
./positive
./positive

Best Practices

Initialize variables before useAlways initialize variables to avoid undefined behavior:
int number = 0;  /* Good */
int other;       /* Contains garbage value */
Use meaningful variable nameslast_digit is clearer than ld or x. Code readability matters!
Proper indentationIndent code blocks consistently (Betty style uses tabs). It makes logic easier to follow.

Common Pitfalls

Wrong:
if (n = 0)  /* This assigns 0 to n! */
Correct:
if (n == 0)  /* This compares n to 0 */
  • Single quotes for characters: 'a'
  • Double quotes for strings: "hello"
putchar('a');      /* Correct */
putchar("a");      /* Wrong - type mismatch */

Build docs developers (and LLMs) love