Every program follows a control path. C program control constructs are conditionals and loops. Almost every program needs to go through some sort of decision making. Decision making processes are simulated in C using conditionals. We can state a conditional as a statement that instructs the compiler to execute a function or statement if a certain condition is proved true. Decision making is done on the basis of some logic. Logic is treated as arithmetic by C compiler. The value 0 stands for true and rest any other value stands for a false value. The most common conditionals are if-else statements, switch case statements and loops.
If-else statements are used for conditional branching. It starts with if followed by an expression in its parentheses. The enclosed expression is evaluated and if the condition proves out to be true the succeeding statement is executed. If the condition is false the succeeding statement is skipped. Break and continue statements are used in if else and switch case constructs. Break will cause the execution to jump out of the loop and execute the following immediate statements whereas continue statement causes the loop to execute again and again until the condition is proved false.
Switch case begins with keyword switch followed by a variable to be switched enclosed in a parentheses. The word case is used to begin each case followed by a variable for that case, then a colon and then by the statements to be executed. Statements will be executed until a break is found. Any of the above constructs can be nested. Now coming to the go to statement, with this goto statement you can jump in between anywhere in a program except you are not allowed to jump in a loop. Goto statements are mostly risky but if there’s a place in the program where a goto statement fits , feel free to use it. However you should not be used.
Let’s define the loops now. C has 3 types of loops namely the while loop, for loop and the do while loop. The “while loop” continues to execute till the condition proves to be false. It’s an entry controlled loop which means that the loop will execute only when the condition is met. In this loop the keyword while is followed by some expression in parentheses, followed by a compound statement enclosed in braces. After reaching at the end of the loop, the control goes back to the top and the condition is revaluated.
Do while loop is an exit controlled loop. The test condition is evaluated at the end only. So even if the condition is false the loop is executed once compared to the while loop which doesn’t allows execution even once if the condition is false. You can call the do while loop as a pos check loop. This is the main difference between the while loop and the do while loop. These loops can also be nested. Nesting is unlimited. The “for loop” is the easiest loop. All its loop controlled elements are gathered at one place while in other loops they are scattered everywhere in the program. In a for loop firstly the initialization expression is executed. Then, the test expression is evaluated. If it’s true, the body of the loop is executed. After the execution the update expression is implemented, the test expression is again evaluated. If it’s true the whole sequence is repeated. You should use a for loop hen you have to repeat a block of statements specific number of times. These are the tools that programs need to perform repetitive tasks and make decisions.
Thursday, September 8, 2011
How is program controlled in C?
Posted by Sunflower at 9/08/2011 06:42:00 PM
Labels: Branching, C, C Language, Compiler, Conditions, Constructs, Control, Logical, Loops, Paths, program, Program control, Statements
Subscribe by Email |
|
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment