Control Structures in C

Mannan Ul Haq
0

In C, control structures are used to control the flow of execution in a program. They allow you to make decisions and repeat blocks of code based on certain conditions. Here are the main control structures in C:


1. if statement: 

The if statement allows you to execute a block of code if a certain condition is true. It has the following syntax:


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


Example of if statement:


int x = 10;

if (x > 0)
{
    printf("x is positive\n");
}


2. if-else statement:

The if-else statement allows you to execute one block of code if a condition is true and another block of code if the condition is false. It has the following syntax:


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


Example of if-else statement:


int x = 5;

if (x % 2 == 0)
{
    printf("x is even\n");
}
else
{
    printf("x is odd\n");
}


3. nested if-else

You can also nest if-else statements to handle multiple conditions.


if (condition1)
{
    // code to be executed if condition1 is true
}
else if (condition2)
{
    // code to be executed if condition1 is false and condition2 is true
}
else
{
    // code to be executed if both condition1 and condition2 are false
}


Example of nested if-else statement:


int x = 10;

if (x > 0)
{
    printf("x is positive\n");
}
else if (x < 0)
{
    printf("x is negative\n");
}
else
{
    printf("x is zero\n");
}


4. short-hand if-else statement:

The short-hand if-else statement, also known as the ternary operator, is a concise way of writing simple conditional expressions in many programming languages, including C. It allows you to evaluate a condition and choose between two expressions based on whether the condition is true or false, all in a single line of code.

The syntax of the ternary operator is as follows:


condition ? expression1 : expression2;


Here's how it works:

1. The `condition` is an expression that is evaluated to either true or false.

2. If the `condition` is true, the `expression1` is executed and its result is returned.

3. If the `condition` is false, the `expression2` is executed and its result is returned.


Let's see this example:


int x = 10;
int y;

// Assign y the value of x if x is greater than 5, otherwise assign 0
y = (x > 5) ? x : 0;

printf("y = %d\n", y); // Output: y = 10


5. switch statement: 

The switch statement allows you to select one of many code blocks to be executed based on the value of a variable. It has the following syntax:


switch (variable)
{
case value1:

    // code to be executed if variable equals value1
    break;

case value2:

    // code to be executed if variable equals value2
    break;

    // ...

default:

    // code to be executed if none of the above cases are true
    break;

}


Example of switch statement:


char grade = 'B';

switch (grade)
{
case 'A':
    printf("Excellent!\n");
    break;

case 'B':
    printf("Good job!\n");
    break;

case 'C':
    printf("Passing grade\n");
    break;

default:
    printf("Invalid grade\n");
    break;
}


6. while loop: 

The while loop repeatedly executes a block of code as long as a certain condition is true.


while (condition)
{
    // code to be executed repeatedly as long as the condition is true
}


Example of while loop:


int count = 0;

while (count < 5)
{
    printf("Count: %d\n", count);
    count++;
}


7. do-while loop:

The do-while loop is similar to the while loop, but it guarantees that the code block is executed at least once before checking the condition. It has the following syntax:


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


Example of do-while loop:


int count = 0;

do {
    printf("Count: %d\n", count);
    count++;
} while (count < 5);


8. for loop:

The for loop allows you to repeatedly execute a block of code for a specific number of times. It has the following syntax:


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


Example of for loop:


for (int i = 1; i <= 5; i++)
{
    printf("i: %d\n", i);
}


These are the main control structures in C. They provide powerful ways to control the flow of your program based on various conditions and loops.



Jump Statements:

In C, jump statements are used to alter the normal flow of control in a program. They allow you to transfer program control to a different part of the code. The two main jump statements in C are:


1. break statement:

The break statement is used to exit the current loop or switch statement. When encountered, it terminates the innermost loop or exits the switch statement, and the control is transferred to the next statement after the loop or switch. Here's an example:


for (int i = 0; i < 10; i++)
{
    if (i == 5)
    {
        break; // exit the loop when i is equal to 5
    }
    printf("%d ", i);
}

printf("Loop ended.\n");


Output:

0 1 2 3 4 Loop ended.


2. continue statement:

The continue statement is used to skip the rest of the current iteration of a loop and move to the next iteration. It forces the loop to immediately jump to the next iteration without executing the remaining code inside the loop for the current iteration. Here's an example:


for (int i = 0; i < 5; i++)
{
    if (i == 2)
    {
        // Skip iteration when i is equal to 2
        continue;
    }
    printf("%d ", i);
}

printf("Loop ended.\n");
 

Output:

0 1 3 4 Loop ended.


These jump statements provide ways to control the flow of a program by altering the normal sequence of execution.


Tags

Post a Comment

0Comments

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Check Now
Accept !