לולאות 02 יולי 15 02 יולי 15 02 יולי 1502 יולי 15 02 יולי 15 02 יולי 1502 יולי 15 02 יולי 15 02 יולי 15 1 Department of Computer Science-BGU.

Slides:



Advertisements
Similar presentations
For loops For loops are controlled by a counter variable. for( c =init_value;c
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
Computer Science 1620 Loops.
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
LOOP (Part 2) for while do-while 1. TK1913-C Programming2 TK1913-C Programming 2 Loop : for Loop : for Condition is tested first Loop is controlled by.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 2: Control Flow.
Introduction to Computer Programming in c
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 5: Structured Programming.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
do - while  while: Execute the body of the loop at least once
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
Chapter 5: Structured Programming
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Chapter 5: Structured Programming
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Repetition and Iteration ANSI-C. Repetition We need a control instruction to allows us to execute an statement or a set of statements as many times as.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Iteration Hussein Suleman UCT Dept of Computer Science CS115 ~ 2004.
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
Java iteration statements ● Iteration statements are statements which appear in the source code only once, but it execute many times. ● Such kind of statements.
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
BY ILTAF MEHDI (MCS, MCSE, CCNA)1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI (MCS, MCSE, CCNA)2 Chapter No: 04 “Loops”
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
UCT Department of Computer Science Computer Science 1015F Iteration
Control Structures (Repetition structure) Jump Statements
Chapter 5: Structured Programming
CSE 220 – C Programming Loops.
REPETITION CONTROL STRUCTURE
Chapter 4 - Program Control
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Chapter 2.2 Control Structures (Iteration)
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Looping.
- Additional C Statements
Loops in C.
Incremental operators
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU

מבנה הלולאה ? ? 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 2 Department of Computer Science-BGU

לולאה (loop) Used to repeat the same instruction(s) over and over again. C provides some flexible ways of deciding how many times to loop, or when to exit a loop. for, while, do-while loops. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 3 Department of Computer Science-BGU

While while (condition) { statement(s); } The statements are executed as long as condition is true When the condition is no longer true, the loop is exited. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 4 Department of Computer Science-BGU

דוגמא - factorial #include void main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; while (i<=n) { fact = fact*i; i++; } printf("the factorial is %d\n", fact); } This is a counter Every iteration i is incremented by 1. (Equivalent to i=i+1) 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 5 Department of Computer Science-BGU

Exercise Input –  Two integers – A and B Output –  How many times A contains B  This is the result of the integer division A/B Note –  Do not use the division operator! 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 6 Department of Computer Science-BGU

Solution #include void main() { int a, b, res; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); res = 0; while ( (res+1) * b <= a) res = res + 1; printf("%d / %d = %d", a, b, res); } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 7 Department of Computer Science-BGU

Additional Exercise Input –  Two integers – A and B Output –  Only if A and B are Digits  Otherwise return to input. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 8 Department of Computer Science-BGU

Solution #include void main() { int a, b, flag = 1; while ( flag){ flag = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); if(a > 9 || a < 0) flag = 1; else if (b > 9 || b < 0 ) flag = 1; } // executing statements using a and b } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 9 Department of Computer Science-BGU

Compact Solution #include void main() { int a, b, flag = 1; while ( flag){ flag = 0; printf("Please enter two numbers.\n"); scanf("%d%d", &a, &b); if(a > 9 || a 9 || b < 0 ) flag = 1; } // executing statements using a and b } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

לולאה do - while do { statement(s) } while (expression); Similar to while loops  Except the condition is evaluated after the loop body  The loop body is always executed at least once, even if the expression is never true (equals zero) 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

לקלוט נתון לפי הדרישה #include void main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i<=0) printf("That's not a positive number! Try again.\n"); } while (i<=0); /* The program continues.... */ } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

לולאה for - מבנה /* Program before the for loop */ for( ; ; ) { loop body; } /* Continue the program after for loop */ initiali zeconditionincrement True False 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

דוגמא For loops are controlled by a counter variable. for( c =init_value; c<=fin_value ; c+=increment_value) { loop body; } c is a counter. c is a incremented after every iteration (can also be decreased!) 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

for vs. while for is equivalent to while… Any for loop can be converted to while loop and vice versa. If we want to perform something for a predefined number of times, better use for. If we just wait for something to happen (not after a certain number or iterations), better use while. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

break When break is encountered, the loop is exited regardless of whether the condition is still true. The program then continues to run from the first line after the while loop. If called within a nested loop, break breaks out of the inner loop only. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

שימושים ב - break void main () {. loop(expression) {. break;. } // continue with the program } void main () {. loop1(expression1) {. loop2(expression2) {. break;. } // end loop2 // continue with loop1 } // end loop1. } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

לקלוט נתון לפי הדרישה עם break #include void main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i >= 0) break; // after break no needed else printf("That's not a positive number! Try again.\n"); } while (1); /* The program continues.... */ } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

continue When continue is encountered, the rest of the loop is ignored. The program then continues to run from the beginning of the loop. Rarely used. Can usually be replaced by an appropriate if- else statement. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

שימוש ב - continue void main () {. loop(expression) {. continue;. } // continue with the program } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

תרגיל פשוט כתוב תכנית שמדפיסה את טבלת הכפל פתרון : #include void main(){ int i,j; printf("Printing the multiplication table: \n\r"); for (i = 1 ; i <= 4 ; i++) { printf("\n\r"); for (j = 1 ; j <= 5 ; j++) printf ("(%2d, %2d)", i, j); } } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 Department of Computer Science-BGU

תרגיל Write a program that accepts a number from the user, and checks whether it is prime. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

פתרון #include void main() { int i, num; printf("enter a number\n"); scanf("%d", &num); for(i = 2 ; i < num; i++) if (num % i == 0) /* Then we know it's not a prime */ break; if (i < num) printf("%d is not a prime number!\n", num); else printf("%d is indeed a prime number!\n", num); } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

תרגיל נוסף Extend the former program so it accepts a number from the user, and prints out all of the prime numbers up to that number (Hint – gotta use nested loops here...) 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

פתרון #include void main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i = 2; i <= last; i++) { for(j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) printf("the number %d is prime\n", i); } // end for } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

תרגיל נוסף Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** **** *** ** * 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

הנה הפתרון #include void main() { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for(j = i; j <= size; j++) printf("*"); printf("\n"); } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

שינוי בתרגיל Change the former prime-listing program, so that is displays only the largest prime number which is smaller than or equal to the user’s input. 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

פתרון 1 #include void main() { int i, j, last; int found = 0; /* This indicates whether we found the largest prime */ printf("enter a number\n"); scanf("%d", &last); i = last; while (!found) { /* Loop until we find our guy */ for(j = 2 ; j < i; j++) if (i % j == 0) break; if (j == i) /* If this is true then i is prime */ found = 1; else i--; } printf("The largest prime not larger than %d is %d.\n", last, i); } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU

פתרון 2 – עם break #include void main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for(i=last ; i>1 ; i--) { for(j = 2 ; j < i; j++) if (i % j == 0) break; // break the inner for loop only !! if (j == i) // i is prime. We found our guy break; } printf("The largest prime not larger than %d is %d.\n", last, i); } 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי Department of Computer Science-BGU