Lec 6 Loop Statements Introduction to Computer Programming KIC 5/22/2019 Department of Information Technology. Introduction to Computer Programming Lec 6 Loop Statements KIC/Introduction to Computer Programming 5/22/2019 Introduction to Computer Programming
KIC/Introduction to Computer Programming Outlines Repetition Statements for repetition statements while repetition statements do…while repetition statements 5/22/2019 KIC/Introduction to Computer Programming
KIC/Introduction to Computer Programming All programs can be broken down into 3 controls: Sequence–handled automatically by compiler Selection – if, if…else , switch Repetition – for, while, do…while 5/22/2019 KIC/Introduction to Computer Programming
Repetition Statements Computer executes group of instructions repeatedly while some condition remains true Counter-controlled repetition: Definite repetition: know how many times loop will execute Control variable used to count repetitions Counter-controlled repetition requires: The name of a control variable (or loop counter) The initial value of the control variable An increment (or decrement) by which the control variable is modified each time through the loop A condition that tests for the final value of the control variable (i.e., whether looping should continue) 5/22/2019 KIC/Introduction to Computer Programming
for Repetition Statements {body for the loop (statements) } 12345678910 for (int count = 1; count < = 10; count++) { printf (“%d ”, count); } No semicolon (;) after last expression 5/22/2019 KIC/Introduction to Computer Programming
KIC/Introduction to Computer Programming 5/22/2019 KIC/Introduction to Computer Programming
7 Example Program Output 1 2 3 4 5 6 7 8 9 10
What does the following program print? #include <stdio.h> int main(){ int i,j=1; for (i=0;i<10;i+=2) { j++; printf("%d\n", j); } getchar(); j For Loop Start 1 i = 0 2 i = 2 3 i = 4 4 i = 6 5 i = 8 6
while Repetition Statements The format Initialization (counter) while (<testing> or <condition>) { body for the loop (statements) increment for the counter } Example: int counter = 1; // initialization while ( counter <= 10 ) { // repetition condition printf( "%d\n", counter ); ++counter; // increment } The statement int counter = 1; Names counter Defines it to be an integer Reserves space for it in memory Sets it to an initial value of 1 5/22/2019 KIC/Introduction to Computer Programming
Example Program Output 1 2 3 4 5 6 7 8 9 10
Comparison (While Loop Vs. For Loop) If we want to display the numbers from 0 to 10 then both loop statements will be as below: int count = 0; while ( count < = 10 ) { printf (“ \n %i ”, count); count++; } Initialization Increment int count; for (count = 0; count < = 10; count++) { printf (“ \n %i ”, count); } 5/22/2019 KIC/Introduction to Computer Programming
Comparison (While Loop Vs. For Loop) Example 5/22/2019 KIC/Introduction to Computer Programming
do…while Repetition Statements The format Initialization (counter) do { body for the loop (statements) increment for the counter } while (<testing> or <condition>); 1 2 3 4 5 6 7 8 9 10 Example int count = 0; do { printf (“ \n %d ”, count); count++; or count=count+1; } while (count<=10); 5/22/2019 KIC/Introduction to Computer Programming
Example
Comparison (While Loop Vs. do.. While Loop) 5/22/2019 KIC/Introduction to Computer Programming
KIC/Introduction to Computer Programming Questions ( (6 Describe what happens in the following loop: int d=7 int c=2 while(c!=d){ c=c+2;} 5/22/2019 KIC/Introduction to Computer Programming