DevAcademia
C++C#CPythonJava
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz
  • C Basics

  • Introduction to C
  • Getting Started with C
  • C Syntax
  • C Output
  • C Comments
  • C Variables
  • C Data Types
  • C Constants
  • C Operators
  • C Booleans
  • C If...Else Statements
  • C Switch Statement
  • C While Loops
  • C For Loops
  • C Break and Continue
  • C Strings
  • C User Input
  • C Memory Address
  • C Pointers
  • C Files
  • C Functions

  • C Functions
  • C Function Parameters
  • C Scope
  • C Function Declaration
  • C Recursion
  • C Math Functions
  • C Structures

  • C Structures
  • C Structs & Pointers
  • C Unions
  • C Enums

  • C Enums
  • C Memory

  • C Allocate Memory
  • C Access Memory
  • C Reallocate Memory
  • C Deallocate Memory
  • C Structs and Memory
  • C Memory Example
  • C Quiz

  • C Quiz

Loading C tutorial…

Loading content
C BasicsTopic 4 of 64
←PreviousPrevNextNext→

C Statements

What is a Statement?

A statement in C is a complete instruction for the compiler. It usually ends with a semicolon (`;`).

Statements can be simple (like assigning a value) or complex (like control flow that directs execution).

Blocks (compound statements) use `{ ... }` and are treated as a single statement; **do not** put a semicolon after a block when used with `if/for/while`.

Example
#include <stdio.h>

int main(void) {
    int x = 5; // declaration + assignment
    x = x + 2; // expression statement
    printf("%d\n", x);
    return 0;
}
Output
7

Expression Statements

An expression statement performs an action, such as arithmetic or function calls.

Examples include assignments (`x = 5;`), increments (`i++;`), or calls (`printf(...);`).

Example
#include <stdio.h>

int main(void) {
    int a = 10;
    a++; // increment
    printf("%d\n", a);
    return 0;
}
Output
11

Compound Statements (Blocks)

Compound statements group multiple instructions using `{ }`. These are useful in functions or control structures like `if` and `for`.

A block is treated as a single statement, even though it contains many lines. Do not write a semicolon after the closing brace when the block is controlled by `if/for/while`.

Example
#include <stdio.h>

int main(void) {
    {
        int y = 3;
        int z = 4;
        printf("%d\n", y + z);
    }
    return 0;
}
Output
7

Control Flow Statements

Control flow statements determine how the program executes based on conditions and loops:

- **if/else**: Decision making.

- **switch**: Multi-way branching.

- **for, while, do-while**: Loops for repetition.

Example
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 3; i++) {
        printf("Iteration %d\n", i);
    }

    int n = 2;
    switch (n) {
        case 1: printf("One\n"); break;
        case 2: printf("Two\n"); break;
        default: printf("Other\n");
    }

    return 0;
}
Output
Iteration 1
Iteration 2
Iteration 3
Two

Jump Statements (break, continue, return, goto)

`break` exits the nearest loop or `switch`; `continue` skips to the next loop iteration; `return` exits a function; `goto` jumps to a labeled statement (use sparingly; `switch` case/default labels are the most common labeled statements).

Example
#include <stdio.h>

int main(void) {
    for (int i = 1; i <= 5; i++) {
        if (i == 3) continue; // skip printing 3
        if (i == 5) { printf("break at %d\n", i); break; }
        printf("%d ", i);
    }
    printf("\n");

    // Labeled statement example (commonly seen with switch)
    goto done;
    printf("This line is skipped.\n");

done:
    printf("Finished with return.\n");
    return 0;
}
Output
1 2 4 
break at 5
Finished with return.

Empty (Null) Statement & Semicolon Traps

A lone semicolon `;` is a valid empty statement. It's useful for loops with empty bodies, but can cause bugs if used accidentally (e.g., after `if`).

Example
#include <stdio.h>

int main(void) {
    // Intentional empty loop body to find length index
    const char *s = "hello";
    int i = 0;
    for (i = 0; s[i] != '\0'; i++) ; // empty body on purpose
    printf("length=%d\n", i);

    int flag = 1;
    // BUG: stray semicolon makes the if do nothing; block always runs
    if (flag); { printf("This prints regardless of flag! (buggy)\n"); }

    // Correct form (no stray semicolon):
    if (flag) { printf("This prints when flag is true.\n"); }

    return 0;
}
Output
length=5
This prints regardless of flag! (buggy)
This prints when flag is true.

Comparison of Statement Types

The table below summarizes the main categories of statements in C.

TypeExamplePurpose
Expressionx = x + 1;Performs a calculation or function call
Compound (Block){ int a=1; int b=2; }Groups multiple statements
Control Flowif (x>0) { ... }Alters execution order
Jumpbreak; continue; return;Transfers control
Labeledcase 1: ...; default: ...;Targets for switch/goto
Empty (Null);Intentional no-op / loop sentinel
Test your knowledge: C Statements
Quiz Configuration
4 of 8 questions
Sequential
Previous allowed
Review enabled
Early close allowed
Estimated time: 5 min
C BasicsTopic 4 of 64
←PreviousPrevNextNext→