Operators and if else
1. Operator Precedence Demonstration:
#include <stdio.h>
int main() {
int result = 5 + 10 * 2 - 8 / 4;
printf("Result: %d\n", result);
return 0;
}
2. Parentheses Influence:
#include <stdio.h>
int main() {
int result = (5 + 10) * 2 - 8 / 4;
printf("Result: %d\n", result);
return 0;
}
3. Bitwise Operator Precedence:
#include <stdio.h>
int main() {
int x = 5, y = 3;
int result = x & y | x << 1;
printf("Result: %d\n", result);
return 0;
}
4. Conditional Operator Exploration:
#include <stdio.h>
int main() {
int a = 10, b = 15;
int max = (a > b) ? a : b;
printf("Maximum: %d\n", max);
return 0;
}
5. Operator Precedence with Function Calls: (functions are advance topic, may discuss them in small)
#include <stdio.h>
int foo() {
printf("Foo called\n");
return 5;
}
int bar() {
printf("Bar called\n");
return 10;
Output :
}
int main() {
int result = foo() + bar() * 2;
printf("Result: %d\n", result);
return 0;
}
6. Complex Expression Simplification:
#include <stdio.h>
int main() {
int x = 5, y = 10, z = 15;
int result = x + y * z / (x + y);
printf("Result: %d\n", result);
return 0;
}
7. Increment/Decrement Operators Effects:
#include <stdio.h>
int main() {
int x = 5, y;
y = x++ + x;
printf("x: %d, y: %d\n", x, y);
return 0;
}
8. Logical Operators and Short-Circuiting:
#include <stdio.h>
int main() {
int x = 5, y = 0;
if (x && y++)
printf("Inside if\n");
printf("x: %d, y: %d\n", x, y);
return 0;
}
9. Assignment Operators and Precedence:
#include <stdio.h>
int main() {
int x = 5, y = 10;
y = x += 3 * 2;
printf("x: %d, y: %d\n", x, y);
return 0;
}
10. Operator Precedence Quiz Game:
#include <stdio.h>
int main() {
int result1 = 5 + 6 / 2;
int result2 = 2 * 3 % 4;
printf("Result 1: %d\n", result1);
printf("Result 2: %d\n", result2);
return 0;
}
# Operator Precedence and If-Else Tutorial
## Program 1: Logical Operators and If-Else
#include <stdio.h>
int main() {
int x = 5, y = 10;
if (x > 3 || y > 15) {
printf("Both conditions are true.\n");
} else {
printf("At least one condition is false.\n");
}
return 0;
}
## Program 2: Ternary Operator and If-Else
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("%d is even.\n", num);
} else {
printf("%d is odd.\n", num);
}
return 0;
}
## Program 3: Compound Conditions and If-Else
#include <stdio.h>
int main() {
int age = 18;
Output:
char gender = 'M';
if (age >= 18 && gender == 'M') {
printf("The person is an adult male.\n");
} else {
printf("The person is not an adult male.\n");
}
return 0;
}
## Program 4: Nested If-Else
#include <stdio.h>
int main() {
int x = 10, y = 5;
if (x > y) {
if (x % 2 == 0) {
printf("x is even and greater than y.\n");
} else {
printf("x is odd and greater than y.\n");
}
} else {
printf("x is not greater than y.\n");
}
return 0;
}
Keep reading with a 7-day free trial
Subscribe to MrRogueKnight’s Substack to keep reading this post and get 7 days of free access to the full post archives.