0% found this document useful (0 votes)
97 views10 pages

If Else Statement

This document discusses if and if else statements in C++. It provides the syntax for if statements, which execute code if a test expression is true. It also explains how if else statements work, with the code in the else block executing if the test expression is false. An example if else program is given that checks if a user-entered integer is positive or negative.

Uploaded by

prince12
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)
97 views10 pages

If Else Statement

This document discusses if and if else statements in C++. It provides the syntax for if statements, which execute code if a test expression is true. It also explains how if else statements work, with the code in the else block executing if the test expression is false. An example if else program is given that checks if a user-entered integer is positive or negative.

Uploaded by

prince12
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/ 10

If else statement in c++

University of Central Punjab


FOIT
Lahore
Contents:-

 Introduction to -If statement(syntax,working,example)


 Introduction to -If else statement(working,example)
If statement
(syntax):

if (testExpression)
{
// statements
}
How if statement works?
Sample of if program
#include <iostream>
using namespace std;
int main()
{
int number;
cout << "Enter an integer: ";
cin >> number;
if ( number > 0)
{
cout << "You entered a positive integer: " << number ;
}
return 0;
}
Syntax

if (testExpression1)
{
// statements to be executed if test expression1 is true ;
}
else
{
// statement to be execute if test expression is false;
}
C++ if...else working

The if else executes the codes inside the body of if statement,


if the test expression is true it skips the codes inside the body of else.

If expression is false it will skips code inside if and execute according to


Else statement.
C++ if...else working
Example

#include <iostream>
using namespace std;
int main()
{
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; }
return 0;
}
 Thank you

You might also like