Professional Documents
Culture Documents
else
1 of 8
http://www.programiz.com/cpp-programming/if-else
'SRXIRXW
MJ7XEXIQ IRX
MJIPWI7XEXIQ IRX
2 IWXIHMJIPWI7XEXIQ IRX
' SRHMXMSREP3 TIVEXSV
7/4/2016 11:30 AM
2 of 8
http://www.programiz.com/cpp-programming/if-else
7/4/2016 11:30 AM
3 of 8
http://www.programiz.com/cpp-programming/if-else
#include <iostream>
using namespace std;
int main() {
int number;
cout<< "Enter an integer: ";
cin>> number;
if ( number > 0) { // Checking whether an integer is positi
cout << "You entered a positive integer: "<<number<<endl;
}
cout<<"This statement is always executed because it's outside
return 0;
}
3 YXTYX
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement
3 YXTYX
Enter a number: 5
This statement is always executed because it's outside if statement
8LIMJIPWII\IGYXIWFSH]SJMJ[ LIRXLIXIWXI\TVIWWMSRMWXVYIERHI\IGYXIWXLI
FSH]SJIPWIMJXIWXI\TVIWWMSRMWJEPWI
7/4/2016 11:30 AM
4 of 8
http://www.programiz.com/cpp-programming/if-else
7/4/2016 11:30 AM
5 of 8
http://www.programiz.com/cpp-programming/if-else
#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;
}
cout<<"This statement is always executed because it's outside
return 0;
7/4/2016 11:30 AM
6 of 8
http://www.programiz.com/cpp-programming/if-else
3 YXTYX
Enter an integer: 4
You entered a negative integer: 4
This statement is always executed because it's outside if...else st
2 IWXIHMJIPWIEVIYWIHMJXLIVIEVIQ SVIXLERSRIXIWXI\TVIWWMSR
if (test expression1){
statement/s to be executed if test expression1 is true;
}
else if(test expression2) {
statement/s to be executed if test expression1 is fals
}
else if (test expression 3) {
statement/s to be executed if text expression1 and 2 ar
}
.
.
.
else {
statements to be executed if all test expressions a
}
7/4/2016 11:30 AM
7 of 8
http://www.programiz.com/cpp-programming/if-else
#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 if (number < 0){
cout<<"You entered a negative integer: "<<number<<endl;
}
else {
cout<<"You entered 0."<<endl;
}
cout<<"This statement is always executed because it's outside
return 0;
3 YXTYX
Enter an integer: 0
You entered 0.
This statement is always executed because it's outside nested if..e
if ( a < b ) {
a = b;
}
else {
a = b;
7/4/2016 11:30 AM
8 of 8
http://www.programiz.com/cpp-programming/if-else
8LIEFSZIGSHIGERFI[ VMXXIRYWMRKGSRHMXMSREPSTIVEXSVEW
a = (a < b) ? b : b;
&SXLGSHIWEFSZIGLIGO[ LIXLIVEMWPIWWXLERFSVRSX-JEMWPIWWXLERFZEPYI
SJFMWEWWMKRIHXSEMJRSXFMWEWWMKRIHXSE
7/4/2016 11:30 AM