The document discusses different types of loops in programming like while, do-while, and for loops that allow code to be repeatedly executed based on conditional expressions. It provides examples of how to write these loops in C programming language and when each type of loop is best used. The document also briefly covers control flow statements like break, continue, and goto that can alter the normal sequential execution of code within loops.
The document discusses different types of loops in programming like while, do-while, and for loops that allow code to be repeatedly executed based on conditional expressions. It provides examples of how to write these loops in C programming language and when each type of loop is best used. The document also briefly covers control flow statements like break, continue, and goto that can alter the normal sequential execution of code within loops.
The document discusses different types of loops in programming like while, do-while, and for loops that allow code to be repeatedly executed based on conditional expressions. It provides examples of how to write these loops in C programming language and when each type of loop is best used. The document also briefly covers control flow statements like break, continue, and goto that can alter the normal sequential execution of code within loops.
The document discusses different types of loops in programming like while, do-while, and for loops that allow code to be repeatedly executed based on conditional expressions. It provides examples of how to write these loops in C programming language and when each type of loop is best used. The document also briefly covers control flow statements like break, continue, and goto that can alter the normal sequential execution of code within loops.
AND FOR-LOOP Why do while and while do Programs using if statement is sequential Program will flow from top to bottom and some blocks are executed based on the condition This sequential system lacks repetition An if statement checks if an expression is true or false, and then runs the code inside the statement only if it is true. The code inside the loop is only run once... A while statement is a loop. Basically, it continues to execute the code in the while statement for however long the expression is true. When to use loop There are two types of loop: While-do Do-while
Loop is best use when you don't know exactly how
many times you may have to loop through a condition - if you know exactly how many times you want to test a condition Loop will go to the previous position as indicated at the start of the loop Control flow while-do (1/4) Example of boiling water Turn on stove Put pan on the stove Look at the water, if not boiling Repetition Then wait, go into previous activity Not knowing how long Turn off stove Remove pan Control flow while-do (2/4) Flow diagram of while do Control flow while-do (3/4) Example format of while-do: while (test expression) { modular block to be executed }
Example of calculating factorial
5! = 5*4*3*2*1 Control flow while-do (4/4) #include <stdio.h> int main() { int number,factorial; printf("Enter a number.\n"); scanf("%d",&number); factorial=1; while (number>0) { /* while loop continues until test condition number>0 is true */ factorial=factorial*number; --number; } printf("Factorial=%d",factorial); return (0); } Example program The above program is available on Linux system Run and try during class Other program and guessing a number Name of programs Random-guess.c Control-while-do.c Control flow do-while (1/4) do...while loop is very similar to while loop difference between these two loops is that, in while loops, test expression is checked at first but, in do...while loop code is executed at first then the condition is checked the code are executed at least once in do...while loops. Control flow do-while (2/4) Flow diagram of do-while Control flow do-while (3/4) Format of do-while: do { some code/s; } while (test expression); Example, adding number : Ask for a number Add the number to the sum Display the result Ask again, quit if zero Control flow do-while (4/4) /*C program to demonstrate the working of do...while statement*/ #include <stdio.h> int main() { int sum=0,num; do /* Codes inside the body of do...while loops are at least executed once. */ { printf("Enter a number\n"); scanf("%d",&num); sum+=num; /* sum = sum+num;*/ } while(num!=0); printf("sum=%d",sum); return (0); } Apa yang terjadi (1/2) #include <stdio.h> int main() { int factorial=1,number=1; while (1) { factorial=factorial*number; number++; } printf(“Factorial = %d\n“,factorial); return (0); } Apa yang terjadi (2/2) #include <stdio.h> int main() { int sum=0,num; do /* Codes inside the body of do...while loops are at least executed once. */ { printf("Enter a number\n"); scanf("%d",&num); sum+=num; } while(1); printf("sum=%d",sum); return (0); } Program example Program example is the same as the above example Name of program is control-do-while.c Control flow for-loop (1/4) Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied Loops are used in performing repetitive work in programming Suppose you want to execute some codes 100 times, it can be performed by writing that code once and repeat the execution 100 times using loop Control flow for-loop (2/4) Flow diagram of control flow for-loop Control flow for-loop (3/4) Format of for loop: for(initial expression; test expression; update expression) { code/s to be executed; } Example of calculating factorial 5! = 5*4*3*2*1 Control flow for-loop (4/4) #include <stdio.h> int main() { int number, count, factorial=1; printf("Enter the value of n.\n"); scanf("%d",&number); for(count=1;count<=number;++count) //for loop terminates if count>n { factorial *= count; /* this statement is equivalent to factorial = factorial*count */ } printf(“Factorial = %d\n“, factorial); return (0); } Control flow goto (1/5) goto allows you to jump unconditionally to arbitrary part of your code (within the same function, later about function). the location is identified using a label a label is a named location in the code. It has the same form as a variable followed by a ’:’ goto statement is used for altering the normal sequence of program execution by transferring control to some other part of the program Control flow goto (2/5) Go To Statement Considered Harmful Communications of the ACM 11(3),1968 Excess use of goto creates spaghetti code. Using goto makes code harder to read and debug. Any code that uses goto can be written without using one. TRY NOT TO USE GOTO AT ANY COST IT IS NOT A GOOD PROGRAMMING PRACTICE Control flow goto (3/5) Flow diagram of goto statement both going forward and backward Control flow goto (4/5) Format of goto: goto label; code to be executed; code to be executed; label: codes to be executed; Control flow goto (5/5) /* C program to demonstrate the working of goto statement.*/ # include <stdio.h> void main() { float num,average,sum; int i,n; printf("Maximum no. of inputs: "); scanf("%d",&n); for(i=1;i<=n;++i) { printf("Enter n%d: ",i); scanf("%f",&num); if(num<0.0) goto jump; /* control of the program jumps to label jump */ sum=sum+num; } jump: average=sum/(i-1); printf("Average: %.2f",average); } Control flow break (1/2) Utilized to terminate loop immediately when encountered for three loops: for, while and do...while Usually used with conditional if statement Flow diagram: Control flow break (2/2) /* C program to demonstrate the working of break statement by terminating a loop, if user inputs negative number*/ # include <stdio.h> int main() { float num,average,sum; int i,n; printf("Maximum no. of inputs\n"); scanf("%d",&n); for(i=1;i<=n;++i) { printf("Enter n%d: ",i); scanf("%f",&num); if(num<0.0) break; //for loop breaks if num<0.0 sum=sum+num; } average=sum/(i-1); printf("Average=%.2f",average); return (0); } Control flow continue (1/2) To skip some statements inside the three loops: for, while and do...while Usually used with conditional if statement (similar to break) Flow diagram Control flow continue (2/2) //program to demonstrate the working of continue statement in C programming # include <stdio.h> void main() { int i,num,product; for(i=1,product=1;i<=4;++i) { printf("Enter num%d:",i); scanf("%d",&num); if(num==0) continue; / *In this program, when num equals to zero, it skips the statement product*=num and continue the loop. */ product*=num; } printf("product=%d",product); } Try example program Each all try the programs yourselves Raise your hand when finished If you have problems, also raise your hand Assignment prime number: A Prime Number can be divided evenly only by 1, or itself First prime number is ‘2’ and the only even prime number Other prime numbers will always be odd numbers Check the prime candidate by dividing the candidate by odd numbers until candidate/2 Write a program to check prime number Write a program to find the first 10 prime numbers Expand program to find the first 10000 prime numbers Fastest program will win a small price