The if else statement

Structure of an if/else statement:

   if (<logical expression>)
      <statement if true; can be a compound statement>
   else     this part is optional
      <statement if false; can be a compound statement>

Borrowing from material covered later:  how to immediately terminate the program:  exit(value)  The value is returned to the operating system.

Remainders.txt    Remainders.c

Nested structure:  if either the if statement or the else statement is a compound statement (between "{" and "}"), it may itself contain an if statement.

Very common structure:  if / else if / . . . / else.

IfElseIf_1.txt    IfElseIf_1.c

The level at which the program enters the if block, it is guaranteed that all of the conditions above have been false.

Conceptually it is convenient to think of "else if" as introducing a section at the same level as the if above it.  The program indentation structure can display this.

IfElseIf_2.txt    IfElseIf_2.c

User-defined Functions

System functions that we have used so far:  printf(<control string>[, argument(s)])  scanf<control string>[,argument(s)])

These actually return integer values, which we have been ignoring.  printf returns the number of characters printed (excluding the \0).  scanf returns the number of input items matched and assigned (which can be 0), or else it returns an error indication, EOF which is defined in stdio.h to be -1.

We have also been writing so far one user-defined function:  int main() — with no parameters/arguments in parentheses.  [In fact there are two parameters that can be used, allowing the user access to information in the command that starts the program running:  (int argc, char* argv[])  We may examine these toward the end of the quarter.]

We can also write other functions, which then can be called from the main.  This aids in modular programming:  each module (function) does a single task.  It can then be called at various points in the over-all program in which that task needs to be performed.

Example of functions that have no parameters and return no value:   cls.txt    cls.c   cls.exe