Loops and Iteration PDF

Document Details

JovialNickel

Uploaded by JovialNickel

Indian Institute of Technology, Kharagpur

Tags

programming loops iteration computer science

Summary

This document from Indian Institute of Technology Kharagpur provides an overview of loops and iteration in computer programming. It describes different types of loops, such as counter-controlled loops, condition-controlled loops, and input-controlled loops, and explains their applications and usage. This document also contains examples illustrating the concepts.

Full Transcript

Loops and Iteration CS10003 PROGRAMMING AND DATA STRUCTURES 1 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Repeated Execution of Groups of Instructions Loop: A group of instructions that is executed repeatedly while some condition remains true. Eac...

Loops and Iteration CS10003 PROGRAMMING AND DATA STRUCTURES 1 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Repeated Execution of Groups of Instructions Loop: A group of instructions that is executed repeatedly while some condition remains true. Each execution of that group of instructions is called an iteration of the loop. The group of instructions being repeated is called the body of the loop. How many iterations: a) Counter-controlled: A counter steps through a set of values. The loop runs once for each value. b) Achievement of some condition: A loop is repeated until some desired condition (like a mathematical equality of inequality) is established. c) Input-controlled: A loop is repeated until the user enters some special value indicating end of input. A loop may have infinite number of iterations (e.g. the desired condition is never satisfied, or the user never enters a terminating value). 2 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Counter Controlled Loop Read 5 integers and display the counter ← 1, sum ← 0 value of their sum. counter false 49 “PASS” false “FAIL” 5 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Input-Controlled Loop Read a sequence of non-negative integers from the user, and display the sum of these integers. A negative input indicates the end of input process. 6 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR When is the Continuation Condition Checked? Pre-Test Loop Post-Test Loop 7 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR while Statement The “while” statement is used to carry out looping operations, in which a group of statements is executed repeatedly, as long as some condition remains satisfied. while (condition) statement_to_repeat; condi False tion while (condition) { statement_1; True... statement_N; statement } (loop body) The condition to be tested is any expression enclosed in parentheses. The condition is evaluated, and if its value is true (non-zero), the loop-body is executed. Then the condition is evaluated again and the same sequence of operations repeats. The loop terminates when the condition evaluates to 0. Note: The while-loop will not be entered at all, if the loop-control condition evaluates to false (zero) even before the first iteration. 8 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Example Output int main() { 4 Line no : 1 int i=1, n; Line no : 2 scanf(“%d”, &n); Line no : 3 while (i 0) { 45 if (next > max) max = next; 32 7 scanf(“%lf”, &next); 5 } Output 0 printf (“The maximum number is %lf\n”, max) ; The maximum number is 45.000000 return 0; } 12 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR Find the sum of digits of a number int main() { int n, sum=0; scanf (“%d”, &n); Output while (n != 0) { 573254 sum = sum + (n % 10); The sum of digits is 26 n = n / 10; } printf (“The sum of digits is %d\n”, sum); return 0; Note how to separate out the digits } of an integer number INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 13 Compute GCD of two numbers int main() { 12 ) 45 ( 3 int A, B, temp; 36 scanf (“%d %d”, &A, &B); 9 ) 12 ( 1 if (A > B) { temp = A; A = B; B = temp; 9 } 3 ) 9 ( 3 while ((B % A) != 0) { 9 temp = B % A; B = A; 0 A = temp; } printf (“The GCD is %d”, A); Initial: A=12, B=45 Iteration 1: temp=9, B=12,A=9 return 0; Iteration 2: temp=3, B=9, A=3 } B % A = 0 🡪 GCD is 3 INDIAN INSTITUTE OF TECHNOLOGY KHARAGPUR 14 Which expressions decide how long a while loop will run? int main() Initialization of the loop control variable { int i = 1, n; Test condition scanf(“%d”, &n); while (i

Use Quizgecko on...
Browser
Browser