Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
C: Else If


1. C: Else If

2. Day of the week
Here is some ways to print the day of a week using various conditional control constructs. Note: There are better ways to do this but not using what has been introduced so far (including this page). Here is a table of the values. Note that counting starts at 0 and not at 1.

3. Pictoral diagram
Syntax diagram for EBNF

4. Using just if then else statements
Here is one way using nested if then else statements.

Here is the C code.


5. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


6. Else-If constructs
A shorter form of the above can be used for the above selection of one of many possibilities.

Here is the C code.


7. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


8. Using just if statements
Here is one way using non-tested if statements. Note the use of a flag variable to determine if no valid day was entered. Note: There are other ways to do this, but this show how a flag variable would be used.

A flag variable should never be called flag. It should have something in the name that describes what it is "flagging". In the example below, the flag variable found1 is 0 if not found set to 1 if found. Here is the C code.


9. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


10. Switch statement
C has a switch statement. It is show here so you have seen it. You should never use this statement (in a beginning course) and seldom, if ever, use it as an expert. It is considered to be problematic and easy to make errors that can be hard to detect. Here is the C code.

The cases must be literals (not string) and will fall into the next case without a break - which causes a lot of issues.

11. Examples of input and output
Here are some examples of input and output for the above program code.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.

Here is an example input.

For the above example input, here is the expected output.


12. End of page