Skip to content

Statements in C

In C programming, a statement is a complete instruction that performs a specific task. Statements are the building blocks of a C program and are executed sequentially unless control flow statements are used to alter the flow of execution. Understanding different types of statements and their usage is crucial for writing structured and functional C code. Let’s explore statements in detail:

Types of Statements:

  1. Expression Statements:
    • An expression statement consists of an expression followed by a semicolon (;).
    • It evaluates the expression and discards the result (if any).
    • Example: x = y + z;, printf(“Hello, World!”);.
  2. Compound Statements (Blocks):
    • A compound statement, also known as a block, is a group of statements enclosed within curly braces {}.
    • It allows multiple statements to be treated as a single unit.
    • Example:

{

int x = 10;

printf(“Value of x: %d\n”, x);

}

  1. Selection Statements:
    • Selection statements allow conditional execution of blocks of code.
    • if statement: Executes a block of code if a specified condition is true.
    • if-else statement: Executes one block of code if a condition is true and another block if the condition is false.
    • switch statement: Evaluates an expression and executes code based on matching cases.
    • Example:

if (x > y)

{

 printf(“x is greater than y\n”);

}

else

{

printf(“x is not greater than y\n”);

 }

  1. Iteration Statements (Loops):
    • Iteration statements, also known as loops, allow repeated execution of a block of code.
    • while loop: Executes a block of code repeatedly as long as a specified condition is true.
    • do-while loop: Similar to a while loop but executes the block of code at least once, then repeats as long as the condition is true.
    • for loop: Executes a block of code repeatedly with a loop control variable initialized, tested, and incremented/decremented within the loop.
    • Example:

for (int i = 0; i < 5; i++)

{

printf(“%d “, i);

}

  1. Jump Statements:
    • Jump statements alter the flow of control within a program.
    • break: Terminates the execution of the innermost loop or switch statement.
    • continue: Skips the remaining code within a loop and proceeds to the next iteration.
    • return: Exits a function and optionally returns a value.
    • goto: Transfers control to a labeled statement within the same function.
    • Example:

for (int i = 0; i < 10; i++)

{

if (i == 5)

 {

break; // Exit loop when i is 5

} printf(“%d “, i);

}

Statement Termination:

  • Statements in C are terminated by a semicolon (;), which indicates the end of a statement.
  • Omitting the semicolon or placing it incorrectly can lead to syntax errors.

Conclusion:

Statements in C provide the means to express the logic and control flow of a program. By using a combination of expression statements, compound statements, selection statements, iteration statements, and jump statements, programmers can create structured and functional code to accomplish various tasks. Understanding the different types of statements and their usage is essential for writing clear, concise, and efficient C programs.