about the NESTED IF STATEMENT,


#include <iostream.h>
int main()
{
int N1, N2;
cout<<"enter value for N1\n";
cin>>N1;
cout<<"enter value for N2\n";
cin>>N2;
if(N1>=N2)
{
if(N1>N2)
cout<<"N1 is greater than N2\n";
else
{
if(N1=N2)
cout<<"N1 is equal to N2\n";
else
cout<<"N2 is greater than N1\n";
}
}
return 0;
}


IS THIS CORRECT USING THE NESTED IF STATEMENT?
IS THIS CORRECT NOT USING CAPSLOCK OR THE 'INSERT CODE' BUTTON!?

You shouldn't abuse nested if-statements like that. (Or your CapsLock key, but that's neither here nor there.)

You can use 'else if' directly after an 'if' block so that you don't have to have silly nesting going on.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
using namespace std;

int main () {
	int a, b;

	cout << "a = ";
	cin >> a;

	cout << "b = ";
	cin >> b;

	if (a > b) cout << "a is bigger." << endl;
	else if (b > a) cout << "b is bigger." << endl;
	else if (a == b) cout << "you win, they're the same." << endl;

	return 0;
}
Topic archived. No new replies allowed.