As a good programming practice, we display a message to prompt the user before accepting data from the keyboard. This enables the user to enter the required data correctly. However, the user may still enter incorrect data. Such data may cause the programs to print incorrect results.
The while loop is particularly suitable when the number of iterations is not known or can not be determined in advanced. In this section, another loop that is useful in similar situations, the do ... while loop is discussed.
Let us use variables m and n to represent two integer numbers and variable r to represent the remainder of their division, i. e., r = m % n. Euclid's algorithm to determine the GCD of two numbers m and n is given below and its action is illustrated form= 50 and n = 35.
The for loop is used for repetitive execution of a statement or a group of statements. It is a very powerful and flexible statement of the C language. It is generally used in situations where the number of iterations of the loop statement is known or can be determined in advance.
In this example, we use two for loops in conjunction with the putchar library function to print all uppercase letters followed by all lowercase letters. The loop variable ch is assumed to be of type char or int. The first for loop prints the uppercase letters as loop variable ch assumes values as ‘A’, 'B’, ... , ‘z’.
This code segment first accepts a range of values in variables m and n, both of type int. The variable num, also of type int, is used as the loop variable. It assumes values from m to n. For each value of loop variable num, the printf statement within the for loop is executed.
The for loop given above prints a line of fifty dashes followed by a newline. The for loop uses i as the loop variable whose initial and final values are 0 and 49, respectively (note the < operator in i < 50). The update expression increments the value of i by 1.
These statements determine and print the absolute value of variable n. If n is negative, then n < 0 evaluates as true and n is assigned the value of -n, which is then printed in the next statement assuming variable n to be of some integer type. However, if the value of n is positive, it remains unchanged and is printed.
While performing a division operation, if the divisor is zero, the division by zero error occurs and program execution halts abruptly. The program segment given below calculates and prints the value of a/b without causing a division by zero error.
A character is a whitespace if it is one of the following characters: blank space (' '), newline ('\n'), horizontal tab ('\t'), carriage return ('\r'), form feed ('\f') or vertical tab ('\v'). The if statement given below tests whether the given character ch is equal to one of these characters using logical or (||) operator and prints an appropriate message.
The program segment given below accepts a character from the keyboard and prints the name of the corresponding color, e. g., if the user enters character R, it prints Red. However, it handles only three colors, namely, red, green and blue.
Dinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.