## paynito 2 years ago help with if/else if and nested if/else in C, so many ; and }

1. paynito

int doomsday(int month){ //returns the dooms date of the month as an int betw 1 and 31 //the evens //4of4,6of6,8of8,10of10,12of12 if (month ==4) {return 4;}; else if (month ==6 ){return 6;}; //June else if (month ==8 ){return 8;}; //Aug else if (month == 10){return 10;}; //Oct //the odds //9to5 at the 7-11 covers all but 1 and 3 else if (month ==1){ //jan 3rd except leapYear, then 4th if (leapYear==True){return 4}; else {return 3}; } ; else if (month==3){return 3;} ; //March 0 and march 7 are doomsday else if (month==5){return 9;}; //May else if (month==7) {return 11;}; //July else if (month==9){return 5;}; //Sept. else if (month==11){return 7;}; //Nov. // the FEBruary case else if (month==2){//leapYear effect in Feb if (leapYear ==True){return 29};//last day of the month for a leapYear else{return 28};}//last day of the month for a NON--leapYear else (month == 12){return 12;}; }; errors are . . . . \$ gcc -o new3 new3.c new3.c: In function ‘doomsday’: new3.c:17:1: error: ‘else’ without a previous ‘if’ new3.c:18:1: error: ‘else’ without a previous ‘if’ new3.c:19:1: error: ‘else’ without a previous ‘if’ new3.c:21:1: error: ‘else’ without a previous ‘if’ new3.c:22:6: error: ‘leapYear’ undeclared (first use in this function) new3.c:22:6: note: each undeclared identifier is reported only once for each function it appears in new3.c:22:16: error: ‘True’ undeclared (first use in this function) new3.c:22:30: error: expected ‘;’ before ‘}’ token new3.c:23:2: error: expected ‘}’ before ‘else’ new3.c:23:16: error: expected ‘;’ before ‘}’ token new3.c: At top level: new3.c:25:1: error: expected identifier or ‘(’ before ‘else’ new3.c:26:1: error: expected identifier or ‘(’ before ‘else’ new3.c:27:1: error: expected identifier or ‘(’ before ‘else’ new3.c:28:1: error: expected identifier or ‘(’ before ‘else’ new3.c:29:1: error: expected identifier or ‘(’ before ‘else’ new3.c:31:1: error: expected identifier or ‘(’ before ‘else’ new3.c:34:1: error: expected identifier or ‘(’ before ‘else’ new3.c:35:1: error: expected identifier or ‘(’ before ‘}’ token

2. rsmith6559

Let's go back to basics. In C syntax, which is also used by C++, Java, javascript and all the newer C-type languages, curly braces surround a block of code. Unless that block declares an object, no semi-colon is used with the curly braces. Semi-colons end a statement. One thing that will make life easier for you would be if you added indentation to your statements each time you have an opening curly brace. It makes it easier to match the open and close braces. The statements that an if statement executes need curly braces only if there's more than one statement. Now to confuse you a bit: if/else if/else isn't the best way to do an n-way branch. The switch statement is used for this: switch( month ) { case 4: return 4; break; case 6: return 6; break; default: fprintf( stderr, "Shucks" ); break; }

3. paynito

I really appreciate your help. I am in a CS 101 class and one rule is that we can't use structures we haven't learned in class yet, so switch isn't allowed. what does a properly formatted if/else if (nested if / else) / else look like?

4. shubha

simple if else : if (<condition>) { //do something } else { //do something else } nested if else : if (<condition1>) { //do something } else { //1st else //do something if ( <condition2>) { //do something } else { //2nd else //do something if (<condition3>) { //do something } else { //3rd else //do something . . . } //closing brace of 3rd else //do something } //closing brace of 2nd else //do something } //closing brace of 1st else hope this helps !

5. paynito

ok, i've got it down to just 2 errors \$ gcc -o new4 new4.c new4.c: In function ‘monthThing’: new4.c:25:22: error: ‘false’ undeclared (first use in this function) new4.c:25:22: note: each undeclared identifier is reported only once for each function it appears in #include <stdio.h> #include <stdlib.h> int leapYear= 0; int monthThing(int month); int main(int argc, char* argv[]){ int y; y=monthThing(1); printf("it's jan %d",y); return EXIT_SUCCESS; } int monthThing (int month){ //returns the doomsdate for each month as a date in integer 1-31 format int x; if (month==1) //january { if (leapYear ==false){x=3;}//jan 3 in most years else{x=4;}}//leap Jan else if(month==2){ //feb if (leapYear ==false){x=28;} //last day of feb else {x=29;} } else if(month==4 ||6 || 8 ||10){x=month;}//most of the evens else if(month==3){x=7;} // zeroth of march and 7th of march else if(month==5){x=9;}// I work 9 to 5 else if(month==7){x=11;}// at the 7-11 else if(month==9){x=5;} else if(month==11){x=7;} return x; }

6. paynito

thanks both of you, that does help to see the double }} after a nested if, I was adding all kinds of extra ; after the } and that seems to make the next else if kind of flail on its own and not be part of the sequence any more, now I'm having trouble if I am passing leapYear to the monthThing(month) I guess have leapYear as a global variable, is leapYear=True the same as leapYear =1 or anything else and leapYear=0 is the same as leapYear=False???

7. paynito

ok, this finally works, had to use 0's instead of false, since didn't #incl<stdbool.h so i''ll close it #include <stdio.h> #include <stdlib.h> int leapYear= 0; int monthThing(int month); int main(int argc, char* argv[]){ int y; y=monthThing(1); printf("it's jan %d",y); return EXIT_SUCCESS; } int monthThing (int month){ //returns the doomsdate for each month as a date in integer 1-31 format int x; if (month==1) //january { if (leapYear ==0){x=3;}//jan 3 in most years else{x=4;}}//leap Jan else if(month==2){ //feb if (leapYear ==0){x=28;} //last day of feb else {x=29;} } else if(month==4 ||6 || 8 ||10){x=month;}//most of the evens else if(month==3){x=7;} // zeroth of march and 7th of march else if(month==5){x=9;}// I work 9 to 5 else if(month==7){x=11;}// at the 7-11 else if(month==9){x=5;} else if(month==11){x=7;} return x; }

8. rsmith6559

false is declared as 0 in some C library (I don't remember which one). One thing to note is that C regards 0 as false, and any other value as true.