Computer Fundamentals with C and Unix
(Control Structures-2)
Lecture 04: Dec. 21, 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.
The if - - else is useful when there is only one condition, and we need to choose one statement/set of statements when condition is true, and when false, the other set of statements are to be chosen. Let use consider to allot division to students based on their grade of marks obtained in a examination, .
D: print “Distinction”
A: print “First div”
B: Print “Second division”
C: Print “Pass Division”
F: “Fail”
In this case we need many conditions, one condition for each letter grades “D - F”. This is cumbersome to do using if then statement, as it requires many nested if-else statement.
Fig. 4.1 shows the program with many conditions, this control structure is called switch-case statement. If case statement represent condition specified as integer or a character, and the expression along with switch (row no. ...) should have values corresponding to case conditions. If no condition is satisfied then default clause is executed, i.e., the section of program corresponding to the “default”. Note that this time we have shown the line numbers along with the code. These line numbers can be set on or off by “esc : set nu” and “esc: set nonu”, respectively, in the vi editor. The break statement indicates that once a particular is true, it is executed and remaining will not be checked.
The Fig. 4.2 shows the running of the program of switch-case, which is self explanatory.
The break statement immediately exits the current loop, and continues executing from the next statement of this loop. This exit may be required when certain condition is met inside a loop.
The following programs show how to count characters and words in a text. This text can be input from keyboard, or can come from a file, or from www.
Looping Statements in C execute the sequence of statements many times until the stated condition becomes false. A loop in C consists of two parts, a body of a loop and a control statement. The control statement is a combination of some conditions that direct the body of the loop to execute until the specified condition becomes false. The purpose of the C loop is to repeat the same code a number of times.
Depending upon the position of a control statement in a program, looping statement in C is classified into two types:
Entry controlled loop: In an entry control loop in C, a condition is checked before executing the body of a loop. It is also called as a pre-checking loop.
Exit controlled loop: In an exit controlled loop, a condition is checked after executing the body of a loop. It is also called as a post-checking loop.
The control conditions must be well defined and specified otherwise the loop will execute an infinite number of times. The loop that does not stop executing and processes the statements number of times is called as an infinite loop. An infinite loop is also called as an Endless loop. Following are some characteristics of an infinite loop:
No termination condition is specified.
The specified condition is never met.
The specified condition determines whether to execute the loop body or not. The C programming language provides us with three types of loop constructs:
The while loop
The do-while loop
The for loop
Sr. No. | Loop Type | Description |
1. | while loop | In while loop, a condition is evaluated before processing the body of the loop. If a condition is true then and only then the body of the loop is executed. |
2. | do ... while loop | In a do ... while loop, the condition is always evaluated after the body of the loop is executed. This loop is also called an exit-controlled loop. |
3. | for loop | In a for loop, the initial value is performed only once, then the condition tests and compares the counter to a fixed value before each iteration is performed, and stops the for loop when condition is false. |
A while loop is the most straightforward looping structure. While loop syntax in C programming language is as follows: Syntax of While Loop in C:
while (condition) {
statements;
}
It is an entry-controlled loop. In while loop, a condition is evaluated before processing body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop.
After exiting the loop, the control goes to the statements which are immediately after the loop. The body of a loop can contain more than one statement. If it contains only one statement, then the curly braces are not compulsory.
In a while loop, if the condition is not true, body of a loop will not be executed, not even once. It is different in do ... while loop which we will see shortly.
Following program illustrates while loop in C language:
Output: 1
2
3
4
5
6
7
8
9
10
We have initialized a variable called num with value 1. We are going to print from 1 to 10 hence the variable is initialized with value 1. If you want to print from 0, then assign the value 0 during initialization.
In a while loop, we have provided a condition (num <= 10), which means the loop will execute the body until the value of num becomes 10. After that, the loop will be terminated, and control will fall outside the loop.
In the body of a loop, we have a print function to print our number and an increment operation to increment the value per execution of a loop. An initial value of num is 1, after the execution, it will become 2, and during the next execution, it will become 3. This process will continue until the value becomes 10 and then it will print the series on console and terminate the loop.
A do ... while loop in C is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop.
Syntax of do while loop in C programming language is as follows: Syntax of Do-While Loop in C:
do {
statements
} while (expression);
As we saw in a while loop, the body is executed if and only if the condition is true. In some cases, we have to execute a body of the loop at least once even if the condition is false. This type of operation can be achieved by using a do-while loop.
In the do-while loop, the body of a loop is always executed at least once. After the body is executed, then it checks the condition. If the condition is true, then it will again execute the body of a loop otherwise control is transferred out of the loop.
Similar to the while loop, once the control goes out of the loop the statements which are immediately after the loop is executed.
The critical difference between the while and do-while loop is that in while loop the while is written at the beginning. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;)
The following loop program in C illustrates the working of a do-while loop:
Below is a do-while loop in C example to print a table of number 2:
Output:
2
4
6
8
10
12
14
16
18
20