WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

C · Basic · question 6 of 100

What are the basic control structures in C (e.g., if, for, while)?

📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

In C programming language, control structures are used to control the flow of program execution based on certain conditions. Here are the basic control structures in C:

If-else statements: If-else statements are used to execute code based on a certain condition. The syntax for an if-else statement is:

    if (condition) {
        // code to be executed if condition is true
    } else {
        // code to be executed if condition is false
    }

In this example, the code within the if block will be executed if the condition is true, while the code within the else block will be executed if the condition is false.

For loops: For loops are used to iterate over a block of code a specified number of times. The syntax for a for loop is:

    for (initialization; condition; increment) {
        // code to be executed
    }

In this example, the initialization expression is executed before the loop starts, the condition expression is evaluated before each iteration, and the increment expression is executed after each iteration. The code within the loop will be executed as long as the condition expression is true.

While loops: While loops are used to iterate over a block of code as long as a certain condition is true. The syntax for a while loop is:

    while (condition) {
        // code to be executed
    }

In this example, the code within the loop will be executed as long as the condition expression is true.

Do-while loops: Do-while loops are similar to while loops, but the code within the loop is executed at least once before the condition is checked. The syntax for a do-while loop is:

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

In this example, the code within the loop will be executed at least once, and then the condition expression will be checked. The code within the loop will continue to execute as long as the condition expression is true.

Here is an example that demonstrates the use of these basic control structures:

    #include <stdio.h>
    
    int main() {
        int i;
        
        for (i = 0; i < 10; i++) {
            if (i % 2 == 0) {
                printf("%d is evenn", i);
            } else {
                printf("%d is oddn", i);
            }
        }
        
        int j = 0;
        while (j < 5) {
            printf("Hello, world!n");
            j++;
        }
        
        int k = 0;
        do {
            printf("Goodbye, world!n");
            k++;
        } while (k < 5);
        
        return 0;
    }

In this example, a for loop is used to iterate over the values of i and determine whether each value is even or odd. An if-else statement is used to print the appropriate message. A while loop is used to print the message "Hello, world!" five times, and a do-while loop is used to print the message "Goodbye, world!" five times.

Understanding the basic control structures in C is essential for writing programs that can perform different operations based on different conditions. These structures allow developers to create flexible, responsive code that can adapt to changing circumstances.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic C interview — then scores it.
📞 Practice C — free 15 min
📕 Buy this interview preparation book: 100 C questions & answers — PDF + EPUB for $5

All 100 C questions · All topics