Jump statements
break statement:
Syntax: for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}
continue statement
Syntax: for (int i = 0; i < 10; i++) {
if (i == 5) {
continue;
}
printf("%d\n", i);
}
goto statement
Syntax: goto label;
// some code here
label:
// some more code here
Conditional statements
If Statement
Syntax: if (x > 10) {
printf("x is greater than 10");
}
If Else Statement
Syntax: if (condition2) {
// Code to execute if both condition1 and condition2 are true
}
else {
// Code to execute if condition1 is true and condition2 is false
}
Nest If Else Statement
Syntax: if (condition1) {
// Code to execute if condition1 is true
if (condition2) {
// Code to execute if both condition1 and condition2 are true
}
else {
// Code to execute if condition1 is true and condition2 is false
}
}
else {
// Code to execute if condition1 is false
}
Looping statements
for loop
Syntax: for(Expression 1; Expression 2; Expression 3){ //code to be executed. }
while loop
Syntax: while(condition) { //code to be executed. }
do-while loop
Syntax: do loop_body_statement while (cond_exp);