1. Agenda for loop Short-handed notation How to use break and continue 2.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Computer Science 1620 Loops.
C Lecture Notes 1 Program Control (Cont...). C Lecture Notes 2 4.8The do / while Repetition Structure The do / while repetition structure –Similar to.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
Repetition Structures: For Loop Constants CSC 1401: Introduction to Programming with Java Week 5 Wanda M. Kunkle.
Introduction to Computers and Programming Class 9 Introduction to C Professor Avi Rosenfeld.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Control Structures II. Why is Repetition Needed? There are many situations in which the same statements need to be executed several times. Example: Formulas.
Chapter 4: Control Structures II
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
1 4.8The do/while Repetition Structure The do/while repetition structure –Similar to the while structure –Condition for repetition tested after the body.
C Program Control Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Chapter 5 Control Structures: Loops 5.1 The while Loop The while loop is probably the most frequently used loop construct. The while loop is a conditional.
Lecture 8: Choosing the Correct Loop. do … while Repetition Statement Similar to the while statement Condition for repetition only tested after the body.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
 An instruction or group of instructions.  Computer executes program repeatedly for specified number of times. Or until some terminating conditions are.
do - while  while: Execute the body of the loop at least once
University of Palestine software engineering department Introduction to data structures Control Statements: Part 1 instructor: Tasneem Darwish.
CSCI 171 Presentation 4. Execution of a C Program Execution starts in main( ) Top down style –sequential flow Unrealistic to expect sequence in more complicated.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
AEEE 195 – Repetition Structures: Part B Spring semester 2011.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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;
CONTROL STATEMENTS LOOPS. WHY IS REPETITION NEEDED?  There are many situations in which the same statements need to be executed several times.  Example:
Think Possibility 1 Iterative Constructs ITERATION / LOOPS C provides three loop structures: the for-loop, the while-loop, and the do-while-loop. Each.
Repetition Repetition allows you to repeat an operation or a series of operations many times. This is called looping and is one of the basic structured.
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”
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Looping Increment/Decrement Switch. Flow of Control Iteration/Switch Statements.
LOOPS IN ‘C’ PROGRAMMING. V ERY O FTEN, Y OU W ILL W ANT TO D O S OMETHING M ORE T HAN O NCE HA.
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / Loops.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
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.
ECE Application Programming
Chapter 4 – C Program Control
REPETITION CONTROL STRUCTURE
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Chapter 4 C Program Control Part II
Lecture 7: Repeating a Known Number of Times
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
The nested repetition control structures & Continue Statements
Chapter 2.2 Control Structures (Iteration)
- Additional C Statements
Chapter 4 - Program Control
for, do-while and switch statments
Program Control Topics While loop For loop Switch statement
Chapter 2.2 Control Structures (Iteration)
REPETITION STATEMENTS
2.6 The if/else Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

1

Agenda for loop Short-handed notation How to use break and continue 2

for loop There are statements that are needed to be repeated. The exact amount of loops (repetitions) is known. Example I: Calculate the sum of 1 to

Syntax for (expression1; expression2; expression3) { 1 st statement; 2 nd statement; … n th statement; } where expression1: starting condition expression2: In-the-loop condition expression3: variable update condition 4 Example: int counter, sum = 0; for (counter = 1; counter <=1000; counter = counter+1) { sum = sum + counter; } printf("The sum is %d \n",sum);

Syntax Recall that while loop uses the following format expression1; while (expression2) { 1 st statement; … n th statement; 3 rd expression; } 5

Flowchart for (counter = 1; counter <=10; counter = counter+1) { statement; } 6 Entry counter = 1 counter <= 10 statement counter = counter + 1 Exit True False

Example Consider the following program that displays the number from 1 to 10. Compare the codes with the do loop and do-while loop that perform the same task. 7

Example #include int main() { int counter; printf("Print counter from 1 to 10\n"); for (counter = 1; counter <=10; counter = counter + 1) { printf("%d ", counter); } return 0; } 8 Print counter from 1 to

Short-handed notation OperatorsExpressionsMeaningsFinal values +=c += 7c = c =d -= 4d = d – 4 1 *=e *= 5e = e * 520 /=f /= 3f = f / 3 2 %=g %= 9g = g % Useful notations in statements (assignment operators) Let c = 3, d = 5, e = 4, f = 6, g = 12

More on short-handed notations OperatorsExamplesMeanings ++++aIncrease a by 1, and use the new value of a ++a++Use the value of the current a, and add 1 to a afterward ----bDecrease b by 1, and use the new value of b --b--Use the value of the current b, and subtract 1 from b 10 Let a = 10, b = 5 x = ++a; // The results are: x = 11, and a = 11 y = a++; // The results are: y = 10, and a = 11 u = --b; // The results are: u = 4, and b = 4 v = b--; // The results are: v = 5, and b = 4

(Precedence) Order of operations OperatorsOrder !right to left * / %left to right + -left to right >=left to right == !=left to right &&left to right ||left to right ? :right to left = += -= *= /= %=right to left,left to right 11

Example on using ++ 12

Example #include int main() { int c; /* demonstrate postincrement */ c = 5; printf(“%d\n”, c); printf(“%d\n”, c++); /* print c then increase */ printf(“%d\n\n”, c); /* demonstrate preincrement */ c = 5; printf(“%d\n”, c); printf(“%d\n”, ++c); /* increase then print c */ printf(“%d\n”, c); return (0); }

Using break and continue break or continue is used to re-route/jump the path under a special circumstance. For more organized programming, avoid using break and continue if possible. 14

break In the loop (while, do-while, for) or switch statement, using break results in the leaving the loop immediately. The program continues at the statement after the loop Example The following program displays the counter using for loop, when the counter is 5, leave the loop immediately. 15

Example #include void main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) { /* if x is 5, terminate loop */ break; } printf("%d", x); } printf("\n Broke out of loop at x == %d\n", x); } Broke out of loop at x == 5

continue In the loop (while, do while, for), at the n th iteration, using continue results in the program skips the rest of statements in the current iteration. However, the program continues on the next (n+1) th iteration. Example The following program displays the counter. At the 5 th iter, printf is skipped, and continue on the 6 th iter. 17

Example #include int main() { int x; for (x = 1; x <= 10; x++) { if (x == 5) { /* if x is 5, skip remaining code in loop body*/ continue; } printf("%d", x); } printf("\n Used continue to skip printing the value 5\n"); return; } Used continue to skip printing the value 5