CC++ Programming Syntax Examples - Week 3
This document demonstrates the syntax for conditional statements and logical operators in CC++.
1. Conditional Statements
One-Way Selection (if)
Executes code only if the condition is true.
int salary = 600;
if (salary > 500) {
salary = salary + 100; // Bonus
}
printf("Final Salary: %d\n", salary);
#include <iostream>
using namespace std;
int salary = 600;
if (salary > 500) {
salary = salary + 100; // Bonus
}
cout << "Final Salary: " << salary << endl;
Two-Way Selection (if-else)
Executes one block if true, another if false.
int number = 7;
if (number % 2 == 0) {
printf("%d is Even\n", number);
} else {
printf("%d is Odd\n", number);
}
int number = 7;
if (number % 2 == 0) {
cout << number << " is Even" << endl;
} else {
cout << number << " is Odd" << endl;
}
Multi-Way Selection (else-if)
Checks multiple conditions in sequence.
int num = 0;
if (num > 0) {
printf("Positive\n");
} else if (num < 0) {
printf("Negative\n");
} else {
printf("Zero\n");
}
int num = 0;
if (num > 0) {
cout << "Positive" << endl;
} else if (num < 0) {
cout << "Negative" << endl;
} else {
cout << "Zero" << endl;
}
Nested if Statements
An if statement inside another if.
int age = 20;
int hasID = 1; // 1 means true
if (age >= 18) {
if (hasID == 1) {
printf("Access Granted\n");
} else {
printf("ID Required\n");
}
} else {
printf("Access Denied\n");
}
int age = 20;
int hasID = 1; // 1 means true
if (age >= 18) {
if (hasID == 1) {
cout << "Access Granted" << endl;
} else {
cout << "ID Required" << endl;
}
} else {
cout << "Access Denied" << endl;
}
2. Ternary Operator (? :)
A shorthand for if-else.
int a = 10, b = 20;
int max;
// Syntax: (condition) ? value_if_true : value_if_false;
max = (a > b) ? a : b;
printf("Max value is: %d\n", max);
int a = 10, b = 20;
int max;
// Syntax: (condition) ? value_if_true : value_if_false;
max = (a > b) ? a : b;
cout << "Max value is: " << max << endl;
3. Switch Statement
Selects one case based on the value of an integer or character expression.
int day = 3;
switch (day) {
case 1:
printf("Sunday\n");
break;
case 2:
printf("Monday\n");
break;
case 3:
printf("Tuesday\n");
break;
default:
printf("Other Day\n");
}
int day = 3;
switch (day) {
case 1:
cout << "Sunday" << endl;
break;
case 2:
cout << "Monday" << endl;
break;
case 3:
cout << "Tuesday" << endl;
break;
default:
cout << "Other Day" << endl;
}
4. Logical Operators
Used to combine boolean conditions.
int age = 25;
int experience = 3;
// AND (&&): Both must be true
if (age > 20 && experience > 2) {
printf("Hired!\n");
}
// OR (||): At least one must be true
int score = 45;
if (score < 50 || score > 100) {
printf("Invalid Score\n");
}
// NOT (!): Reverses the condition
int isLightOn = 0; // 0 means false
if (!isLightOn) {
printf("The light is OFF\n");
}
int age = 25;
int experience = 3;
// AND (&&): Both must be true
if (age > 20 && experience > 2) {
cout << "Hired!" << endl;
}
// OR (||): At least one must be true
int score = 45;
if (score < 50 || score > 100) {
cout << "Invalid Score" << endl;
}
// NOT (!): Reverses the condition
int isLightOn = 0; // 0 means false
if (!isLightOn) {
cout << "The light is OFF" << endl;
}