0% found this document useful (0 votes)
2 views8 pages

Loop

The document explains various types of loops in C programming, including for loops, while loops, and do...while loops, detailing their syntax and how they function. It also covers assignment operators in C, providing examples of their usage. Additionally, the document includes sample code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views8 pages

Loop

The document explains various types of loops in C programming, including for loops, while loops, and do...while loops, detailing their syntax and how they function. It also covers assignment operators in C, providing examples of their usage. Additionally, the document includes sample code snippets to illustrate the concepts discussed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Loop

A loop is used for executing a block of statements repeatedly until a given condition returns false.

Syntax of for loop:

for (initialization; condition test; increment or decrement)


{
//Statements to be executed repeatedly
}

Flow Diagram of For loop

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.

DoWhile: 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 once, before checking the test expression. Hence, the do...while
loop is executed at least once
do...while loop Syntax
do
{ // codes
}
while (testExpression);
How do...while loop works?
The code block (loop body) inside the braces is executed once.
Then, the test expression is evaluated. If the test expression is true, the loop body is executed again.
This process goes on until the test expression is evaluated to 0 (false).
When the test expression is false (nonzero), the do...while loop is terminated.

// Program to add numbers until user enters zero


#include <stdio.h>
int main()
{
double number, sum = 0;

// loop body is executed at least once


do
{
printf("Enter a number: ");
scanf("%lf", &number);
sum += number;
}
while(number != 0.0);
printf("Sum = %.2lf",sum);
return 0;
}

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 −

Operator Description Example

= 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.

*= Multiply AND assignment operator. It multiplies the right


C *= A is equivalent to C = C *
operand with the left operand and assigns the result to the left
A
operand.

/= Divide AND assignment operator. It divides the left operand


C /= A is equivalent to C = C / A
with the right operand and assigns the result to the left operand.

%= Modulus AND assignment operator. It takes modulus using C %= A is equivalent to C = C %


two operands and assigns the result to the left operand. A

<<= Left shift AND assignment operator. C <<= 2 is same as C = C << 2

>>= Right shift AND assignment operator. C >>= 2 is same as C = C >> 2

&= Bitwise AND assignment operator. C &= 2 is same as C = C & 2

^= Bitwise exclusive OR and assignment operator. C ^= 2 is same as C = C ^ 2

|= Bitwise inclusive OR and assignment operator. C |= 2 is same as C = C | 2

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 −

Line 1 - = Operator Example, Value of c = 21


Line 2 - += Operator Example, Value of c = 42
Line 3 - -= Operator Example, Value of c = 21
Line 4 - *= Operator Example, Value of c = 441
Line 5 - /= Operator Example, Value of c = 21
Line 6 - %= Operator Example, Value of c = 11
Line 7 - <<= Operator Example, Value of c = 44
Line 8 - >>= Operator Example, Value of c = 11
Line 9 - &= Operator Example, Value of c = 2
Line 10 - ^= Operator Example, Value of c = 0
Line 11 - |= Operator Example, Value of c =

You might also like