In this tutorial, we are going to learn about C Expressions and Statements.
C Expressions:
An expression is a combination of constants and variables interconnected by one or more operators. An expression consists of one or more operands and one or more operators.
Operands are values and operators are symbols that represent particular actions.
Given below are a few examples of expressions:
- num1 + num2 // variables num1 and num2 are operands and + is the operator used.
- x = y // the assignment operator (=) is used to assign the value stored in y to x.
- a = b + c // the value of the expression (b + c) is assigned to a.
- x <= y // the lesser-than-equalTo (<=) the relational operator compares the values of x and y and returns either 1 (true) or 0 (false).
- a++ // the value of variable a is incremented by 1 i.e, this expression is equivalent to a = a + 1.
Expressions can also represent logical conditions that are either true or false. In C, the conditions true and false are represented by the integer values 1 and 0 respectively.
C Statements:
Earlier, we learned that a program is a collection of statements.
A statement is an instruction given to the computer to perform an action.
There are three different types of statements in C:
- Expression Statements
- Compound Statements
- Control Statements
An expression statement or simple statement consists of an expression followed by a semicolon (;).
Given below are few examples of expression statements:
a = 100;
b = 20;
c = a / b;
A compound statement also called a block, consists of several individual statements enclosed within a pair of braces { }. For example:
{
a = 3;
b = 10;
c = a + b;
}
A single statement or a block of statements can be executed depending upon a condition using control statements like if, if-else, etc. We shall learn more about control statements in later sections.
Given below is an example of if control statement:
a = 10;
if ( a > 5) {
b = a + 10;
}
References:
Happy Learning 🙂