From the course: C Essential Training

Making a decision - C Tutorial

From the course: C Essential Training

Start my 1-month free trial

Making a decision

- C programs run top down as written in the source code file. Each statement runs one after the other, until the final statement in the function. You can alter program flow by calling a function, in which case the functions statements execute. Here in the main function at line 32, the get_value function is called. At this point execution jumps to the function, which is located at line four in this source code file. Within a function, an if decision statement can alter program flow. When the if statement condition is true, the statements belonging to it are executed, otherwise they're skipped. So here at line 34, this statement is executed only when the value of variable rows is greater than 18. You can also alter program flow by using a loop. The for statement at line 37 repeats a group of statements based on a condition. This for statement is yet another way to alter program flow. The flow in this code is altered by the if statement at line nine. The if keyword is followed by a set of parentheses in which a condition appears, an expression. When this expression proves true or equals a non zero value, a block of statements are executed. When the expression is false, the block is skipped. Here when the value of variable a is less than 10, the statement puts at line 11 is executed, otherwise it's skipped. Build and run the code and try a few values. Four and let's run again. 100. You see the statement is skipped. Because only one statement is included in this If construction, you can remove the braces. In fact, both lines here, technically a single statement, though it's tradition and expected to put the if statement on another line. Oh, and don't put the semi colon right here, which happens often out of habit, but it's wrong. The if statement's semi-colon is actually right here. Because multiple statements belong to this code's if decision block, they must be enclosed embraces. Build and run this code on your own to see what it does. You can nest if statements as shown in this code, the two statements work like a filter to ensure in this case, if a is greater than six, but a second test to see if is also less than 15. When these conditions are met, the putchar Statement is executed. Of course, this code can be simplified. Here you see the simplified version. This exercise file places both conditions into a single if statement, the code runs the same, but it's easier to read if the value of a is greater than six. And this is the logical and operator, the value of variable a is less than 15. If these conditions are true, both of them, this statement, just one executes. Build and run. And you see in the output asterisks appear by values greater than six and less than 15. This shows not only how flow can be altered in a program, but how a decision can rely upon two expressions coupled with a logical operator.

Contents