0% found this document useful (0 votes)
9 views39 pages

CH 3 Loop

loops in C language gtu pps

Uploaded by

23ae9
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)
9 views39 pages

CH 3 Loop

loops in C language gtu pps

Uploaded by

23ae9
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

CH-3 LOOPING

By: Prof. (Dr.) Ajaysinh Rahod


What is loop?

◻ Loop is used to execute the block of code several times according


to the condition given in the loop. It means it executes the same
code multiple times.
loop(condition)
{
//statements
}
Output
printf("Hello\n"); Hello
printf("Hello\n"); Hello
printf("Hello\n"); Hello
printf("Hello\n"); Hello
printf("Hello\n"); Hello
if v/s while

Flowchart of v/s Flowchart of


if while

Fals Fals
condition e condition e

Tru Tru
e e
… …
Looping or Iterative Statements in C

Looping Statements are


Entry Controlled Loop: while, for
Exit Controlled Loop: do…while
Virtual Loop: goto
While loop

◻ while is an entry controlled loop


◻ Statements inside the body of while are repeatedly
executed till the condition is true
◻ while is keyword
Syntax
while(condition)
{
// Body of the while
// true part
}
Example:

◻ WAP to print 1 to n (while loop)


Program Output
1 #include <stdio.h> Enter n:10
2 void main() 1
3 { 2
4 int i,n; 3
5 i=1; 4
6 printf("Enter n:"); 5
7 scanf("%d",&n); 6
8 while(i<=n) 7
9 { 8
10 printf("%d\n",i); 9
11 i=i+1; 10
12 }
13 }
WAP to print Odd numbers between 1 to n(while loop)

Program
i n condition Process Output
1 #include<stdio.h>
1 Linking header file
2 void main() 2 Calling void main()
3 { 3
4 1 Declarations
4 int i=1, n; 5 Enter value of
5 printf(“Enter the value of n”); n
6 5 Scanning value of n 5
6 scanf(“%d ”,&n); 7 i <= n, TRUE While loop condition
7 while(i <= n) 8
9 i%2 != 0 TRUE If condition
8 { 10 Printing value of i 1
9 if(i%2!=0) 11 2 Incrementing value of i by 1
12 Jump to line:7
10 printf(“%d/n”,i);7 i <= n, TRUE While loop condition
11 i=i+1; 8 Line
9 i%2 != 0 If condition, skip line:10
12 } FALSE
13 } 11 3 Incrementing value of i by 1
12 Jump to line:7
7 i <= n, TRUE While loop condition
Output 8
Enter the value of n 9 i%2 != 0 TRUE If condition
5 10 Printing value of i 3
1 11 4 Incrementing value of i by 1
3 12 Jump to line:7
5 13 End of program
WAP to print multiplication table(while loop)

Program Output
1 #include<stdio.h> Enter n for multiplicatio
2 void main() 5 * 1 = 5
3 { 5 * 2 = 10
5 * 3 = 15
4 int i=1,n;
5 * 4 = 20
5 printf("Enter n for multiplication table:");
5 * 5 = 25
6 scanf("%d",&n); 5 * 6 = 30
7 while(i<=10) 5 * 7 = 35
8 { 5 * 8 = 40
9 printf("%d * %d = %d\n",n,i,n*i); 5 * 9 = 45
10 i=i+1; 5 * 10 = 50
11 }
12 }
WAP to print multiplication table(while loop)

Program Output
1 #include<stdio.h> Enter n for
2 void main() multiplication table:5
3 { 5 * 1 = 5
5 * 2 = 10
4 int i=1,n;
5 * 3 = 15
5 printf("Enter n for multiplication 5 * 4 = 20
6 table:"); 5 * 5 = 25
7 scanf("%d",&n); 5 * 6 = 30
8 while(i<=10) 5 * 7 = 35
9 { 5 * 8 = 40
10 printf("%d * %d = %d\n",n,i,n* 5 * 9 = 45
11 i); 5 * 10 = 50
12 i=i+1;
}
}
WAP to Sum of 5 numbers entered by user(while
loop)

Program Output
1 #include<stdio.h> Enter a number=10
2 void main() Enter a number=20
3 { Enter a number=30
Enter a number=40
4 int sum=0, i=1,n;
Enter a number=50
5 while(i<=5) Sum is=150
6 {
7 printf("Enter a number=");
8 scanf("%d",&n);
9 sum=sum+n;
10 i=i+1;
11 }
12 printf("Sum is=%d",sum);
13 }
Looping

◻ Loop Control in C, is the repeated execution of code,


for a specified number of times, if the condition is met.

loop (repeat 10
times)
{
printf("*");
}
Syntax and Logic

Syntax Logic
while(condition) int i = 1;
{ while (i <= 5)
// Body of the while {
// true part printf("%d\n", i);
} i=i+1;
}
WAP to find factors of a number(while loop)

