CS 2213 Advanced Programming
Control Flow in C


Read Chapter 3 of the text.
Previous Topic: Types, Operators, and Expressions in C
Next Topic: Functions and Program Structure

Statements and Blocks

Like in Java, statements are followed by a semicolon.

Braces group definitions, declarations and statements together into a compound statement or block so they behave as a single statement.

Examples:
the statements of a function
braces around multiple statements after if, else, while, for


if-else

This is the same as in Java


else-if

This is the same as in Java


switch This is the same as in Java


Loops--- While and For

These are almost the same as in Java.

You cannot declare variables in the initialization of a for.

Remember that there is no boolean data type so integers are used with 0 being false and anything else being true.

In Java, the break statement can take an argument which allow you to break out of multiple levels of a for loop.
You cannot do this in C.
break will only break out of the innermost level.


Loops--- Do-while

This is the same as in Java


Break and Continue

These are the same as in Java but cannot take a label argument.

break terminates the innermost switch, for, while, or do statement.


Goto and Labels

In C, just as in Java you can use labels anywhere.
In Java you can refer to labels only with continue and break statements.
C also allows goto statements.
There are rare cases when one might want to use these, but in this course they are to be avoided.