
C Control Structures
- In C programming, control structures are used to control the flow of a program.
- They includes if statements, if...else statements, else if ladders.
- while loops, do...while loops, for loops, switch statements, nested control structures.
- break, labeled break, continue, labeled continue statements, exit statements, and goto statements.
1
The if statement is used to execute a block of code if a specified condition is true.
Loading…
In this code, the program checks if the value of num is greater than 0. If it is true, it prints "The number is positive."
2. if...else Statement
The if...else statement is used to execute one block of code if a condition is true and another block if the condition is false.
Loading…
Here, the program checks if num is greater than 0. If it is, it prints "The number is positive," otherwise, it prints "The number is not positive."
3. else if Ladder
An else if ladder is used to check multiple conditions in sequence.
Loading…
In this example, the program checks if num is positive, negative, or zero and prints the appropriate message.
4. while Loop
The while loop is used to repeatedly execute a block of code as long as a condition is true.
Loading…
This code prints numbers from 1 to 5 by repeatedly executing the printf statement as long as i is less than or equal to 5.
5. do...while Loop
The do...while loop is similar to the while loop, but it always executes the block of code at least once before checking the condition.
Loading…
In this example, the program prints numbers from 1 to 5, and even if the condition is initially false, it executes the loop once.
6. for loop
The for loop is used to iterate over a range of values with an initialization, condition, and increment expression.
Loading…
This code accomplishes the same task as the previous examples by using a for loop. It iterates from 1 to 5 and prints the values.
7. switch Statement
The switch statement is used to select one of many code blocks to be executed based on a given expression.
Loading…
In this example, the program prints a message based on the value of choice.
8. Nested Control Structures
Nested control structures are control structures within other control structures. For example, you can have an if statement inside a for loop.
Loading…
This code uses a nested if statement inside a for loop to print whether a number is even or odd.
9. break Statement
The break statement is used to exit a loop prematurely.
Loading…
In this code, the for loop will terminate when i reaches 3 due to the statement.
10. Labeled break and continue Statements
You can label loops to use break and continue with specific loops in nested structures.
Loading…
11. goto Statement
The goto statement is used to transfer control to a labeled statement.
Loading…
12. exit Statement
The exit statement is used to terminate a program.
Loading…
Conclusion
In C programming, control structures such as if, for, while, switch and more enable developers to create organized and efficient code, facilitating conditional execution, looping, and decision-making in their programs.