Loop
Loop
A loop is used for executing a block of statements repeatedly until a given condition returns false.
Step 1: First initialization happens and the counter variable gets initialized.
Step 2: In the second step the condition is checked, where the counter variable is tested for the
given condition, if the condition returns true then the C statements inside the body of for loop gets
executed, if the condition returns false then the for loop gets terminated and the control comes out
of the loop.
Step 3: After successful execution of statements inside the body of loop, the counter variable is
incremented or decremented, depending on the operation (++ or –).
Example of For loop
#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
Nested For Loop in C
Nesting of loop is also possible. Lets take an example to understand this:
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<4; j++)
{
printf("%d, %d\n",i ,j);
}
}
return 0;
}
Output: 1 2 3
0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3
In the above example we have a for loop inside another for loop, this is called nesting of loops.
One of the example where we use nested for loop is Two dimensional array.
While loop
A while loop in C programming repeatedly executes a target statement as long as a given condition
is true.
Syntax of While Loop
while(condition) {
statement(s);
}
#include <stdio.h>
int main () {
int a = 10;
while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}
return 0;
}
Output: value of a: 10, 11---19
Here, statement(s) may be a single statement or a block of statements. The condition may be any
expression, and true is any nonzero value. The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the
loop.
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
Assignment operator :An assignment operator assigns a value to its left operand based on the value of its right
operand.
The following table lists the assignment operators supported by the C language −
= Simple assignment operator. Assigns values from right side C = A + B will assign the value
operands to left side operand of A + B to C
+= Add AND assignment operator. It adds the right operand to the C += A is equivalent to C = C +
left operand and assign the result to the left operand. A
-= Subtract AND assignment operator. It subtracts the right
operand from the left operand and assigns the result to the left C -= A is equivalent to C = C - A
operand.
Example
Try the following example to understand all the assignment operators available in C −
#include <stdio.h>
main() {
int a = 21;
int c ;
c = a;
printf("Line 1 - = Operator Example, Value of c = %d\n", c );
c += a;
printf("Line 2 - += Operator Example, Value of c = %d\n", c );
c -= a;
printf("Line 3 - -= Operator Example, Value of c = %d\n", c );
c *= a;
printf("Line 4 - *= Operator Example, Value of c = %d\n", c );
c /= a;
printf("Line 5 - /= Operator Example, Value of c = %d\n", c );
c = 200;
c %= a;
printf("Line 6 - %= Operator Example, Value of c = %d\n", c );
c <<= 2;
printf("Line 7 - <<= Operator Example, Value of c = %d\n", c );
c >>= 2;
printf("Line 8 - >>= Operator Example, Value of c = %d\n", c );
c &= 2;
printf("Line 9 - &= Operator Example, Value of c = %d\n", c );
c ^= 2;
printf("Line 10 - ^= Operator Example, Value of c = %d\n", c );
c |= 2;
printf("Line 11 - |= Operator Example, Value of c = %d\n", c );
}
When you compile and execute the above program, it produces the following result −