Study Title: Exploration of Loop Constructs in Programming
Objective:
This study aims to examine the various types of loops in programming, their structures, and their applications. By understanding how loops function, students and programmers can enhance their ability to write efficient and effect...
Study Title: Exploration of Loop Constructs in Programming
Objective:
This study aims to examine the various types of loops in programming, their structures, and their applications. By understanding how loops function, students and programmers can enhance their ability to write efficient and effective code.
Overview:
Loops are fundamental programming constructs that allow for the repeated execution of a block of code based on specific conditions. This study focuses on three primary types of loops: for loops, while loops, and do-while loops. Each type has unique characteristics, use cases, and syntactical structures that cater to different programming needs.
Key Areas of Focus:
Types of Loops:
For Loops: Ideal for scenarios where the number of iterations is known beforehand.
While Loops: Useful for situations where the number of iterations is uncertain and depends on a condition.
Do-While Loops: Guarantees that the loop will execute at least once, regardless of the condition.
Applications:
Iterating through data structures (arrays, lists).
Performing repeated calculations or operations.
Implementing algorithms requiring repetitive steps.
Best Practices:
Avoiding infinite loops by ensuring proper exit conditions.
Utilizing loop control statements (break and continue) for refined control over loop execution.
Conclusion:
Understanding loops is crucial for efficient programming and algorithm design. This study provides insights into their mechanics and encourages best practices to enhance coding proficiency.
Size: 97.24 KB
Language: en
Added: Oct 16, 2024
Slides: 33 pages
Slide Content
Looping statements For ( Initializes, condition checking, executes the body statements , at last update) While ( Initializes, condition checking, executes the body statements , updation done inside the body.) do-while( Executes the body statements , condition checking ) Further these loops classified into two types Entry Controlled loops: In Entry controlled loops the test condition is checked before entering the main body of the loop. For Loop and While Loop is Entry-controlled loops. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the loop body. The loop body will execute at least once, irrespective of whether the condition is true or false. do-while Loop is Exit Controlled loop. For Loop Syntax :- for (initialization; condition; updating Statement) { // statements inside the body of loop }
For loop For loop is also known as definite loop ,because programmer knows exactly how many times loop will repeat. if we place a ; after for loop it will consider as null statement. Complier will not return any error in this situation, rather treated it as null statement which causes the time delays Multiple initialization separate with comma. Initialization can also be skipped by just putting the semicolon.
Working of for loop Initialization Condition For loop body statement Updating False True Exit
#include <stdio.h> int main() { int i ; for( i =1;i<=10;i++) { printf ("%d\n", i ); } return 0; } #include <stdio.h> int main() { int i ; for( i =1;i<=10;i++) { printf ("%d\n", i *2); } return 0; }
PRINT YOUR NAME 5 TIMES #include < stdio.h > int main() { int a; for(a=1;a<=10;a++) { printf ("nav\n"); } return 0; } Condition is not mentioned( infinite loop) #include <stdio.h> int main() { for(int a = 0; ; a++) { printf ("%d ",a); } return 0; }
I nfinite loop #include <stdio.h> int main() { for(int a = 10; a> 0 ; a++) { printf ("%d ",a); } return 0; } I nfinite loop #include <stdio.h> int main() { for(int a = 0; a < 10 ; a--) { printf ("% d",a ); } return 0; }
#include <stdio.h> int main() { int a,b,c ; for(a=0,b=0;a<4,b<4;a++,b++) { printf ("%d %d \n", a,b ); } } #include<stdio.h> int main() { int a,i ; printf("enter value of a\n"); scanf("% d",&a ); for( i =2;i< a;i ++) { if( a%i ==0) break; } if( i ==a) { printf("prime numbre %d", i ); } else { printf("Not a prime numbre "); } return 0; }
#include<stdio.h> int main() { int i ; for( i =32;i<=126;i++) { printf("The character attached with ASCCI %d is %c\n ", i,i ); } return 0; } #include <stdio.h> int main() { int sum=0, avg ; for (int i =1;i<=300;i++) { if(i%7==0) { sum= sum+i ; avg = sum/ i ; } } printf("sum is %d\ n",sum ); return 0; }
While Loop In which condition is evaluated before processing a body of the loop. If a condition is true then and only then the body of a loop is executed. After the body of a loop is executed then control again goes back at the beginning, and the condition is checked if it is true, the same process is executed until the condition becomes false. Once the condition becomes false, the control goes out of the loop. It is also known as top checking loop, since control condition placed at the first line. ** if condition not updated it will go into the infinite loop. Syntax while (condition) { statements; }
#include<stdio.h> int main() { int a=1; while(a<=10) { printf ("%d\ n",a ); a++; } return 0; } #include<stdio.h> int main() { int a=1,sum=0;; while(a!=0) { printf("enter value of a\n"); scanf("% d",&a ); sum = sum+a ; } printf("SUM IS % d",sum ); return 0; }
if condition not updated it will go into the infinite loop. #include <stdio.h> int main() { int i =1; while( i ) { printf ("%d", i ); i ++; } return 0; } WAP to calculate the sum of numbers from m to n. #include<stdio.h> int main() { int m,n,sum =0; printf ("enter the value of m"); scanf ("% d",&m ); printf ("enter the value of n"); scanf ("% d",&n ); while(m<=n) { sum = sum+m ; printf ("sum = %d\ n",sum ); m =m+1; } return 0; }
SUM of first 10 numbers #include<stdio.h> int main() { int a=0,sum ; while(a<=10) { sum = sum+a ; a =a+1; } printf ("sum = %d\ n",sum ); return 0; }
Do-While loop It is similar to the while loop except that the condition is always executed after the body of a loop. It is also called an exit-controlled loop. In do-while loop, the while condition is written at the end and terminates with a semi-colon (;) Syntax do { statements } while (expression);
#include<stdio.h> int main() { int a=1; do { printf ("%d\ n",a ); a++; }while(a<=10); return 0; } #include <stdio.h> int main() { int i = 0; do { printf ("name\n"); i ++; } while ( i < 5); return 0; }
Nested for loop Loop inside loop For loop used to control the number of times that particular set of statements executed. Another outer loop could be used to control the number of times that a whole loop is repeated. Syntax:- for ( initialization; condition; increment ) //outer loop { for ( initialization; condition; increment )//inside loop { // statement of inside loop } // statement of outer loop }
#include <stdio.h> int main() { int i,j ; for ( i =1;i<=5;i++) { printf ("\n"); for (j=1;j<=10;j++) { printf ("*"); } } return 0; } #include <stdio.h> int main() { int i,j ; for ( i =1;i<=5;i++) { printf ("\n"); for (j=1;j<= i;j ++) { printf ("*"); } } return 0; }
#include <stdio.h> int main() { int i,j ; for ( i =1;i<=5;i++) { printf ("\n"); for (j=1;j<= i;j ++) { printf ("% d",j ); } } return 0; } #include <stdio.h> int main() { int i,j ; for ( i =1;i<=5;i++) { printf ("\n"); for (j=1;j<= i;j ++) { printf ("%d", i ); } } return 0; }
Floyd's Triangle #include <stdio.h> int main() { int i,j,k =1; for ( i =1;i<=4;i++) { printf ("\n"); for (j=1;j<= i;j ++) { printf ("% d",k ++); } } return 0; } #include <stdio.h> int main() { int i,j ; for( i =1;i<=5;i++) { for(j=5;j>= i;j --) { printf ("*"); } printf ("\n"); } return 0; }
#include<stdio.h> int main() { int i,j ; for( i =0;i<5;i++) { for(j=5;j> i;j --) { printf(" "); } for(j=0;j<= i;j ++) { printf("*"); } printf("\n"); } return 0; } #include<stdio.h> int main() { int i,j ; for( i =0;i<5;i++) { for(j=0;j<5;j++) { if ( i ==0|| i ==4) { printf("*"); } else { if(j==0||j==4) { printf("*"); } else { printf(" "); } } } printf("\n"); } return 0; }
#include <stdio.h> int main() { int i,j,k,l ; for ( i =0;i<=8;i++) { for (j=8;j> i;j --) { printf (" "); } for (k=0;k< i;k ++) { printf ("*"); printf (" "); } printf ("\n"); } for ( i =0;i<=6;i++) { for (j=0;j<= i;j ++) { printf (" "); } for (k=6;k>= i;k --) { printf ("*"); printf (" "); } printf ("\n"); } return 0; }
#include <stdio.h> int main() { int i =1; while( i <=3) { int j=1; while(j<=3) { printf ("*"); j++ ; } i ++; printf(" \n"); } return 0; } #include <stdio.h> int main() { int i =1; while( i <=3) { int j=1; while(j<= i ) { printf("*"); j++ ; } i ++; printf(" \n"); } return 0; }
Nested do-while loop do{ do{ // statement of inside loop }while(condition); // statement of outer loop }while(condition);
#include <stdio.h> int main() { int i =1; do { int j=1; do { printf ("*"); j++ ; } while(j<=5); printf ("\n"); i ++; } while( i <=3); return 0; } #include <stdio.h> int main() { int i =1; do { int j=1; do { printf ("*"); j++ ; } while(j<= i ); printf ("\n"); i ++; } while( i <=3); return 0; }
Jumping statements Jump Statement makes the control jump to another section of the program unconditionally when encountered. It is usually used to terminate the loop or switch-case instantly. It is also used to escape the execution of a section of the program. goto continue break
Break; #include <stdio.h> int main() { int i ; for ( i = 1; i <= 15; i ++) { printf ("%d\n", i ); if ( i == 10) break; } return 0; } continue #include <stdio.h> int main() { int i , j; for ( i = 1; i < 3; i ++) { for (j = 1; j < 5; j++ ) { if (j == 2) continue; printf ("%d\n", j); } } return 0; }
Goto Statement and Reverse goto #include <stdio.h> int main() { int i , j; for ( i = 1; i < 5; i ++) { if ( i == 2) goto some; printf ("%d\n", i ); } some: printf ("Two"); return 0; } #include <stdio.h> int main() { int a,i ; table: printf("Enter the number for printing the table "); scanf("% d",&a ); for( i =1;i<=10;i++) { printf("%d x %d = %d\n", a,i,a * i ); if( i ==10) goto table; } return 0; }
Palindrome number #include <stdio.h> int main() { int num , mod , rev=0,temp; printf ("Enter an number\n"); scanf ("%d", & num ); temp = num ; while (temp > 0) { mod = temp % 10; rev = rev *10+mod; temp = temp/10; } if ( num == rev) printf ("%d is a palindrome number.\n", num ); else printf ("%d isn't a palindrome number.\n", num ); return 0; } Armstrong number is a number that is equal to the sum of cubes of its digits . #include <stdio.h> int main() { int num , mod , sum=0,temp; printf ("Enter an number\n"); scanf ("%d", & num ); temp = num ; while (temp > 0) { mod = temp % 10; sum = sum+(mod*mod*mod); temp = temp/10; } if ( num == sum) printf ("%d is a armstrong number.\n", num ); else printf ("%d isn't a armstrong number.\n", num ); return 0; }
palindrome number using for loop #include <stdio.h> int main() { int num , rem , rev=0,temp; printf("Enter an number\n"); scanf("%d", & num ); temp = num ; for (temp = num;temp >0;temp=temp/10) { rem = temp % 10; rev = rev *10+rem; } if ( num == rev) printf("%d is a palindrome number.\n", num ); else printf("%d isn't a palindrome number.\n", num ); return 0; }
Fibonacci Series #include <stdio.h> int main() { int a=0, b=1, SUM, i,n ; printf ("enter number for how long u want to print the fibonacci seriers "); scanf ("% d",&n ); printf("%d %d ", a,b ); for( i = 2; i <= n; i ++) { SUM = a + b; printf ("%d ", SUM); a = b; b = SUM; } return 0; } Factorial #include<stdio.h> int main() { int i,f =1,n; printf ("Enter a number: "); scanf ("% d",&n ); for( i =1;i<= n;i ++){ f=f* i ; } printf ("Factorial of %d is: %d", n,f ); return 0; }
Sum of series 1+2+3……..n #include<stdio.h> int main() { int i,sum =0,n; printf ("Enter a number: "); scanf ("% d",&n ); for( i =1;i<= n;i ++){ sum+= i ; } printf ("sum is: % d",sum ); return 0; } Sum of series 1^2+2^2+3^2....n^2 #include<stdio.h> int main() { int i,sum =0,n; printf ("Enter a number: "); scanf ("% d",&n ); for( i =1;i<= n;i ++){ sum=sum+( i * i ); } printf ("sum is: % d",sum ); return 0; }
1+1/2+1/3+1/4………..1/n #include<stdio.h> int main() { int i,sum =0,n; printf ("Enter a number: "); scanf ("% d",&n ); for( i =1;i<= n;i ++){ sum=sum+1/i; // use typecasting } printf ("sum is: % d",sum ); return 0; } X+x^2+x^3+x^4…… x^n #include<stdio.h> #include<math.h> int main() { int i,n,x,sum =0; printf ("Enter a number: "); scanf ("%d % d",&n,&x ); for( i =1;i<= n;i ++){ sum= sum+pow ( x,i ); } printf ("sum is: % d",sum ); return 0; }