Introduction To Programming C++ Practices
Introduction To Programming C++ Practices
EDUCATION
RANA UNIVERSITY
FACULTY OF COMPUTER SCIENCE
DEPARTMENT
Introduction to
Programming
1st SEMESTER
2024
Contents
Overview ........................................................................................................ Error! Bookmark not defined.
Introduction
In C++, conditional statements enable the program to make decisions and execute specific code
based on certain conditions. These statements control the flow of the program by allowing it to
take different paths depending on whether particular conditions are true or false. The three main
conditional statements in C++ are if, else, and else if.
1. The if Statement
• The if statement is the most basic form of a conditional statement. It checks a specified
condition, and if that condition is true, the code within its block will execute. If the
condition is false, the program skips this block of code.
• Example: Checking if a number is positive:
• Here, if number is not greater than 0, the program will print that the number is negative.
3. The else if Statement
• The else if statement allows for multiple conditions to be checked in sequence. After
the initial if statement, you can use one or more else if statements to test additional
conditions. If none of the conditions are true, an optional else can provide a final path
of execution.
• Example: Checking different age categories:
• In this example, the program checks each condition in sequence, printing "Adult" for
ages between 20 and 64.
Summary
Using if, else, and else if statements, C++ programs can evaluate conditions and determine
which code blocks to execute, allowing for flexibility in how the program responds to
different inputs and scenarios.
To help illustrate how if, else if, and else conditions work in C++, here are a few examples
to get started. I provided the structure for each example, including the code, output, and
explanation, so you can apply it across additional cases.
Example 1: Check if a Number is Positive, Negative, or Zero
Explanation:
▪ The program takes an integer as input.
▪ It checks three conditions using if, else if, and else:
• If number is greater than 0, it outputs "The number is positive."
• If number is less than 0, it outputs "The number is negative."
• If neither of the above conditions is true (meaning number is 0), it outputs
"The number is zero."
Sample Output:
Explanation:
▪ The program takes an integer input.
▪ It checks whether the number is even or odd by using the modulo operator %:
• If number % 2 == 0, it outputs "The number is even."
• Otherwise, it outputs "The number is odd."
Sample Output:
Explanation:
▪ The program takes an integer input.
▪ It checks multiple conditions to see if the number is divisible by both 3 and 5, by
only 3, or by only 5:
• If number is divisible by both, it outputs "The number is divisible by both 3
and 5."
• If number is divisible only by 3, it outputs "The number is divisible by 3."
• If number is divisible only by 5, it outputs "The number is divisible by 5."
• If none of the above conditions is true, it outputs "The number is not divisible
by 3 or 5."
Sample Output: