- ..clause 1..: Processing for imitation
- ..clause 2..: The usage is the same.
- ..clause 3..: Make it legibly.
It explained the method of controlling the execution of specific processing according to the condition by using the if sentence in the preceding chapter.
However, this method has only the selection whether to execute processing.
Though the example named there is money-> roast meat table d'hote and had - > bamboo basket Sova was written at the beginning of the preceding chapter
The method of the preceding chapter is had [datta] that doesn't do - > anything by eating there is money->.
When you want to make the sentence executed when not agreeing to not only the agreement with the condition but also the condition
Else (Els) sentence can be used with the if sentence.
Use the else sentence as follows.
When the condition is an imitation, the following sentence of the else sentence is executed.If (conditional expression) Sentence executed for the truth;Sentence executed for else imitation;
Even when the else sentence is applied, the usage of the if sentence is quite the same.
However, the sentence executed for the imitation was only applied as an option.
The following program is an example of having rewritten the program made in the preceding chapter by the else sentence.
The result when this program is executed and ten is input becomes as follows.#include <stdio.h> int main(void) { int suuti; scanf("%d",&suuti); If (suuti == 10) Printf ("The input value is ten. \n"); Else printf ("The input value is not ten. \n"); return 0; }
The result when this program is executed and it inputs it excluding ten becomes as follows.
10 Input data
The input value is ten.
If the result is seen, the second sentence is executed for the imitation as understood.
135 Input data
The input value is not ten.
Divide into two or more lines to write the if-else sentence legibly, and add the indent.
Moreover, write as follows when you use the complex sentence by the if-else sentence.#include <stdio.h> int main(void) { if (suuti == 10) printf(The input value is ..".. ten. \n"); else printf(The input value is not ..".. ten. \n"); }
When this writing is used, the relation between the if sentence and the else sentence is understood at one view.#include <stdio.h> int main(void) { Input..value.\n"); } else { Printf ("The input value is not ten. \n"); } }
#include <stdio.h> int main(void) { if (suuti == 10) { Printf ("The input value is ten. \n"); } else { Printf ("The input value is not ten. \n"); } }