0% found this document useful (0 votes)
7 views

C++ Lecture 3

C++ conditional statements like if, if-else, and nested if-else are used to execute code based on certain conditions being true or false. The document discusses the syntax of these statements and provides examples of using if to check if a number is positive, if-else to check if a number is positive or negative, and nested if-else to check if a number is positive, negative, or zero.

Uploaded by

Mahdi Last
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)
7 views

C++ Lecture 3

C++ conditional statements like if, if-else, and nested if-else are used to execute code based on certain conditions being true or false. The document discusses the syntax of these statements and provides examples of using if to check if a number is positive, if-else to check if a number is positive or negative, and nested if-else to check if a number is positive, negative, or zero.

Uploaded by

Mahdi Last
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/ 8

C++ PROGRAMMING

Conditional Statements

Dr. Hayder Kareem


2023-2024
C++ Programming Dr. Hayder Kareem
Conditional Statements

C++ if-else
In C++ programming, if statement is used to test the condition. There are various
types of if statements in C++.
1. if statement
2. if-else statement
3. nested if statement
Conditional Statements are used in C++ to run a certain piece of program only if a
specific condition is met.

1. If Statement:
The C++ if statement tests the condition. It is executed if condition is true.
Syntax:

if (Boolean_expression or Condition)
{ // statement(s) will execute if the Boolean expression(Condition) is true }

{1}
C++ Programming Dr. Hayder Kareem
Conditional Statements

Example: Program to print positive number entered by the user

{2}
C++ Programming Dr. Hayder Kareem
Conditional Statements
2. IF-else Statement
The C++ if-else statement also tests the condition. It executes if block if condition
is true otherwise else block is executed.
Syntax:

if (Boolean expression or Condition)


{ // statement(s) will execute if the Boolean expression(Condition) is true }
else
{ // statement(s) will execute if the boolean expression (Condition) is false }

{3}
C++ Programming Dr. Hayder Kareem
Conditional Statements

Example: Program to check whether an integer is positive or negative

3. if...else if...else Statement (Nested If)


An if statement can be followed by an optional else if...else statement, which is very
useful to test various conditions using single if...else if statement.

{4}
C++ Programming Dr. Hayder Kareem
Conditional Statements
When using if , else if , else statements there are few points to keep in mind.

• An if can have zero or one else's and it must come after any else if's.
• An if can have zero to many else if's and they must come before the else.
• Once an else if succeeds, none of the remaining else if's or else's will be
tested.
Syntax:
if (Boolean expression 1)
{
// Executes when the Boolean expression 1 is true
}
else if(Boolean expression 2)
{
// Executes when the Boolean expression 2 is true
}
else if( Boolean expression 3)
{
// Executes when the Boolean expression 3 is true
} else
{
// executes when the none of the above condition is true }

{5}
C++ Programming Dr. Hayder Kareem
Conditional Statements

{6}
C++ Programming Dr. Hayder Kareem
Conditional Statements
Example: Program to check whether an integer is positive, negative or zero

{7}

You might also like