Tuesday, 16 October 2012 0 comments

Week 4-5: Flow Control

In the first few weeks, I had a rough view of the basic syntax and structure of C language. Instead of writing programs practically, I learnt to read a program which mainly involving printf() statements and some simple syntax, and I got 100 points in the first assignment!

In the previous two weeks, a new field - Flow Control was introduced. It was divided into two parts, in Flow Control I, relational, equality and logical operators were introduced. They are provided to facilitate flow controls, especially in calculations and executing statements, it is important to distinguish their meanings and purposes. Besides, two types of flow controls, sequential and selection, were taught. For sequential, statements in a program are executed one after another, it is easy to understand and use. For selection, it involves a choice of alternative actions, and can be classified into if, if-else, and switch. When using selection, statements will be executed in different orders depending on the value (zero or non-zero) of the expression. I have learnt the if and if-else statement in cert. level so a clear concept has already existed in my mind.


In programming, we should be very careful about the structure, the order of each word, and the presence of each symbol. The following are four modifications of a program, they are very alike, but actually one of them is different from the others.


 In Flow Control II, repetition was introduced. In this type of flow control, we use while and do-while statements to repeatedly execute a statement. They are very useful in performing miscellaneous calculations and tasks. In the following example, for statement, which is equivalent to the while statement, is used to find the factorial of an input integer.


To practice, assignment 2 was finished in the last week and it was required to use prinf() statements, print-out (screen) formatting and branching statement "if-else" to perform simple calculation using C. It was the first program I wrote, it behaves as a price comparator for mobile phone users. The program first prints the information of mobile phone network operator monthly plans, it then asks for the air-time usage information. After inputting a single integral number, it is expected that the costs of joining different operators and different plans will be calculated and listed out. The flow control in this program is not complicated and is rather easy, so I spent only about 45 minutes in compiling it.



In short, Flow Control is a challenging topic and it consolidates my mind in writing programs. A program involving Flow Control will not be workable if we just follow the structure and put in all the syntax validly. Addition to syntactically correct, it also requires plentiful logical thinking. We need to design every statement logically with our problem-solving skills, or else, the program will be resulted in a valid meaningless one, infinite loop is a good illustration.