Beruflich Dokumente
Kultur Dokumente
C++ if Statement
C++ if...else
C++ Nested if...else
11/10/2019
by
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
SHEHZAD LATIF
Assistant Professor,
Hajvery University – Lahore
Email: Shehzadch49@yahoo.Com
1
C++ if Statement
Syntax:
if (testExpression)
11/10/2019
{
// statements
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
}
Programming Fundamentals by
11/10/2019
3
Programming Fundamentals by
11/10/2019
4
11/10/2019
#include <iostream>
using namespace std;
int main()
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
{
int number;
cout << "Enter an integer: ";
cin >> number;
// checks if the number is positive
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0; 5
}
Output:
11/10/2019
Enter an integer: 5
You entered a positive number: 5
This statement is always executed.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
6
C++ if...else
The if else executes the codes inside the body of if statement if the
11/10/2019
test expression is true and skips the codes inside the body of else.
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
If the test expression is false, it executes the codes inside the body
of else statement and skips the codes inside the body of if.
7
How if...else statement works?
Programming Fundamentals by
11/10/2019
8
Programming Fundamentals by
11/10/2019
9
#include <iostream>
using namespace std;
11/10/2019
int main()
{
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number >= 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else
{
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed."; 10
return 0;
}
Output
Enter an integer: -4
You entered a negative integer: -4.
Programming Fundamentals by
11/10/2019
11
11/10/2019
upon whether the test expression is true or false. Sometimes, a
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test
12
conditions.
Syntax of Nested if...else
if (testExpression1)
{
// statements to be executed if testExpression1 is true
11/10/2019
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and
testExpression2 is false and testExpression3 is true
}
.
else
{
// statements to be executed if all test expressions are false 13
}
Example 3: C++ Nested if...else
// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main()
11/10/2019
{
int number;
cout << "Enter an integer: ";
cin >> number;
Programming Fundamentals by
SHEHZAD LATIF (03134797617)
if ( number > 0)
{
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0)
{
cout<<"You entered a negative integer: " << number << endl;
}
else
{
cout << "You entered 0." << endl; 14
} cout << "This line is always printed.";
return 0;
}
Output
You entered 0.
Enter an integer: 0
Programming Fundamentals by
11/10/2019
15