C_Programming_Concepts
C_Programming_Concepts
1. Conditional Statements
if (x > 0) {
printf("x is positive\n");
if (x > 0) {
printf("x is positive\n");
} else {
if (x > 0) {
printf("x is positive\n");
} else if (x < 0) {
printf("x is negative\n");
} else {
printf("x is zero\n");
}
- Switch Statement: Handles multiple cases efficiently.
switch (x) {
case 1:
printf("x is 1\n");
break;
case 2:
printf("x is 2\n");
break;
default:
2. Branching Statements
if (i == 5) break;
- continue: Skips the rest of the loop body and starts the next iteration.
if (i == 5) continue;
}
- goto: Jumps to a labeled part of the program (not recommended for structured programming).
goto label;
label:
printf("Jumped to label\n");
3. Loops
printf("%d\n", i);
int i = 0;
while (i < 5) {
printf("%d\n", i);
i++;
int i = 0;
do {
printf("%d\n", i);
i++;
Iterative statements are another term for loops, focusing on repeated execution. All loops (for, while,
5. Logical Errors
Logical errors occur when the program runs but produces incorrect results due to flaws in logic.
Examples:
printf("x is zero\n");
if (x == 0) {
printf("x is zero\n");
- Infinite loops:
int i = 0;
while (i < 5) {
Fix: Increment i:
while (i < 5) {
printf("%d\n", i);
i++;
}
- Off-by-one errors:
printf("%d\n", i);
printf("%d\n", i);