Download presentation
Presentation is loading. Please wait.
Published byNoreen Fields Modified over 5 years ago
1
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
2
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
3
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
4
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
5
for Repetition Statements
{body for the loop (statements) } for (int count = 1; count < = 10; count++) { printf (“%d ”, count); } No semicolon (;) after last expression 5/22/2019 KIC/Introduction to Computer Programming
6
KIC/Introduction to Computer Programming
5/22/2019 KIC/Introduction to Computer Programming
7
7 Example Program Output 1 2 3 4 5 6 7 8 9 10
8
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
9
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
10
Example Program Output 1 2 3 4 5 6 7 8 9 10
11
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
12
Comparison (While Loop Vs. For Loop)
Example 5/22/2019 KIC/Introduction to Computer Programming
13
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
14
Example
15
Comparison (While Loop Vs. do.. While Loop)
5/22/2019 KIC/Introduction to Computer Programming
16
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
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.