C Control Statements Overview
C Control Statements are used to control the flow of execution in a C program. The following are the types of control statements in C:
- Conditional statements
- Loop statements
- Jump statements
Conditional Statements
The conditional statements in C are used to execute a certain block of statements based on a condition. The following are the types of conditional statements in C:
if statement
The if
statement is used to execute a block of statements if the condition is true. The general syntax of the if statement is as follows:
if (condition) {
// statements
}
if-else statement
The if-else
statement is used to execute one block of statements if the condition is true, and another block of statements if the condition is false. The general syntax of the if-else statement is as follows:
if (condition) {
// statements
} else {
// statements
}
nested if statement
The nested if statement is used to execute a block of statements based on multiple conditions. The general syntax of the nested if statement is as follows:
if (condition1) {
if (condition2) {
// statements
}
}
Loop Statements
The loop statements in C are used to execute a block of statements repeatedly until a certain condition is met. The following are the types of loop statements in C:
for loop
The for
loop is used to repeatedly execute a block of statements until the condition is false. The general syntax of the for
loop is as follows:
for (initialization; condition; increment) {
// statements
}
while loop
The while
loop is used to repeatedly execute a block of statements until the condition is false. The general syntax of the while
loop is as follows:
while (condition) {
// statements
}
do-while loop
The do-while
loop is used to repeatedly execute a block of statements until the condition is false. The do-while
loop executes at least one iteration before evaluating the condition. The general syntax of the do-while
loop is as follows:
do {
// statements
} while (condition);
Jump Statements
The jump statements in C are used to transfer control to a different part of the program. The following are the types of jump statements in C:
break statement
The break
statement is used to terminate the current loop or switch statement. The general syntax of the break
statement is as follows:
break;
continue statement
The continue
statement is used to skip the rest of the statements in the current iteration of the loop and move on to the next iteration. The general syntax of the continue
statement is as follows:
continue;
goto statement
The goto
statement is used to transfer control to a labeled statement in the program. The general syntax of the goto
statement is as follows:
goto label;
//...
label: statement;
Important Points
- Control statements are used to control the flow of execution in a C program.
- Conditional statements are used to execute a block of statements based on a condition.
- Loop statements are used to repeatedly execute a block of statements until a certain condition is met.
- Jump statements are used to transfer control to a different part of the program.
Summary
C Control Statements are a fundamental part of programming in the C language. By using these statements, you can control the flow of execution in your program, iterate over data, and jump to different parts of your program. Understanding the syntax, usage, and behavior of C control statements is essential to writing efficient and effective C code.