Program
1 #include <stdio.h>
2 void main()
3 {
4 int i=1,n;
5 printf("Enter n to find factors=")
6 ;
7 scanf("%d",&n);
8 while(i<=n)
9 {
10 if(n%i==0)
11 printf("%d,",i);
12 i=i+1;
13 }
}
Output

Enter n to find
factors=12
1,2,3,4,6,12,
for Loop

◻ for is an entry controlled loop


◻ Statements inside the body of for are repeatedly executed till the
condition is true
◻ for is keyword Syntax
for (initialization; condition; updateStatement)
{
// statements
}

The initialization statement is executed only once.


Then, the condition is evaluated. If the condition is false, the for loop is
terminated.
If the condition is true, statements inside the body of for loop are executed, and
the update statement is updated.
Again the condition is evaluated.
WAP to print numbers 1 to n (for
loop)
Program
1 #include<stdio.h>
2 void main()
3 {
4 int i,n;
5 printf("Enter a number
6 :");
7 scanf("%d",&n);
8 for(i=1;i<=n;i++)
9 {
10 printf("%d\n",i);
11 }
} Output
Enter a number:5
1
2
3
4
5
WAP to find factors of a number (for loop)

Program
1 #include <stdio.h>
2 void main()
3 {
4 int i,n;
5 printf("Enter n to find factors=")
6 ;
7 scanf("%d",&n);
8 for(i=1;i<=n;i++)
9 {
10 if(n%i==0)
11 printf("%d,",i);
12 }
}
Output
Enter n to find
factors=12
1,2,3,4,6,12,
WAP to check given number is perfect or not(for loop)

Outp
1 void main(){ ut
2 int i,n,sum=0; Enter a number:6
3 printf("Enter a number:"); 1+2+3=6
4 scanf("%d",&n); 6 is a perfect number
5 for(i=1;i<n;i++) Outp
6 { ut
7 if(n%i==0) Enter a number:8
8 { 1+2+4+=7
9 printf("%d+",i); 8 is not a perfect number
10 sum=sum+i; Outp
11 } ut
12 } Enter a number:496
13 printf("=%d",sum); 1+2+4+8+16+31+62+124+248+
14 if(sum==n) =496
15 printf("\n%d is a perfect number",n); 496 is a perfect number
16 else
17 printf("\n%d is not a perfect number",
18 n);
}
do while Loop
◻ do while is an exit controlled loop.
◻ Statements inside the body of do while are repeatedly executed till
the condition is true.
◻ Do and while are keywords. Syntax
do
{
// statement
}
while (condition);

Loop body will be executed first, and then condition is checked.


If the condition is true, the body of the loop is executed again and
the condition is evaluated.
This process goes on until the condition becomes false.
If the condition is false, the loop ends.
WAP to print Odd numbers between 1 to
n(do while loop)
Program
1 void main()
2 {
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(i%2!=0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
15 } Output
Enter a number:5
1,3,5
WAP to find factors of a number(do while loop)

Program
1 void main()
2 {
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 if(n%i==0)
9 {
10 printf("%d,",i);
11 }
12 i=i+1;
13 }
14 while(i<=n);
Output
15 }
Enter a number:6
1,2,3,6,
WAP to print reverse a number(do while
loop)
Program
1 void main()
2 {
3 int n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 do
7 {
8 printf("%d",n%10);
9 n=n/10;
10 }
11 while(n!=0);
12 }

Output
Enter a number=1234
4321
goto Statement

◻ goto is an virtual loop


◻ The goto statement allows us to transfer control of the program to the
specified label.
◻ goto is keyword
Syntax Syntax
goto label label:
; .
. .
. .
. goto label;
label:

The label is an identifier. When the goto statement is


encountered, the control of the program jumps to label: and starts
executing the code.
WAP to print Odd numbers between 1 to n(goto)

Program
1 void main()
2 {
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(i%2!=0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd;
15 } Output
16 } Enter a number:5
1,3,5
WAP to find factors of a number(goto)
Program
1 void main()
2 {
3 int i=1,n;
4 printf("Enter a number:");
5 scanf("%d",&n);
6 odd:
7 if(n%i==0)
8 {
9 printf("%d,",i);
10 }
11 i=i+1;
12 if(i<=n)
13 {
14 goto odd; Output
15 } Enter a number:6
16 } 1,2,3,6,
break statement
◻ When a break statement is encountered inside a
loop, the loop is immediately terminated and the
program control resumes at the next statement
following the loop.
◻ It can be used to terminate a case in
the switch statement
break statement
#include <stdio.h>
int main ()
{ /* local variable definition */
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15)
{
/* terminate the loop using break statement */
break;
}
} return 0;
value of a: 10
} value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
continue statement
◻ C language provides a statement called continue; which can be
applied in loops.
◻ When a continue statement is encountered inside a loop, control
jumps to the beginning of the loop for the next iteration, skipping
the execution of statements inside the body of the loop for the
current iteration. Instead of terminating the loop, it forces the next
iteration of the loop to take place.
◻ While a break; transfers control out (meaning terminates) the
loop, a continue; statement transfers control to the
innermost enclosing for, while, and do statements and continues
with the next iteration.
◻ Similar to a break statement, a continue statement can be written
simply as continue;.
Syntax:
continue;
continue statement
#include<stdio.h>
i:1
void main() i:2
i:3
{ int i; i:4
for (i = 1; i < 10; i++) i:6
I:7
{ i:8
i:9
if (i % 5 == 0)
{ continue; // transfers control to for-loop, if i is divisible by 5.
}
printf("i : %d\n", i);
}
}
Nested Loop
◻ C programming allows to use one loop inside another loop.
◻ Syntax

