• Home
  • 1.23 Expressions and Statements Basics

1.23 Expressions and Statements Basics

View Categories

1.23 Expressions and Statements Basics

1 min read

An expression is a combination of operands and operators that produces a value. A statement is a complete instruction that controls execution or performs an operation.

#include <stdio.h>

int main()
{
    int a = 10;
    int b = 20;

    a + b;

    a = a + b;

    printf("%d\n", a);

    return 0;
}

Example Output #

30

Explanation #

The expression:

a + b

produces the value 30, but the result is not used.

The expression:

a = a + b

contains an addition expression and an assignment operation. The resulting value is assigned to a.

A semicolon turns an expression into an expression statement:

a + b;

C also has other types of statements, including compound statements, selection statements, iteration statements, and jump statements.

Basic Statement Categories #

Category Examples Purpose
Expression statement a = 10; Evaluates an expression
Compound statement { ... } Groups multiple statements
Selection statement if, switch Selects execution paths
Iteration statement while, do, for Repeats statements
Jump statement break, continue, goto, return Transfers control

Expressions and Statements #

Expression Statement
Produces a value or designates an object/function Represents a complete executable construct
Can be part of a larger expression Can contain one or more expressions
Example: a + b Example: a = a + b;
Does not necessarily end with ; An expression statement ends with ;

A compound statement is a block enclosed by braces:

{
    int number = 10;
    printf("%d\n", number);
}

The declarations and statements inside the braces form a single compound statement.

Powered by BetterDocs

Leave a Reply

Your email address will not be published. Required fields are marked *