In C, you can make decisions in your code using if, else-if, else, ternary operators, and switch statements.
if (x == 3) { printf("x is 3!"); }
An if statement checks a condition and runs the code inside if the condition is true.
if (x == 3) { printf("x is 3!"); }
An else-if statement provides additional conditions to test if the previous if condition was false.
if (x > 3) { printf("x is greater than 3"); } else if (x < 3) { printf("x is less than 3"); }
An else statement runs code when all previous if and else-if conditions are false.
if (x > 3) { printf("x is greater than 3"); } else if (x < 3) { printf("x is less than 3"); } else { printf("x is 3"); }
A dangling else occurs when it's unclear which if statement an else belongs to. It's best to use braces to clarify.
if (x > 3) { printf("x is greater than 3"); } else if (x < 3) { printf("x is less than 3"); } else { printf("x is 3"); }
A ternary operator is a shorthand for simple if-else statements. It chooses between two values based on a condition.
int min = (a < b) ? a : b; /* min gets the smaller of a or b */
A switch statement lets you test a variable against several possible values. It's a cleaner way to handle multiple conditions.
int day = 2; switch (day) { case 1: printf("Monday"); break; case 2: printf("Tuesday"); break; default: printf("Other day"); }
Logical operators like && (and), || (or), and ! (not) are used to combine or negate conditions in if statements.
if (a > 5 && b < 10) { printf("Both conditions are true"); }
Loops let you repeat actions. The main types are while loops, do-while loops, and for loops.
while (a < 10) { a++; }
A while loop keeps running as long as its condition is true.
while (a < 10) { a++; }
A do-while loop runs its code at least once before checking the condition to continue.
do { printf("Not true!"); } while (2 == 3);
A for loop repeats a set number of times, which is useful when you know in advance how many times you want to loop.
for (int i = 0; i < 10; i++) { printf("Hello!"); }
You can control loop execution with continue (to skip to the next iteration) and break (to exit the loop).
for (int i = 0; i < 10; i++) { if (i == 5) continue; /* Skip the rest of the loop body */ printf("%d\n", i); }
You can convert a for loop to a while loop and vice versa. They are functionally similar but use different syntax.
/* For loop */ for (int i = 0; i < 10; i++) { printf("%d\n", i); } /* Equivalent while loop */ int i = 0; while (i < 10) { printf("%d\n", i); i++; }
Welcome to our comprehensive collection of programming language cheatsheets! Whether you're a seasoned developer or a beginner, these quick reference guides provide essential tips and key information for all major languages. They focus on core concepts, commands, and functions—designed to enhance your efficiency and productivity.
ManageEngine Site24x7, a leading IT monitoring and observability platform, is committed to equipping developers and IT professionals with the tools and insights needed to excel in their fields.
Monitor your IT infrastructure effortlessly with Site24x7 and get comprehensive insights and ensure smooth operations with 24/7 monitoring.
Sign up now!