170 likes | 262 Views
بسم الله الرحمن الرحيم. Programming Fundamentals. Chapter No: 04 “ Loops ”. INSTRUCTOR : ILTAF MEHDI ( MCS, MCSE , CCNA, Web Developer ). Course Contents. LOOPS. “Loops” are meant for repeated processing.
E N D
بسم الله الرحمن الرحيم BY ILTAF MEHDI (MCS, MCSE, CCNA)
Programming Fundamentals Chapter No: 04“Loops” INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)
Course Contents BY ILTAF MEHDI (MCS, MCSE, CCNA)
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)
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”. BY ILTAF MEHDI (MCS, MCSE, CCNA)
The for loop • The head of the loop consists of three parts separated by semicolons. • The C for statement has the following form: • for (expression1; expression2; expression3) statement; or { block of statements } • The first is run before the loop is entered. This is usually the initialization of the loop variable. • The second is a test, the loop is exited when this returns false. • The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter. BY ILTAF MEHDI (MCS, MCSE, CCNA)
The for loop All the following are legal for-statements in C-language. • for (int x=0; ((x>3) && (x<9)); x++) • for (int x=0,y=4; ((x>3) && (y<9)); x++, y+=2) • 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; BY ILTAF MEHDI (MCS, MCSE, CCNA)
A simple C-program using for-loop #include<stdio.h> #include<conio.h> void main() { int x; for (x=0; x<=3; x++) { printf("x=%d", x); } getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)
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; BY ILTAF MEHDI (MCS, MCSE, CCNA)
A simple C-program using while-loop #include<stdio.h> #include<conio.h> void main() { int x=0; while (x<=3) { printf("x=%d", x); x++; } getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)
Write a C-program for a simple Calculator using while loop #include<stdio.h> #include<conio.h> 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)
#include<stdio.h> #include<conio.h> 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)
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); BY ILTAF MEHDI (MCS, MCSE, CCNA)
A simple C-program using while-loop #include<stdio.h> #include<conio.h> void main() { int x=0; do { printf("x=%d", x++); } while (x>0); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)
#include <stdio.h>void main() • {int i=0, n = 5;do • { printf("the numbers are: %d \n", i);i = i +1;} • while( i<n) ;} OUTPUT: the numbers are: 0 the numbers are: 1 the numbers are: 2 the numbers are: 3 the numbers are: 4 BY ILTAF MEHDI (MCS, MCSE, CCNA)
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 example: #include<stdio.h> void main() { int i; i = 0; while ( i < 20 ) { i++; if ( i == 10) break; } getch(); } 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). BY ILTAF MEHDI (MCS, MCSE, CCNA)
ContinueStatement • 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<stdio.h> void main() { int i; i = 0; while ( i < 20 ) { i++; continue; printf("Nothing to see\n"); } getch(); } In this example, the printf function is never called because of the “continue;”. BY ILTAF MEHDI (MCS, MCSE, CCNA)