.. _control: Control Constructs ================== .. index:: selection statement, loop Control constructs determine the flow of execution of the program. With no control constructs, statements execute sequentially. Some control constructs are :dfn:`selection statements` meaning that it selects which code to execute. Other control constructs are :dfn:`loops`, where the control construct determines how many times the loop will execute. C keywords related to selection statements include: :keyword:`if`, :keyword:`else`, :keyword:`switch`, :keyword:`case`, :keyword:`break`, :keyword:`default`. C keywords related to loops include: :keyword:`for`, :keyword:`while`, :keyword:`do`, :keyword:`break`, :keyword:`continue`. if block ----------- :: if( condition ) statement; if( condition ) { statement block; } if( condition ) { statement block1; } else { statement block2; } :: if( x < 10 && y < x ) printf( "x is less than 10, but greater than y" ); else { if( x < 10 ) printf( "x is less than 10, and not greater than y" ); else { if( y < x ) printf( "x is not less than 10 and is greater than y" ); else printf( "x is not less than 10 and not greater than y" ); } } Note that the two pairs of brackets are not needed, but they add to the clarity of the program. Avoid the ambiguous else ... :: if( condition1 ) if( condition2 ) statement; else { /* WRONG */ statement block; } Relational and Logical Operators ------------------------------------ Critical to any control construct are conditions statements that evaluate to either **True** or **False** (boolean statements). .. note:: In C, extend the Boolean statements to integers. 0 and *NULL* are **False**, and any other valid data value is **True**. Relational Operators ~~~~~~~~~~~~~~~~~~~~~~~ Relational operators evaluate how two items compare to each other. ======== ===================================================== Operator Meaning ======== ===================================================== == Equal to each other (note: ``a == b`` NOT ``a = b``) != Not equal > Greater than < Less than <= Less than or equal >= Greater than or equal ======== ===================================================== .. note:: C does not have relational operators to evaluate strings or other data structures -- only the fundamental data types. See :ref:`data_types1`. Other functions may be used for these evaluations, such as :func:`strcmp()`. Logical Operators ~~~~~~~~~~~~~~~~~~~~~~~ Logical operators allow us to combine other boolean expressions to form one based on the desired logic. ======== ======== ========== Operator Meaning Example ======== ======== ========== && AND a && b || OR a || b ! NOT !a ======== ======== ========== Parenthesis **( )** may also be used to control the order in which boolean expressions are evaluated. while loops ---------------- :: while( operator ) { statement block; } For loop ------------ Here we present another looping construct. It is most often used when iterating through a sequence of a pre-defined length. :: for( expr1; expr2; expr3 ) { code block; } Is equivalent to: :: expr1; while( expr2 ) { code block; expr3; } - expr1 is the *initializer* of the `for` loop. - expr2 is the *condition* for continuing with the loop. - expr3 is evaluated after each execution of the code block and is usually used to facilitate *iterating* through the loop. :: #include #define MAX 5 int main(void) { int i, x=1, y=1; for( i = 1; i < MAX; i++ ) { x = i * y; y = x*i; } printf( "x = %d, y = %d\n", x, y); return 0; } Now compete :ref:`hw3`.