Download presentation
Presentation is loading. Please wait.
Published byRuth Berniece Gordon Modified over 9 years ago
1
BY ILTAF MEHDI (MCS, MCSE, CCNA)1
2
INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
3
BY ILTAF MEHDI (MCS, MCSE, CCNA)3 Course Contents Chapter NoChapter NamePage No 1. The Turbo C Programming Environment 01 2. C Building Blocks 27 3. Decisions 99 4. Loops 67 5. Functions 135 6. Arrays and Strings 179
4
LOOPS “Loops” are meant for repeated processing. “To execute a set of instructions repeatedly until a particular condition is being satisfied”. In every programming language, also in the C programming language, there are circumstances where you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf functions, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times. Three types of looping statements are there: 1) For Loop 2) While Loop 3) Do while Loop BY ILTAF MEHDI (MCS, MCSE, CCNA)4
5
The for loop “The for loop is frequently used, usually where the loop will be executed to a fixed number of times”. OR “The for loop works well where the number of iterations of the loop is known before the loop is entered”. 5BY ILTAF MEHDI (MCS, MCSE, CCNA)
6
The for loop The head of the loop consists of three parts separated by semicolons. The C for statement has the following form: for (expression 1 ; expression 2 ; expression 3 ) statement; or { block of statements } 1. The first is run before the loop is entered. This is usually the initialization of the loop variable. 2. The second is a test, the loop is exited when this returns false. 3. The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter. 6BY ILTAF MEHDI (MCS, MCSE, CCNA)
7
The for loop All the following are legal for-statements in C-language. 1. for (int x=0; ((x>3) && (x<9)); x++) 2. for (int x=0,y=4; ((x>3) && (y<9)); x++, y+=2) 3. for (int x=0,y=4,z=40; z; z/=10) The first example shows that multiple conditions can be written. The second example shows that multiple expressions can be separated a comma (,). In the third example the loop will continue to iterate until z becomes 0; 7BY ILTAF MEHDI (MCS, MCSE, CCNA)
8
A simple C-program using for-loop #include void main() { int x; for (x=0; x<=3; x++) { printf("x=%d", x); } getch(); } 8BY ILTAF MEHDI (MCS, MCSE, CCNA)
9
The while loop The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed. The while loop continues to loop until the conditional expression becomes false. The condition is tested upon entering the loop. while (expression) statement; 9BY ILTAF MEHDI (MCS, MCSE, CCNA)
10
A simple C-program using while-loop #include void main() { int x=0; while (x<=3) { printf("x=%d", x); x++; } getch(); } 10BY ILTAF MEHDI (MCS, MCSE, CCNA)
11
Write a C-program for a simple Calculator using while loop #include void main() { clrscr(); float num1=1.0,num2=1.0; char op; while (!(num1==0.0 && num2==0.0)) { printf("\n Type Number, Enter Operator(*, -, +, /), Number\n"); scanf ("%f %c %f", &num1, &op, &num2); if(op=='+') printf("= %f", num1 + num2); else if(op=='-') printf("= %f", num1 - num2); else if(op=='*') printf("= %f", num1 * num2); else if(op=='/') printf("= %f", num1 / num2); } printf("\n\n now the control is out of the while loop"); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)11
12
Write C-program that will sum the integers #include void main(void) { clrscr(); int sum=0, i=1, count=0; printf("\n Enter the number of integers you want to sum:"); scanf ("%d", &count); while(i<=count) { sum += i++; } printf("Total of the first %d numbers is %d\n", count, sum); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)12
13
The do while loop The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once. The do...while loop checks the conditional expression only after the repetition part is executed. The do...while loop is used when you want to execute the loop body at least once. do statement; while (expression); 13BY ILTAF MEHDI (MCS, MCSE, CCNA)
14
A simple C-program using while-loop #include void main() { int x=0; do { printf("x=%d", x++); } while (x>0); getch(); } 14BY ILTAF MEHDI (MCS, MCSE, CCNA)
15
Write a c-program using do-while loop #include #include void main() { int i=0, n = 5; do { printf("the numbers are: %d \n", i); i = i +1; } while( i<n) ; getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)15
16
Break Statement To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following c-program: #include void main() { clrscr(); int i = 0; while ( i < 20 ) { i++; if ( i == 10) { break ; } printf("\n the value of i is: %d", i); } printf("\n\n whlie loop body will not be executed any more."); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)16 In this example, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break).
17
Continue Statement With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below: #include void main() { int i = 0; while ( i < 20 ) { i++; continue; printf("Nothing to see\n"); } getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)17 In this example, the printf function is never called because of the “continue;”.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.