C++ if...else and Nested if..
description
Transcript of C++ if...else and Nested if..
��������������� �� ������������������ � ��������������� ����� ����� ��������� �
� ������������ � � �������������������������� ���������� ���� ������� ��������
���� �� ����� ������������ ����� �� �������������� �� ��� �������������� ��� ���
��� ����� � ��!�� ���� ��"������ �� ��� ������ ��� ����� ��������������!��
������� � � ��� ��������� ������������� � � ��#������ ������ �������� �� ��� ��
�����������!��$������ ������ �� ����������� �����������
�� �� ��
���%����� � �
������%����� � �
&������������%����� � �
�� ����� ��'�������
������������� � ���������� ������������������ ����� ������������ ���(����������
�� ����� ���������������������������������� ���������$�� ������������� � ��#���������
������� ����� ������������������������������� ���������$�� ������������� � �
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
1 of 8 7/4/2016 11:30 AM
��������� � ����������� ���$ �������� ����� �� ��������� �������)�*�(����������
�� ����� ��������������������� �������� �$�������������������$������������� ����� ���
����������������� �������� �$�������+�,������������� ���� �������������� �����
-����$��� �����$�� �������������� �� ���������$�!�
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
2 of 8 7/4/2016 11:30 AM
.
����������� ������� ��� ������� ������$ ������� ��������� �� $������������!�
#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;
}
'������/
Enter an integer: 5
You entered a positive number: 5
This statement is always executed because it's outside if statement
'������0
Enter a number: ‐5
This statement is always executed because it's outside if statement
�������������������$�� �������� �� ������������������� ���������� ��������������
$�� ������������������������� ��������
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
3 of 8 7/4/2016 11:30 AM
������������� � ���������� ������������������������� ������������ ���(����������
�� ����� ���������������������������������� ���������$�� ������������� � ��#���������
������� ����� ���������������������������������� ���������$�� �������
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
4 of 8 7/4/2016 11:30 AM
����������� ����������� �������� ������� ������$ ���������������!����� �����!�
)�� ������ ��1����������!�*
#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;
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
5 of 8 7/4/2016 11:30 AM
}
'�����
Enter an integer: ‐4
You entered a negative integer: ‐4
This statement is always executed because it's outside if...else st
&����������������������������������� ������� �� ����������������
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
}
���� ������������������ � ������� ������� �� ���������������� �(����������������
��������� �������������������������������� ���������$�����+�,�-����$��� ����#�����
������������������������ ���������������������������� ���������������� �(�����
���� ���������������� �������������������������������� ���������$�����+�,�-����$���
������������������ �� ����(���������������������� ������������������� �����������
���������� �������� �������������� �-�� ���$��� ����� �����������
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
6 of 8 7/4/2016 11:30 AM
����������� ����������� ������������ ������� ������$ ���������������!��� �����!�
���2���
#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; }
'�����
Enter an integer: 0
You entered 0.
This statement is always executed because it's outside nested if..e
�� ����� ������������������������������������������������ � ��� ����
������� � � ���� ����������������������� � �3
if ( a < b ) {
a = b;
}
else {
a = ‐b;
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
7 of 8 7/4/2016 11:30 AM
}
�����$�!��������� �$��� ����� ���� ���� ����� �������������3
a = (a < b) ? b : ‐b;
#�����������$�!��������� ������������������� �$���� ���(�������������� �$��!���
���$��������� ����������� �����$��������� �������
��� ������4 �$ �������� �2�5�6�������������!���5����!�� �����
C++ if...else and Nested if...else http://www.programiz.com/cpp-programming/if-else
8 of 8 7/4/2016 11:30 AM