C Statements

Learn several basic types of statements.

Types of C Statements

In the C programming language, there are several basic types of statements that you can use to create programs. These statements are the building blocks of C code and are used to perform various operations and control the flow of your program. Here are some of the most fundamental types of statements in C:

1. Conditional Statements:

Conditional statements allow you to execute different blocks of code based on whether a specified condition is true or false. The primary conditional statements in C are:

i. if Statement:

if (condition) {
    // Code to execute if the condition is true
}

ii. if-else Statement:

if (condition) {
    // Code to execute if the condition is true
} else {
    // Code to execute if the condition is false
}

iii. Multiple if-else Statement:

if (condition1) {
    // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else {
    // Code to execute if none of the conditions are true
}

iv. switch Statement:

switch (expression) {
    case value1:
        // Code to execute if expression matches value1
        break;
    case value2:
        // Code to execute if expression matches value2
        break;
    // More cases as needed
    default:
        // Code to execute if expression doesn't match any case
}

2. Loop Statements:

Loop statements are used to execute a block of code repeatedly as long as a specific condition is met. There are three primary types of loop statements in C:

i. for Loop:

The 'for' loop is used when you know the exact number of iterations required. It consists of an initialization, a condition, and an iteration statement. The loop will continue to execute as long as the condition is true.

for (initialization; condition; iteration) {
    // Code to be executed repeatedly
}

ii. while Loop:

The 'while' loop repeatedly executes a block of code as long as a given condition is true. It checks the condition before each iteration.

while (condition) {
    // Code to be executed repeatedly
}

iii. do-while Loop:

The 'do-while' loop is similar to the 'while' loop, but it checks the condition after executing the block of code, meaning the code within the loop will always run at least once.

do {
    // Code to be executed repeatedly
} while (condition);

* These are some of the fundamental statements in the C programming language. If you want to learn more additional C statements (for example, goto, break, continue, etc.) 'click here'.

What's Next?

We actively create content for our YouTube channel and consistently upload or share knowledge on the web platform.