[go: up one dir, main page]

0% found this document useful (0 votes)
2 views7 pages

Introduction To Programming C++ Practices

Introduction to programming C++ Practices for beginners

Uploaded by

Fazil Nasrat
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)
2 views7 pages

Introduction To Programming C++ Practices

Introduction to programming C++ Practices for beginners

Uploaded by

Fazil Nasrat
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/ 7

MINISTRY OF HIGHER

EDUCATION
RANA UNIVERSITY
FACULTY OF COMPUTER SCIENCE
DEPARTMENT

Introduction to
Programming
1st SEMESTER
2024

Prepared by: Fazil Ahmad “Nusrat”


Digital Logic Design

Contents
Overview ........................................................................................................ Error! Bookmark not defined.

FACULTY OF COMPUTER SCIENCE I


Introduction to Programming

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, the message will only be displayed if number is greater than 0.


2. The else Statement
• The else statement provides an alternative path of execution if the if condition is false.
It must follow an if statement and will execute its block only when the initial if
condition fails.
• Example: Checking if a number is positive or negative:

• 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

FACULTY OF COMPUTER SCIENCE 2


Introduction to Programming

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.

FACULTY OF COMPUTER SCIENCE 3


Introduction to Programming

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:

FACULTY OF COMPUTER SCIENCE 4


Introduction to Programming

Example 2: Check if a Number is Even or Odd

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:

FACULTY OF COMPUTER SCIENCE 5


Introduction to Programming

Example 3: Check if a Number is Divisible by 3 and 5

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:

FACULTY OF COMPUTER SCIENCE 6

You might also like