I am trying to write out the code for the Vigenere program. Specifically, I am trying to write out an else if clause that checks whether the key is all alphabetic. Thus, I have:

                     if (argc != 2)
                     printf("Wrong input \n");
                     return 1;
\\\Check whether key is all alphabetic.
                     else if  (for (int i = 0, n = strlen(argv[1]); i < n; i++)
                     printf("Wrong input \n");
                     return 1;

The first if clause works. But the second else if, does not. Rather I receive this error:

 error: expected expression
  else if  (for (int i = 0, n = strlen(argv[1]); i < n; i++)

Any help?

1 Answer 1


Seems a bit convoluted. How would a for loop return a true/false to an if statement? If it's possible, I'd like to see a demonstration of it. Even if it is possible, why make things so complicated?

There's an old saying in programming - "KISS - Keep It Simple, Stupid!", something I tell myself at some point on every project. ;-) So with that in mind, let's think about a few things.

First, let's look at the else clause. Is an "else" clause needed? Think about it. If the first "IF" statement is true, then the program will be terminated. If it's false, then processing will continue with the next line, so there's really no need for an else clause following the first if statement and it's associated code block. In fact, it introduces opportunity for confusion and for bugs to be introduced into the code. It increases code complexity and decreases efficiency (the else clause has to be handled/processed). This is a very common thing for new programmers - whether to chain together multiple IF statements with ELSE clauses or whether to let the stand as independent IF statements, or even the overuse of if statements when not even needed.

Next, why is there an "IF" following the else? Think again about what has to happen. The code needs to loop through the string, examining each letter. If it finds a non-alpha, it's going to execute the printf and terminate the program. The third if statement is handling what needs to be done. The second if statement is both not needed and an error in coding.

You might also remember "Find the Second Right Answer!" Often, when coding, the first right version works, but there may be a better way to do a task. Did you compartmentalize the tasks as much as you could? Is there any unnecessary code, branching, etc? Anything that can be simplified to make it more efficient? So, "KISS" and think! ;-)

"IF" this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .