150 likes | 332 Views
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.
E N D
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 • 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;
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.
Just “for” fun • Print a rectangle made of asterisks using a for loop: ***** ***** ***** ***** *****
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; • }
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
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; }
Nested for loops • We can put a for loop within another for loop • Example: print a triangle made out of asterisks * ** *** **** ***** ****** ******* ******** ********* **********
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); }
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
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; }
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
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; }