◻ The syntax for a nested for loop statement in C is as follows −

for ( init; condition; increment )


{
for ( init; condition; increment )
{
statement(s);
}
statement(s);
}
A final note on loop nesting is that you can put any type of loop inside any
other type of loop. For example, a 'for' loop can be inside a 'while' loop
or vice versa.
Types of loops

Entry Control Loop Entry Control Loop


int i=1; int i; False
while(i<=10) for(i=1;i<=10;i
{ ++) condition
printf("%d" {
,i++); printf("%d" True
} ,i);
}
Loop Body
Types of loops

Exit Control Loop Virtual Loop

int i=1; int i=1;


do labelprint:
{ printf("%d",i++);
printf("%d",i++); if(i<=10)
} goto labelprint;
while(i<=10);

Label Statement
Loop Body

True
condition condition
False True False

goto
Pattern
Pattern

There are important points to note in pattern


1. Determine, how many rows?
2. Determine, how many numbers/characters/columns in
a row?
3. Determine, Increment/Decrement among *
the number of rows. * *
* *
1. Determine, starting in each row *
* * *
1 1 1 *
11 12 23 * *
111 123 456 *
1111 1234 78910 * *
11111 12345 *
WAP to print given pattern (nested loop)
*
**
Program
***
**** 1 void main()
***** 2 {
3 int i,j;
No. of rows: 5 4 for(i=1;i<=5;i++)
5 {
No. of characters 6 for(j=1; j<=i;
Row-1: * 7 j++)
Row-2: ** 8 {
Row-3: *** 9 printf("*"
Row-4: **** 10 );
Row-5: ***** 11 }
12 printf("\n");
Inner loop:
Increment
}
Outer loop:
}
Increment
Starting: *
WAP to print given pattern (nested loop)
1
12
Program
123
1234 1 void main()
12345 2 {
3 int i,j;
No. of rows: 5 4 for(i=1;i<=5;i++)
5 {
No. of values 6 for(j=1; j<=i; j++)
Row-1: 1 7 {
Row-2: 12 8 printf("%d",j);
Row-3: 123 9 }
Row-4: 1234 10 printf("\n");
Row-5: 12345 11 }
12 }
Inner loop:
Increment
Outer loop:
Increment
Starting: 1
WAP to print given pattern (nested loop)

Program
5
54 1 void main()
543 2 {
5432 3 int i,j;
54321 4 for(i=5;i>0;i--)
5 {
No. of rows: 5
6 for(j=5; j>=i ; j--)
No. of values 7 {
Row-1: 5 8 printf("%d",j);
Row-2: 54 9 }
Row-3: 543 10 printf("\n");
Row-4: 5432 11 }
Row-5: 54321 12 }

Inner loop: Decrement


Outer loop:
Decrement/Increment
Starting: 5
WAP to print given pattern (nested loop)
* Program
** 1 void main()
*** 2 {
**** 3 int i,j,k;
***** 4 for(i=1;i<=5;i++)
5 {
No. of rows: 5 6 for(k=5;k>i;k--)
No. of values 7 {
Row-1: ----* 8 printf(" ");
Row-2: ---** 9 }
Row-3: --*** 10 for(j=1;j<=i;j++)
Row-4: -**** 11 {
Row-5: ***** 12 printf("*");
13 }
Inner loop: Decrement 14 printf("\n"); *
Outer loop: Decrement/Increment 15 }
16 } **
Starting: -(space) ***
Ending: * ****
*****
Any Question???
Thank you…

You might also like