C Programming Notes
C Programming Notes
C Loops
In programming, a loop is used to repeat a block of code until the specified condition is met. C
programming has three types of loops:
for loop
while loop
do...while loop
for Loop
The syntax of the for loop is:
return 0;
1 2 3 4 5 6 7 8 9 10
i is initialized to 1. The test expression i < 11 is evaluated. Since 1 less than 11 is true, the body of for
loop is executed. This will print the 1 (value of i) on the screen. The update statement ++i is
executed. Now, the value of i will be 2. Again, the test expression is evaluated to true, and the body
of for loop is executed. This will print 2 (value of i) on the screen. Again, the update statement ++i is
executed and the test expression i < 11 is evaluated. This process goes on until i becomes 11. When i
becomes 11, i < 11 will be false, and the for loop terminates.
#include <stdio.h>
int main()
scanf("%d", &num);
sum += count;
return 0;
Sum = 55
The value entered by the user is stored in the variable num. Suppose, the user entered 10. The count
is initialized to 1 and the test expression is evaluated. Since the test expression count<=num (1 less
than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1.
Then, the update statement ++count is executed and count will equal to 2. Again, the test expression
is evaluated. Since 2 is also less than 10, the test expression is evaluated to true and the body of the
for loop is executed. Now, sum will equal 3. This process goes on and the sum is calculated until the
count reaches 11. When the count is 11, the test expression is evaluated to 0 (false), and the loop
terminates. Then, the value of sum is printed on the screen.
while loop
The syntax of the while loop is:
while (testExpression) {
The while loop evaluates the testExpression inside the parentheses (). If testExpression is true,
statements inside the body of while loop are executed. Then, testExpression is evaluated again. The
process goes on until testExpression is evaluated to false. If testExpression is false, the loop
terminates (ends).
#include <stdio.h>
int main() {
int i = 1;
while (i <= 5) {
printf("%d\n", i);
++i;
return 0;
Here, we have initialized i to 1. When i = 1, the test expression i <= 5 is true. Hence, the body of the
while loop is executed. This prints 1 on the screen and the value of i is increased to 2. Now, i = 2, the
test expression i <= 5 is again true. The body of the while loop is executed again. This prints 2 on the
screen and the value of i is increased to 3. This process goes on until i becomes 6. Then, the test
expression i <= 5 will be false and the loop terminates.
do...while loop
The do..while loop is similar to the while loop with one important difference. The body of do...while
loop is executed at least once. Only then, the test expression is evaluated. The syntax of the
do...while loop is:
do {
while (testExpression);
The body of do...while loop is executed once. Only then, the testExpression is evaluated. If
testExpression is true, the body of the loop is executed again and testExpression is evaluated once
more. This process goes on until testExpression becomes false. If testExpression is false, the loop
ends.
#include <stdio.h>
int main() {
do {
scanf("%lf", &number);
sum += number;
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
Sum = 4.70
Here, we have used a do...while loop to prompt the user to enter a number. The loop works as long
as the input number is not 0. The do...while loop executes at least once i.e. the first iteration runs
without checking the condition. The condition is checked only after the first iteration has been
executed.
do {
scanf("%lf", &number);
sum += number;
while(number != 0.0);
So, if the first input is a non-zero number, that number is added to the sum variable and the loop
continues to the next iteration. This process is repeated until the user enters 0. But if the first input
is 0, there will be no second iteration of the loop and sum becomes 0.0. Outside the loop, we print
the value of sum.
Practice Codes
Program to Print English Alphabets
#include <stdio.h>
int main() {
char c;
return 0;
ABCDEFGHIJKLMNOPQRSTUVWXYZ
In this program, the for loop is used to display the English alphabet in uppercase. Here's a little
modification of the above program. The program displays the English alphabet in either uppercase
or lowercase depending upon the input given by the user.
#include <stdio.h>
int main() {
char c;
printf("Enter u to display uppercase alphabets.\n");
scanf("%c", &c);
if (c == 'U' || c == 'u') {
} else {
return 0;
abcdefghijklmnopqrstuvwxyz
////////////////////////////////////////////
Multiplication Table Up to 10
#include <stdio.h>
int main() {
int n;
scanf("%d", &n);
return 0;
9*1=9
9 * 2 = 18
9 * 3 = 27
9 * 4 = 36
9 * 5 = 45
9 * 6 = 54
9 * 7 = 63
9 * 8 = 72
9 * 9 = 81
9 * 10 = 90
Here, the user input is stored in the int variable n. Then, we use a for loop to print the multiplication
table up to 10.
The loop runs from i = 1 to i = 10. In each iteration of the loop, n * i is printed. Here's a little
modification of the above program to generate the multiplication table up to a range (where range is
also a positive integer entered by the user).
#include <stdio.h>
int main() {
int n, i, range;
scanf("%d", &n);
do {
scanf("%d", &range);
return 0;
Enter an integer: 12
12 * 1 = 12
12 * 2 = 24
12 * 3 = 36
12 * 4 = 48
12 * 5 = 60
12 * 6 = 72
12 * 7 = 84
12 * 8 = 96
Here, we have used a do...while loop to prompt the user for a positive range.
do {
scanf("%d", &range);
If the value of range is negative, the loop iterates again to ask the user to enter a positive number.
Once a positive range has been entered, we print the multiplication table.