0% found this document useful (0 votes)
3 views

Looping in Programming Language C

The document provides a comprehensive overview of looping constructs in the C programming language, including for-loops, while-loops, and do-while loops, along with their syntax, examples, and differences. It also explains the use of continue and break statements within loops, demonstrating their functionality with code examples. Additionally, it covers nested loops and the use of logical operators in while loops.

Uploaded by

sasmita8687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Looping in Programming Language C

The document provides a comprehensive overview of looping constructs in the C programming language, including for-loops, while-loops, and do-while loops, along with their syntax, examples, and differences. It also explains the use of continue and break statements within loops, demonstrating their functionality with code examples. Additionally, it covers nested loops and the use of logical operators in while loops.

Uploaded by

sasmita8687
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

IGIT, SARANG

Looping in Programming Language C


:
• Introduction of Looping
• for-loop: example, description, various forms
• while-loop: example, description, logical operator
• Introduction of do..while loop
• Difference between while-loop and do..while loop
• while vs do..while loop
• Continue Statement and its example
• Break statement
• Break Statement examples:
• Use of break in a while loop
• Use of break statement in switch-case

Name: S. Ashutosh
Looping in C
• Definition: In programming, a loop is used to repeat a block of code until the specified
condition is met.
• A loop statement allows us to execute a statement or group of statements multiple
times
• programming has three types of loops:
– for loop
– while loop
– do...while loop
• For-loop
– This is one of the most frequently used loops in C programming.
Syntax of for loop:
for (initialization; condition test; increment or decrement)
{
//Statements to be executed repeatedly
}
Flow Diagram of for-loop

Description of the 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: 1
2
3
Various forms of for-loop in C
• Here instead of num++, num=num+1 is used which is same as num++.
e.g. for (num=10; num<20; num=num+1)
• Initialization part can be skipped from loop as shown below, the counter variable is
declared before the loop.
int num=10;
for (;num<20;num++)
Note: Even though we can skip initialization part but semicolon (;) before condition is
must, without which you will get compilation error.
• Like initialization, you can also skip the increment part as we did below. In this case
semicolon (;) is must after condition logic. In this case the increment or decrement
part is done inside the loop.
for (num=10; num<20; )
{
//Statements
num++;
}
• This is also possible. The counter variable is initialized before the loop and
incremented inside the loop.
int num=10;
for (;num<20;)
{
//Statements
num++;
}
• As mentioned above, the counter variable can be decremented as well. In the below
example the variable gets decremented each time the loop runs until the condition
num>10 returns false.
for(num=20; num>10; num--)
Nested For Loop in C:
Nesting of loop is also possible. Example to understand this:
#include <stdio.h>
int main()
{
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
printf("%d, %d\n",i ,j);
}
}
return 0;
}

Output:
00
01
02
10
11
12

• Multiple initialization inside for Loop in C


We can have multiple initializations in the for loop as shown below.
for (i=1,j=1;i<10 && j<10; i++, j++)
Example of for loop with multiple test conditions
#include <stdio.h>
int main()
{
int i,j;
for (i=1,j=1 ; i<3 || j<5; i++,j++)
{
printf("%d, %d\n",i ,j);
}
return 0;
}
• In the above example we have a for loop inside another for loop, this is called nesting
of loops. Here after completion of inner loop execution then next iteration of outer
loop start.

• while loop
– Syntax of while loop:
while (condition test)
{
//Statements to be executed repeatedly
// Increment (++) or Decrement (--) Operation
}
Flow chart of while loop

