0% found this document useful (0 votes)
13 views18 pages

2 Conditionals

The document is a lesson plan covering various conditional statements in programming, including if statements, if-else statements, nested if-else, conditional operators, and switch statements. It provides syntax, examples, and outputs for each type of statement, along with practical programming exercises to reinforce the concepts. Additionally, it includes sample code for determining even/odd numbers, divisibility checks, and grading based on student percentages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

2 Conditionals

The document is a lesson plan covering various conditional statements in programming, including if statements, if-else statements, nested if-else, conditional operators, and switch statements. It provides syntax, examples, and outputs for each type of statement, along with practical programming exercises to reinforce the concepts. Additionally, it includes sample code for determining even/odd numbers, divisibility checks, and grading based on student percentages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lesson Plan

Conditionals
Topics to be covered:

If statemen

If-else statemen

Nested if-else statemen

Conditional operator

Switch statement

If statement

An if statement is the most common conditional statement. It executes when the condition being

checked evaluates to true.

Syntax:

if(condition) {

//statements

Example:

if(x > 20) {

cout << "Value of x is greater than 20\n";

Output:

CASE 1: Let value of x be 30

Value of x is greater than 20

CASE 2: Let value of x be 10

No output in this case

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

of the statement is executed.

Syntax:

if(condition) {

//statements

} else {

// statements

Example:

if(x > 20) {

cout << "Value of x is greater than 20\n";

} else {

cout << "Value of x is less than "

20

Java
C++ &+ DSA
DSA
Output:

CASE 1: Let value of x be 30

Value of x is greater than 20

CASE 2: Let value of x be 10

Value of x is less than 20

Nested if-else

It is simply an if-else statement inside another if-else statement.

Syntax:

if (condition - 1)

if (condition - 2)

//statements

else

//statements

else

//statements

Example:

cout << "Value of x is "

if (x > 20)

if(x > 100)

cout << “very much”;

cout << “greater\n”;

else

cout << “smaller\n”;

Java
C++ &+ DSA
DSA
Output:

CASE 1: Let value of x be 150

Value of x is very much greater than 20

CASE 2: Let value of x be 50

Value of x is greater than 20

CASE 3: Let value of x be 10

Value of x is less than 20

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:

if (val < 10 || val > 20)

cout << "Value is either greater than 20 or less than 10."

Case 1: val = 3

Output: Value is either greater than 20 or less than 10.

Case 2: val = 40

Output: Value is either greater than 20 or less than 10.

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

statement - 1 is executed else the statement - 2 is executed.

Syntax: condition ? statement - 1 : statement - 2;

Java
C++ &+ DSA
DSA
Example:

//Without ternary operator

if (val == 10)

cout << "The current value is 10\n";

else

cout << "The current value is not 10\n";

//With ternary operator

val == 10 ? cout << "The current value is 10\n" : cout << "The

current value is

not 10\n";

Case 1: val = 10

Output: The current value is 10

Case 2: val = 20

Output: The current value is not 10

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

variable with different values.

Syntax:

switch (expression)

case x:

// code

break;

case y:

// code

break;

default:

// code

Java
C++ &+ DSA
DSA
Example:

switch (ch) {

case ‘a’:

cout << “Vowel\n”;

break;

case ‘e’:

cout << “Vowel\n”;

break;

case ‘i’:

cout << “Vowel\n”;

break;

case ‘o’:

cout << “Vowel\n”;

break;

case ‘u’:

cout << “Vowel\n”;

break;

default:

cout << “Consonant\n”;

Output:

Case 1: ch = ‘a’

Output: Vowel

Case 2: ch = ‘x’

Output: Consonant

Q1. Take positive integer input and tell if it is even or odd

#include <iostream>

int main() {

int number;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive

if (number > 0) {

// Check if the input is even or odd

if (number % 2 == 0) {

std::cout << number << " is even." << std::endl;

} else {

Java
C++ &+ DSA
DSA
std::cout << number << " is odd." << std::endl;

} else {

std::cout << "Please enter a positive integer." <<


std::endl;

return 0;

Q2. Take positive integer input and tell if it is divisible by 5 or not.

#include <iostream>

int main() {

int number;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive

if (number > 0) {

// Check if the input is divisible by 5

if (number % 5 == 0) {

std::cout << number << " is divisible by 5." <<


std::endl;

} else {

std::cout << number << " is not divisible by 5." <<


std::endl;

} else {

std::cout << "Please enter a positive integer." <<


std::endl;

return 0;

Q3. Given an integer. Print the absolute value of that integer.

#include <iostream>

int main() {

int number;

Java
C++ &+ DSA
DSA
// Input an integer

std::cout << "Enter an integer: ";

std::cin >> number;

// Calculate and print the absolute value

int absoluteValue = (number < 0) ? -number : number;

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() {

float costPrice, sellingPrice;

// Input cost price and selling price

std::cout << "Enter the cost price: ";

std::cin >> costPrice;

std::cout << "Enter the selling price: ";

std::cin >> sellingPrice;

// Determine profit or loss and calculate the amount

float profitOrLoss = sellingPrice - costPrice;

if (profitOrLoss > 0) {

std::cout << "Profit of " << profitOrLoss << " units." <<
std::endl;

} else if (profitOrLoss < 0) {

std::cout << "Loss of " << -profitOrLoss << " units." <<
std::endl;

} else {

std::cout << "No profit, no loss." << std::endl;

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;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive and a three-digit number

if (number > 0 && number >= 100 && number <= 999) {

std::cout << number << " is a three-digit number." <<


std::endl;

} else {

std::cout << number << " is not a three-digit number." <<


std::endl;

return 0;

Q6. Take positive integer input and tell if it is divisible by 5 and 3.

#include <iostream>

int main() {

int number;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive and divisible by both 5 and


3

if (number > 0 && number % 5 == 0 && number % 3 == 0) {

std::cout << number << " is divisible by both 5 and 3."


<< std::endl;

} else {

std::cout << number << " is not divisible by both 5 and


3." << std::endl;

return 0;

Q7. Take positive integer input and tell if it is divisible by 5 or 3.

Java
C++ &+ DSA
DSA
#include <iostream>

int main() {

int number;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive and divisible by either 5


or 3

if (number > 0 && (number % 5 == 0 || number % 3 == 0)) {

std::cout << number << " is divisible by either 5 or 3."


<< std::endl;

} else {

std::cout << number << " is not divisible by either 5 or


3." << std::endl;

return 0;

Q8. Take 3 positive integers input and print the greatest of them.
#include <iostream>

int main() {

int num1, num2, num3;

// Input three positive integers

std::cout << "Enter the first positive integer: ";

std::cin >> num1;

std::cout << "Enter the second positive integer: ";

std::cin >> num2;

std::cout << "Enter the third positive integer: ";

std::cin >> num3;

// Check if the inputs are positive and determine the


greatest

if (num1 > 0 && num2 > 0 && num3 > 0) {

int greatest = (num1 > num2) ? (num1 > num3 ? num1 :


num3) : (num2 > num3 ? num2 : num3);

std::cout << "The greatest number is: " << greatest <<
std::endl;

} else {

Java
C++ &+ DSA
std::cout << "Please enter positive integers." <<
std::endl;

return 0;

Q9. Write a program to check whether a character is an alphabet or not.

#include <iostream>

int main() {

char character;

// Input a character

std::cout << "Enter a character: ";

std::cin >> character;

// Check if the character is an alphabet

if ((character >= 'a' && character <= 'z') || (character >=


'A' && character <= 'Z')) {

std::cout << character << " is an alphabet." <<


std::endl;

} else {

std::cout << character << " is not an alphabet." <<


std::endl;

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;

// Input a positive integer

std::cout << "Enter a positive integer: ";

std::cin >> number;

// Check if the input is positive and divisible by either 5


or 3 but not by 15

if (number > 0 && be "==" and "!=" {

Java
C++ &+ DSA
DSA
std::cout << number << " is divisible by either 5 or 3

but not by 15." << std::endl;

} else {

std::cout << number << " is not divisible by either 5 or

3 or is divisible by 15." << std::endl;

return 0;

Q11. Take input percentage of a student and print the Grade according to marks:

91-100 Excellen

81-90 Very Goo

71-80 Goo

61-70 Can do bette

51-60 Averag

40-50 Below Averag

<40 Fail

#include <iostream>

int main() {

double percentage;

// Input the percentage

std::cout << "Enter the percentage of the student: ";

std::cin >> percentage;

// Determine the grade based on the percentage

if (percentage >= 91 && percentage <= 100) {

std::cout << "Grade: Excellent" << std::endl;

} else if (percentage >= 81 && percentage <= 90) {

std::cout << "Grade: Very Good" << std::endl;

} else if (percentage >= 71 && percentage <= 80) {

std::cout << "Grade: Good" << std::endl;

} else if (percentage >= 61 && percentage <= 70) {

std::cout << "Grade: Can do better" << std::endl;

} else if (percentage >= 51 && percentage <= 60) {

std::cout << "Grade: Average" << std::endl;

} else if (percentage >= 40 && percentage <= 50) {

std::cout << "Grade: Below Average" << std::endl;

} else if (percentage < 40) {

std::cout << "Grade: Fail" << std::endl;

} else {

Java
C++ &+ DSA
DSA
std::cout << "Invalid percentage entered." << std::endl;

return 0;

Q12. Write a program to check whether a given character is a vowel or a consonant.

#include <iostream>

int main() {

char character;

// Input a character

std::cout << "Enter a character: ";

std::cin >> character;

// Convert the character to lowercase for simplicity

char lowercaseChar = tolower(character);

// Check if the character is a vowel or a consonant

if (lowercaseChar >= 'a' && lowercaseChar <= 'z') {

if (lowercaseChar == 'a' || lowercaseChar == 'e' ||

lowercaseChar == 'i' || lowercaseChar == 'o' || lowercaseChar ==

'u') {

std::cout << character << " is a vowel." <<

std::endl;

} else {

std::cout << character << " is a consonant." <<

std::endl;

} else {

std::cout << character << " is not a valid alphabet

character." << std::endl;

return 0;

Q13. Take 3 numbers input and tell if they can be the sides of a triangle.

#include <iostream>

int main() {

double side1, side2, side3;

Java
C++ &+ DSA
DSA
// Input three numbers

std::cout << "Enter the length of side 1: ";

std::cin >> side1;

std::cout << "Enter the length of side 2: ";

std::cin >> side2;

std::cout << "Enter the length of side 3: ";

std::cin >> side3;

// Check if the numbers can be sides of a triangle

if (side1 + side2 > side3 && side2 + side3 > side1 && side3 +

side1 > side2) {

std::cout << "These lengths can form the sides of a

triangle." << std::endl;

} else {

std::cout << "These lengths cannot form the sides of a

triangle." << std::endl;

return 0;

Q14. Given the marks of the student. If the marks are greater than 33 print the result as pass otherwise fail

without using if-else statement.

#include <iostream>

int main() {

int marks;

// Input the marks

std::cout << "Enter the marks: ";

std::cin >> marks;

// Check and print the result without using if-else

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;

// Input week number

std::cout << "Enter the week number (1-7): ";

std::cin >> weekNumber;

// Print day of the week using switch-case

switch (weekNumber) {

case 1:

std::cout << "Monday" << std::endl;

break;

case 2:

std::cout << "Tuesday" << std::endl;

break;

case 3:

std::cout << "Wednesday" << std::endl;

break;

case 4:

std::cout << "Thursday" << std::endl;

break;

case 5:

std::cout << "Friday" << std::endl;

break;

case 6:

std::cout << "Saturday" << std::endl;

break;

case 7:

std::cout << "Sunday" << std::endl;

break;

default:

std::cout << "Invalid week number. Please enter a


number between 1 and 7." << std::endl;

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;

// Input month number

std::cout << "Enter the month number (1-12): ";

std::cin >> monthNumber;

Java
C++ &+ DSA
DSA
// Print total number of days in the month using switch-case

switch (monthNumber) {

case 1: case 3: case 5: case 7: case 8: case 10: case 12:

std::cout << "Total number of days in the month: 31"


<< std::endl;

break;

case 4: case 6: case 9: case 11:

std::cout << "Total number of days in the month: 30"


<< std::endl;

break;

case 2:

std::cout << "Total number of days in the month: 28


or 29 (leap year)" << std::endl;

break;

default:

std::cout << "Invalid month number. Please enter a


number between 1 and 12." << std::endl;

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>

// Function to perform addition

double add(double num1, double num2) {

return num1 + num2;

// Function to perform subtraction

double subtract(double num1, double num2) {

return num1 - num2;

// Function to perform multiplication

double multiply(double num1, double num2) {

return num1 * num2;

// Function to perform division

double divide(double num1, double num2) {

// Check for division by zero

if != {

return num1 / num2;

Java
C++ &+ DSA
DSA
} else {

std::cout << "Error: Division by zero is not allowed." <<


std::endl;

return 0.0; // Returning 0 in case of division by zero

int main() {

double num1, num2;

char operation;

// Input two numbers and an operator

std::cout << "Enter the first number: ";

std::cin >> num1;

std::cout << "Enter the second number: ";

std::cin >> num2;

std::cout << "Enter the operator (+, -, *, /): ";

std::cin >> operation;

// Perform the operation using switch-case

double result;

switch (operation) {

case '+':

result = add(num1, num2);

break;

case '-':

result = subtract(num1, num2);

break;

case '*':

result = multiply(num1, num2);

break;

case '/':

result = divide(num1, num2);

break;

default:

std::cout << "Invalid operator. Please enter a valid


operator." << std::endl;

return 1; // Return with an error code

// Print the result

std::cout << "Result: " << result << std::endl;

return 0;

Java
C++ &+ DSA
DSA
THANK

YOU !

You might also like