else if vs if

Having trouble.

Whats the difference between "if" and "else if"?

If I'm going to make a "nested if statement" does it affect the usage of "if" and "else if"?

cause here in my code(not yet finished):
(I'm trying to make a program that will determine your zodiac sign)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include <iostream>
using namespace std;
int main ()
{
char a,b,c,d,e,f,g,h,i,j,k,l,m;
int n;
	
cout<<"Enter Your Birth Month. Press "<<endl;
cout<<"a= January"<<endl;
cout<<"b= February"<<endl;
cout<<"c= March"<<endl;
cout<<"d= April"<<endl;
cout<<"e= May"<<endl;
cout<<"f= June"<<endl;
cout<<"g= July"<<endl;
cout<<"h= August"<<endl;
cout<<"i= September"<<endl;
cout<<"j= October"<<endl;
cout<<"k= November"<<endl;
cout<<"l= December"<<endl;
cin>>m;
cout<<"Enter your Birth Date (1-31)"<<endl;
cin>>n;
	
if (m=='a') 
{
if ((n<=19)&&(n>=1))
cout<<"Your zodiac sign is Capricorn.";
}
else if (m=='l') 
{
if ((n<=31)&&(n>=22))
cout<<"Your zodiac sign is Capricorn.";
}

else if (m=='a') 
{
if ((n<=31)&&(n>=20))
cout<<"Your zodiac sign is Aquarius.";
}
else if (m=='b') 
{
if ((n<=18)&&(n>=1))
cout<<"Your zodiac sign is Aquarius.";
}
else if (m=='b') 
{
if ((n<=29)&&(n>=19))
{
cout<<"Your zodiac sign is Pisces.";
}
}
return 0;
}



I'm having trouble with the underlined statement above.
If the statement will remain like that, the "Aquarius" word will not be printed out.
But if I'm going to remove "else" in "else if (m=='a')", the "Aquarius" word will be printed out.

Why is that happening?

Does that mean that "if" and "else if" must be alternately written?

And if I'm going to write "else if" consecutively,then it means it is considered wrong?

Help pls..

Last edited on
'else' must come after an if block. It means the else block is only executed if the previous if block was not executed.

'else if' is just an else immediately followed by an if... so the else if condition will only be executed if the previous if condition was false.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
if( foo )
{
    // <- this block is only executed if 'foo' is true
}
else if( bar )  // <- 'bar' is only checked if 'foo' is false
{
    // <- this block only executed if 'foo' is false and 'bar' is true
}
else
{
    // <- this block only executed if 'foo' and 'bar' are both false
}
if( baz ) // <- no 'else', so previous 'ifs' don't matter
{
    // <- this block only executed if 'baz' is true.   foo/bar don't matter
}


If the statement will remain like that, the "Aquarius" word will not be printed out.
But if I'm going to remove "else" in "else if (m=='a')", the "Aquarius" word will be printed out.


if m=='a', then the if statement on line 25 will be true, meaning that if block will execute... and no following elses will execute.

So your if condition on line 36 will never be true... because if it were true, that 'else' will skip it because the if condition above it would have executed instead.
Thanks a lot.
Also note that a little whitespace and proper indentation would help make this program readable.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
   else if (m=='l')
   {
      if ((n<=31)&&(n>=22))
         cout<<"Your zodiac sign is Capricorn.";
   }
   else if (m=='a')
   {
      if ((n <= 31) && (n >= 20))
         cout<<"Your zodiac sign is Aquarius.";
   }
   else if (m == 'b')
   {
      if ((n <= 18) && (n >= 1))
         cout << "Your zodiac sign is Aquarius.";
   }


And since m is a char, have you considered a switch statement instead of the if/else if clauses?

Last edited on
nope, I don't know how to use switch statement. I used if-else statement that's why my program is very long.
Thanks.
Topic archived. No new replies allowed.