Computer Fundamentals with C and Unix
(Control Structures)
Lecture 03: Dec. 27, 2022 Prof. K.R. Chowdhary : Professor of CS
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.
Many times we like to repeat a certain operation number of times, this we can do by putting the corresponding statement inside a loop, e.g., for loop, which repeat the statement a fixed and predetermined number of times. Like, we did print the “Hello World” message 10 times, in our earlier discussion. The for-loop has a counter, say i, which is initialized by say 0, and every time for repeating the loop a condition has to be met or satisfied, and the loop counter is incremented every time. The program in Fig. 3.1 prints the counter value every time the loop is executed.
In the beginning we initialize the loop counter i to 0, and for the execution of for loop, which has only one statement, the condition is checked (i < 10), if true, the loop is executed. Then, at the end of eac execution, the loop counter i is incremented. This process repeats as long as i < 10 holds true. The moment i be comes 10, i < 10 becomes false, and the loop terminates, and program also terminates.
Fig. 3.2 shows many things. At the top we compile the program:
gcc -o loop loopi.c
which generates executable output file by name loop. Then we run this file by command:
./loop
which produces the result as desired. The list command ls -l, lists all files in the current directory. We note that loop file at the bottom is shown as executable -rwxrwxr-x, that means the owner can read, write, execute, the group can also do the same, and other people can read, cannot write, but can executed this file, of size 15960 bytes.
Many times we want to do certain jobs when a condition is true, and not do when condition is false. For example, giving discount in train ticket for senior citizen. Say this discount is 30%, and allowed for those whose age is more or equal to 60 year. Or for example, giving first division to those who percent is more or equal to 60%, and in addition, the honors whose percent is more or equal to 75%.
This condition we implement using if statement, as the Fig. 3.3 explains. Note that the scanf statement inputs the data from keyboard and stores into the memory locations. The memory locations corresponding to the data items (variables) age, tktamt, and fare are &age, &tktamt and &fare, respectively.
The if statement is followed with a logical expression enclosed in a parenthesis, here it is (age >= 60). If this expression is true, then the statement immediately after if is executed, otherwise the statement after the else is executed.
The printf statement at the end is not part of the conditions, and executed always irrespective of the conditions of the if statements.
The Fig. 3.4 shows running of the compiled program ifcon.c, i.e., a.out at command prompt. In first case, with age 65, the condition with if statment is true, while for the second with age 56, the condition is false.