2 Conditionals
2 Conditionals
Conditionals
Topics to be covered:
If statemen
If-else statemen
Conditional operator
Switch statement
If statement
An if statement is the most common conditional statement. It executes when the condition being
Syntax:
if(condition) {
//statements
Example:
Output:
If-else statement
An if-else statement is designed to give us this functionality in our code. It executes statements if
some condition is true or false. If the condition is true, the if part is executed, otherwise the else part
Syntax:
if(condition) {
//statements
} else {
// statements
Example:
} else {
20
Java
C++ &+ DSA
DSA
Output:
Nested if-else
Syntax:
if (condition - 1)
if (condition - 2)
//statements
else
//statements
else
//statements
Example:
if (x > 20)
else
Java
C++ &+ DSA
DSA
Output:
Conditional operators
These operators are used when a condition comprises of more than one Boolean expression/ condition
check.
We have following types of conditional operators - logical-and, logical-or and ternary operator.
Syntax:
if(condition - 1 || condition - 2)
statement;
Example:
Case 1: val = 3
Case 2: val = 40
Case 3: val = 11
Output: No output
Ternary operator (?:) : It is a smaller version for the if-else statement. If the condition is true then
Java
C++ &+ DSA
DSA
Example:
if (val == 10)
else
val == 10 ? cout << "The current value is 10\n" : cout << "The
current value is
not 10\n";
Case 1: val = 10
Case 2: val = 20
NOTE : “and” can also be written instead of “&&” and “or” can also be written as “||”.
Switch statement
Switch Statement is like an if-else ladder with multiple conditions, where we check for equality of a
Syntax:
switch (expression)
case x:
// code
break;
case y:
// code
break;
default:
// code
Java
C++ &+ DSA
DSA
Example:
switch (ch) {
case ‘a’:
break;
case ‘e’:
break;
case ‘i’:
break;
case ‘o’:
break;
case ‘u’:
break;
default:
Output:
Case 1: ch = ‘a’
Output: Vowel
Case 2: ch = ‘x’
Output: Consonant
#include <iostream>
int main() {
int number;
if (number > 0) {
if (number % 2 == 0) {
} else {
Java
C++ &+ DSA
DSA
std::cout << number << " is odd." << std::endl;
} else {
return 0;
#include <iostream>
int main() {
int number;
if (number > 0) {
if (number % 5 == 0) {
} else {
} else {
return 0;
#include <iostream>
int main() {
int number;
Java
C++ &+ DSA
DSA
// Input an integer
std::cout << "The absolute value of " << number << " is: " <<
absoluteValue << std::endl;
return 0;
Q4. If cost price and selling price of an item is input through the keyboard, write a program to determine
whether the seller has made profit or incurred loss. Also determine how much profit he made or loss he
incurred.
#include <iostream>
int main() {
if (profitOrLoss > 0) {
std::cout << "Profit of " << profitOrLoss << " units." <<
std::endl;
std::cout << "Loss of " << -profitOrLoss << " units." <<
std::endl;
} else {
return 0;
Q5. Take positive integer input and tell if it is a three digit number or not.
Java
C++ &+ DSA
DSA
#include <iostream>
int main() {
int number;
if (number > 0 && number >= 100 && number <= 999) {
} else {
return 0;
#include <iostream>
int main() {
int number;
} else {
return 0;
Java
C++ &+ DSA
DSA
#include <iostream>
int main() {
int number;
} else {
return 0;
Q8. Take 3 positive integers input and print the greatest of them.
#include <iostream>
int main() {
std::cout << "The greatest number is: " << greatest <<
std::endl;
} else {
Java
C++ &+ DSA
std::cout << "Please enter positive integers." <<
std::endl;
return 0;
#include <iostream>
int main() {
char character;
// Input a character
} else {
return 0;
Q10. Take positive integer input and tell if it is divisible by 5 or 3 but not divisible by 15.
#include <iostream>
int main() {
int number;
Java
C++ &+ DSA
DSA
std::cout << number << " is divisible by either 5 or 3
} else {
return 0;
Q11. Take input percentage of a student and print the Grade according to marks:
91-100 Excellen
71-80 Goo
51-60 Averag
<40 Fail
#include <iostream>
int main() {
double percentage;
} else {
Java
C++ &+ DSA
DSA
std::cout << "Invalid percentage entered." << std::endl;
return 0;
#include <iostream>
int main() {
char character;
// Input a character
'u') {
std::endl;
} else {
std::endl;
} else {
return 0;
Q13. Take 3 numbers input and tell if they can be the sides of a triangle.
#include <iostream>
int main() {
Java
C++ &+ DSA
DSA
// Input three numbers
if (side1 + side2 > side3 && side2 + side3 > side1 && side3 +
} else {
return 0;
Q14. Given the marks of the student. If the marks are greater than 33 print the result as pass otherwise fail
#include <iostream>
int main() {
int marks;
std::cout << "Result: " << ((marks > 33) ? "Pass" : "Fail")
<< std::endl;
return 0;
Q15. Write a program to input week number(1-7) and print day of week name using switch case.
Java
C++ &+ DSA
DSA
#include <iostream>
int main() {
int weekNumber;
switch (weekNumber) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
case 7:
break;
default:
return 0;
Q16. Write a program to input month number and print total number of days in month using switch case.
#include <iostream>
int main() {
int monthNumber;
Java
C++ &+ DSA
DSA
// Print total number of days in the month using switch-case
switch (monthNumber) {
break;
break;
case 2:
break;
default:
return 0;
Q17. Write a program to create a calculator that performs basic arithmetic operations (add, subtract,
multiply and divide) using switch case and functions. The calculator should input two numbers and an
operator from user.
#include <iostream>
if != {
Java
C++ &+ DSA
DSA
} else {
int main() {
char operation;
double result;
switch (operation) {
case '+':
break;
case '-':
break;
case '*':
break;
case '/':
break;
default:
return 0;
Java
C++ &+ DSA
DSA
THANK
YOU !