1 / 15

Loops for Iterative code execution

Loops for Iterative code execution. Loops. Loops are used to repeat a block of code. Unless specified otherwise, all statements within the loop are executed sequentially Three types of loops: for while do … while…. Body of loop. Condition. True. False. for loop.

elinor
Download Presentation

Loops for Iterative code execution

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Loops for Iterative code execution

  2. Loops • Loops are used to repeat a block of code. • Unless specified otherwise, all statements within the loop are executed sequentially • Three types of loops: • for • while • do … while… Body of loop Condition True False

  3. for loop • Used when you know the number of iterations for the code block for (variable initialization; test expression; variable update) { statements to execute while the test expression is true; } Statements after the for loop;

  4. Execution of a for loop for (variable initialization; test expression; variable update) { statements to execute while the test expression is true; } Statements after the for loop; 1) Initialize the loop variable. 2) Evaluate the test expression. 3) If it's true, execute statements 1 through n. Update loop variable. Then go back to step #2 4) If it's false, skip over the loop body and continue execution with statements after the loop.

  5. Just “for” fun • Print a rectangle made of asterisks using a for loop: ***** ***** ***** ***** *****

  6. Example – Print first n positive numbers (ascending order) • #include <stdio.h> • int main() • { • int num, i; • printf("Enter the number of positive numbers to print\n"); • scanf("%d", &num); • for (i = 1; i <= num; i++) • { • printf("%d\n", i); • } • system("pause"); • return 0; • }

  7. Example – Print first n positive numbers (descending order) Example – sum of n consecutive even numbers Example – Display all factors of a number Example – Determine if a number is prime

  8. Example – Determine HCF of two numbers /* Muhammad Faisal Amjad Program to Find Highest Common Factor. COP 3223H - Spring 2014 */ #include <stdio.h> int main() { int num1, num2, i, hcf; printf("Enter two integers: "); scanf("%d %d", &num1, &num2); for(i=1; i <= num1 && i <= num2; ++i) { if(num1 % i == 0 && num2 % i == 0) // Checking whether i is a factor of both numbers { hcf = i; } } printf("H.C.F of %d and %d is %d\n\n", num1, num2, hcf); system("pause"); return 0; }

  9. Nested for loops • We can put a for loop within another for loop • Example: print a triangle made out of asterisks * ** *** **** ***** ****** ******* ******** ********* **********

  10. while loop • A while loop statement repeatedly executes a block of code as long as a given condition (represented by the test-expression) is true. • while loop is used for zero or more iterations of a block of code • Syntax: while (test-expression) { statement(s); }

  11. Example – while loop /* Muhammad Faisal Amjad Program to add user-provided numbers until 0 is entered by the user COP 3223H - Spring 2014 */ intmain() { int sum=0,num; printf("Enter a number\n"); scanf("%d",&num); while(num!=0) /* Code inside the body of a while loop is executed zero or more times. */ { sum+=num; printf("Enter a number\n"); scanf("%d",&num); } printf("sum=%d",sum); return 0; } Notice NO semi-colon here

  12. Example – how many times a number is divisible by 2 /* Muhammad Faisal Amjad Program to Find how many times a number is divisible by 2 COP 3223H - Spring 2014 */ #include <stdio.h> int main() { intnum, temp, count = 0; printf("Enter a number: "); scanf("%d", &num); temp = num; while(temp % 2 == 0) { count++; temp /= 2; } printf("\nThe number %d is %d times divisible by 2\n\n", num, count); system("pause"); return 0; }

  13. do - while loop • A do - while loop statement repeatedly executes a block of code as long as a given condition (represented by the test-expression) is true. • do - while loop is used for one or more iterations of a block of code • Syntax: do { statement(s); } while (test-expression); Notice the semi-colon here

  14. Example – do - while loop /* Muhammad Faisal Amjad Program to add user-provided numbers until 0 is entered by the user COP 3223H - Spring 2014 */ intmain() { int sum=0,num; do /* Code inside the body of a while loop is executed one or more times. */ { printf("Enter a number\n"); scanf("%d",&num); sum+=num; }while (num!=0); printf("sum=%d",sum); return 0; }

  15. Loop Control Statements

More Related