Statements in C

Statements in C are the smallest standalone units of execution in a program. They perform actions such as assignments, calculations, function calls, and control of program flow. Every C program consists of a sequence of statements that the compiler executes sequentially unless directed otherwise by control structures.

Understanding statements is essential to write organized, readable, and efficient programs. This guide covers types of statements, their syntax, usage, rules, best practices, and examples.

What is a Statement?

  • A statement is a complete instruction that tells the compiler to perform a specific action.
  • Every statement in C ends with a semicolon (;) except for compound statements (blocks) and preprocessor directives.
  • Statements are executed sequentially unless control structures (like if, loops, or switch) change the flow.
  • Example: int a = 10; // assignment statement

Types of Statements in C

  • Expression Statements: Perform calculations, assignments, or function calls.
  • Compound Statements (Blocks): Group multiple statements into one unit using braces {}.
  • Selection Statements: Control program flow using decisions (if, if-else, switch).
  • Iteration Statements: Execute statements repeatedly (for, while, do-while).
  • Jump Statements: Change control flow abruptly (break, continue, return, goto).
  • Empty Statement: A semicolon by itself representing no operation.

Expression Statements

An expression followed by a semicolon is called an expression statement. It usually changes the program state or produces a side effect.

  • Assignment Expression: int x = 10;
  • Function Call: printf("Hello World\n");
  • Increment/Decrement: x++; y--;
  • Arithmetic Operations: x = a + b * c;

Compound Statements (Blocks)

A compound statement groups multiple statements into a single unit using braces { }. It is treated as one statement by the compiler.

  • Syntax: { statement1; statement2; statement3; }
  • Example: { int x = 5; x++; printf("%d", x); }
  • Used in loops, if-else structures, and functions to execute multiple statements together.
  • Helps maintain proper scope for variables declared inside the block.

Selection Statements

Selection statements allow the program to make decisions and execute certain statements based on conditions.

  • if Statement: Executes a block if a condition is true. Example: if (x > 0) { printf("Positive"); }
  • if-else Statement: Executes one block if condition is true, another if false. Example: if (x > 0) { printf("Positive"); } else { printf("Non-positive"); }
  • Nested if: if statements inside another if for multiple conditions.
  • switch Statement: Executes a block based on multiple possible constant values. Example: switch (day) { case 1: printf("Monday"); break; default: printf("Other day"); }

Iteration Statements (Loops)

Iteration statements repeat execution of a block of code until a condition is met.

  • for Loop: Repeats a block a known number of times. Example: for(int i=0; i<5; i++){ printf("%d", i); }
  • while Loop: Repeats as long as a condition is true. Example: int i=0; while(i<5){ printf("%d", i); i++; }
  • do-while Loop: Executes at least once, then repeats while condition is true. Example: int i=0; do{ printf("%d", i); i++; } while(i<5);

Jump Statements

Jump statements alter normal sequential execution of statements.

  • break: Exits a loop or switch immediately. Example: break;
  • continue: Skips current iteration and continues with next loop iteration. Example: continue;
  • return: Exits a function and optionally returns a value. Example: return 0;
  • goto: Jumps to a labeled statement. Example: goto label; label: printf("Jumped");

Empty Statement

  • A semicolon (;) by itself represents an empty statement.
  • Used where a statement is required syntactically but no action is needed.
  • Example: for(int i=0; i<10; i++); // loop body is empty

Rules for Statements in C

  • Every statement ends with a semicolon (except blocks and preprocessor directives).
  • Statements can be nested within other statements (e.g., loops inside if-else).
  • Compound statements can declare local variables accessible only inside the block.
  • Statements should be properly indented for readability.
  • Avoid unreachable statements after return, break, or goto.

Best Practices for Statements

  • Use meaningful variable names in statements for readability.
  • Keep statements short and simple to improve maintainability.
  • Group related statements in blocks for logical organization.
  • Avoid using goto; prefer structured control statements.
  • Always comment complex statements or blocks to explain logic.

Examples of Statements in C

  • Expression Statement: int x = 5; x += 10; printf("%d", x);
  • Compound Statement: { int a = 10; int b = 20; printf("Sum: %d", a+b); }
  • If-Else Statement: if (score >= 50) { printf("Pass"); } else { printf("Fail"); }
  • Switch Statement: switch(day) { case 1: printf("Monday"); break; default: printf("Other day"); }
  • For Loop Statement: for(int i=0; i<5; i++){ printf("%d", i); }
  • While Loop Statement: int i=0; while(i<5){ printf("%d", i); i++; }
  • Do-While Statement: int i=0; do{ printf("%d", i); i++; } while(i<5);
  • Jump Statement: for(int i=0; i<10; i++){ if(i==5) break; }

Conclusion

Statements form the backbone of any C program. They direct the flow of execution, perform operations, and control program logic. By understanding types of statements, their rules, and best practices, programmers can write organized, readable, and efficient code.

Mastering statements in C is crucial for building reliable programs that are easy to maintain and extend.