• Description:
step1: The variable count is initialized with value 1 and then it has been tested for the
condition.
step2: If the condition returns true then the statements inside the body of while loop
are executed else control comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested
again for the loop condition.
• Example of while loop
#include <stdio.h>
int main()
{
int count=1;
while (count <= 4)
{
printf("%d ", count); count++;
}
return 0;
}
Output:
1234
Example 1:
#include <stdio.h>
int main()
{
int var = 6;
while (var >=5)
{
printf("%d", var); var++;
}
return 0;
}
Infinite loop: var will always have value >=5 so the loop would never end.
Example 2:
#include <stdio.h>
int main()
{
int var =5;
while (var <=10)
{
printf("%d", var);
var--;
}
return 0;
}
Infinite loop: var value will keep decreasing because of –- operator, hence it will always
be <= 10.
Use of Logical operators in while loop
• Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in
while loop.
The following scenarios are valid :
• while(num1<=10 && num2<=10)
-using AND(&&) operator, which means both the conditions should be true.
• while(num1<=10||num2<=10)
– OR(||) operator, this loop will run until both conditions return false.
• while(num1!=num2 &&num1 <=num2)
– Here we are using two logical operators NOT (!) and AND(&&).
• while(num1!=10 ||num2>=num1)
Example of while loop using logical operator
#include <stdio.h>
int main()
{
int i=1, j=1;
while (i <= 4 || j <= 3)
{
printf("%d %d\n",i, j);
i++; j++;
}
return 0;
}
Output:
11
22
33
44
• do..while loop
– Syntax of do-while loop
Do
{
//Statements
}while(condition test);
– Flow diagram of do while loop

• Difference between while loop and do while loop:


A do while loop is similar to while loop with one exception that it executes the statements
inside the body of do-while before checking the condition. On the other hand in the while
loop, first the condition is checked and then the statements in while loop are executed. So
you can say that if a condition is false at the first place then the do while would run once,
however the while loop would not run at all.
Example of do while loop
#include <stdio.h>
int main()
{
int j=0;
Do
{
printf("Value of variable j is: %d\n", j);
j++;
}while (j<=3);
return 0;
}
Output:
Value of variable j is: 0
Value of variable j is: 1
Value of variable j is: 2
Value of variable j is: 3

While vs do..while loop in C


Using while loop:
#include <stdio.h>
int main()
{
int i=0;
while(i==1)
{
printf("while vs do-while");
}
printf("Out of loop");
}
Output:
Out of loop
Same example using do-while loop
#include <stdio.h>
int main()
{
int i=0;
do
{
printf("while vs do-while\n");
}while(i==1);
printf("Out of loop");
}
Output:
while vs do-while
Out of loop
• Explanation: As mentioned in the beginning of this guide that do-while runs at least
once even if the condition is false because the condition is evaluated, after the
execution of the body of loop.

Continue Statement with example


• The continue statement is used inside loops. When a continue statement is
encountered inside a loop, control jumps to the beginning of the loop for next
iteration, skipping the execution of statements inside the body of loop for the current
iteration.
• C – Continue statement
Syntax:
continue;
• Flow diagram of continue statement

Example:
#include <stdio.h>
int main()
{
int counter=10;
while (counter >=0)
{
if (counter==7)
{
counter--; continue;
}
printf("%d ", counter);
counter--;
}
return 0;
}
Output:
10 9 8 6 5 4 3 2 1 0
The print statement is skipped when counter value was 7.
break statement
• It is used
– to come out of the loop instantly. When a break statement is encountered
inside a loop, the control directly comes out of loop and the loop gets
terminated. It is used with if statement, whenever used inside loop.
– in switch case control structure. Whenever it is encountered in switch-case
block, the control comes out of the switch-case.
• Syntax:
break;
• Flow diagram of break statement
Example – Use of break in a while loop
#include <stdio.h>
int main()
{
int num =0;
while(num<=100)
{
printf("value of variable num is: %d\n", num);
if (num==2)
{
break;
} num++;
}
printf("Out of while-loop");
return 0;
}
Output:
value of variable num is: 0
value of variable num is: 1
value of variable num is: 2
Out of while-loop
Example – Use of break statement in switch-case
#include <stdio.h>
int main()
{
int num;
printf("Enter value of num:"); scanf("%d",&num);
switch (num)
{
case 1
printf("You have entered value 1\n");
break;
case 2:
printf("You have entered value 2\n");
break;
case 3:
printf("You have entered value 3\n");
break;
default:
printf("Input value is other than 1,2 & 3 ");
}
return 0;
}
Output:
Enter value of num:2
You have entered value 2
if we don’t use the break statement after every case block then the output of this
program would be:
Enter value of num: 2
You have entered value 2
You have entered value 3
Input value is other than 1, 2 & 3

You might also like