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.

Slides:



Advertisements
Similar presentations
Control Structure: Loop (Part 1) Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
Advertisements

Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Computer Science 1620 Loops.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
1 Repetition structures Overview while statement for statement do while statement.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
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.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Some loop programs 1: Read in 10 integers and output their sum
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 2004.
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
Chapter 4 Control Structure: Loop Knowledge: Understand the various concepts of loop control structure Skill: Be able to develop a program involving loop.
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.
COMP 110 Introduction to Programming Mr. Joshua Stough September 24, 2007.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Lecturer: Omid Jafarinezhad Sharif University of Technology Department of Computer Engineering 1 Fundamental of Programming (C) Lecture 5 Structured Program.
Do-while loop Syntax do statement while (loop repetition condition)
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Previously Repetition Structures While, Do-While, For.
1. Agenda for loop Short-handed notation How to use break and continue 2.
Control Structures II Repetition (Loops). Why Is Repetition Needed? How can you solve the following problem: What is the sum of all the numbers from 1.
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
Repetition Structures Repetition Structures allow you to write programs that will repeat program steps multiple times. –Also called Loops –Counter controlled.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Lecture 4: C/C++ Control Structures Computer Programming Control Structures Lecture No. 4.
1 JavaScript: Control Structures. 2 Control Structures Flowcharting JavaScript’s sequence structure.
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;
ITERATIVE STATEMENTS. Definition Iterative statements (loops) allow a set of instruction to be executed or performed several until condition are met.
Example # 1 Draw a flowchart for calculating the area of a room:
1 Standard Version of Starting Out with C++, 4th Brief Edition Chapter 5 Looping.
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
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 STATEMENTS - Part1  Also called LOOP STATEMENTS OR LOOP STRUCTURES 1 C++ Statements that repeat one or more actions while some condition is.
Principle Prog Revision. Question 1 (a) Identify errors in the following program segment and how the errors can be corrected. void main(){ constant int.
Iteration & Loop Statements 1 Iteration or Loop Statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
1 ICS103 Programming in C Lecture 7: Repetition Structures.
PGT C Programming1 Week 4 – Repetition Structures / Loops.
February 7, You have seen how If statement works from last week’s activities. You have learned that If statement is a conditional statement. Today,
Sesi 0607EKT120/4 Computer Programming Week 5 – Repetition / 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.
JavaScript: Control Structures I Outline 1 Introduction 2 Algorithms 3 Pseudocode 4 Control Structures 5 if Selection Structure 6 if/else Selection Structure.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
The Repetition control structure using while loop.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
UCT Department of Computer Science Computer Science 1015F Iteration
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
CS1010 Programming Methodology
Repetition-Sentinel,Flag Loop/Do_While
Chapter 2.2 Control Structures (Iteration)
Programming Fundamentals
Control Structures Lecture 7.
Looping.
How to develop a program?
Chapter 8 The Loops By: Mr. Baha Hanene.
Chapter 6 Decision Making and Looping
Chapter 2.2 Control Structures (Iteration)
Week 6 CPS125.
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Week 3 – Repetition (ctd.)
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

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 a counter Syntaxes for (initial value ; condition; update counter) statement; Or for (initial value ; condition; update counter) { statement; }

TK1913-C Programming3 TK1913-C Programming 3 Example Write a program which does the following: Reads 5 integers and displays the sum of all integers Input example: Output example: 16

TK1913-C Programming4 TK1913-C Programming 4 Recall the flowchart counter ← 1, sum ← 0 counter < 6 sum←sum+ x false true counter++ output sum input x

TK1913-C Programming5 TK1913-C Programming 5 Note the initial value of i and condition i ← 1, sum ← 0 i < 6 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?

TK1913-C Programming6 TK1913-C Programming 6 i ← 0, sum ← 0 i < 6 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?

TK1913-C Programming7 TK1913-C Programming 7 i ← 0, sum ← 0 i < 5 sum←sum+ x false true i++ output sum input x How many times does the loop get executed?

TK1913-C Programming8 TK1913-C Programming 8 The C statements: int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } printf(“%d”,sum);

TK1913-C Programming9 TK1913-C Programming 9 i ← 0, sum ← 0 i < 5 sum←sum+ x false true i++ output sum input x int x, sum, i; sum = 0; for (i = 0; i < 5; i++) { scanf(“%d”,&x); sum = sum + x; } printf(“%d”,sum);

TK1913-C Programming10 TK1913-C Programming 10 for statement for statement Example: for ( num = 1; num <= 3; num++ ) printf(“%d\t”, num); num ??? _ 1_1_

TK1913-C Programming11 TK1913-C Programming 11 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement

TK1913-C Programming12 TK1913-C Programming 12 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 _ for statement for statement

TK1913-C Programming13 TK1913-C Programming 13 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 1 1_1_ for statement for statement

TK1913-C Programming14 TK1913-C Programming 14 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement

TK1913-C Programming15 TK1913-C Programming 15 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 2 1_1_ for statement for statement

TK1913-C Programming16 TK1913-C Programming 16 for statement for statement Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); bil 2 12_12_

TK1913-C Programming17 TK1913-C Programming 17 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement

TK1913-C Programming18 TK1913-C Programming 18 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 12_12_ for statement for statement

TK1913-C Programming19 TK1913-C Programming 19 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 3 123_123_ for statement for statement

TK1913-C Programming20 TK1913-C Programming 20 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 4 123_123_ for statement for statement

TK1913-C Programming21 TK1913-C Programming 21 Example: for (num = 1; num <= 3; num++ ) printf(“%d\t”, num); num 4 123_123_ for statement for statement

TK1913-C Programming22 TK1913-C Programming 22 for while do-while

TK1913-C Programming23 TK1913-C Programming 23 Loop: while Loop: while  Condition is tested first  Loop is controlled by condition or a counter  Syntax while (condition) statement; Or while (condition) { statement; }

TK1913-C Programming24 TK1913-C Programming 24 Recall this example: Given an exam marks as input, display the appropriate message based on the rules below:  If marks is greater than 49, display “PASS”, otherwise display “FAIL”  However, for input outside the range, display “WRONG INPUT” and prompt the user to input again until a valid input is entered

TK1913-C Programming25 TK1913-C Programming 25 false true input m m 100 m>49 “PASS” “FAIL” true false “WRONG INPUT” input m Exercise: Convert this flowchart to a C program

TK1913-C Programming26 TK1913-C Programming 26 int marks; scanf(“%d”,&marks); while (marks 100) { printf(“WRONG INPUT”); scanf(“%d”,&marks); } if (marks>49) { printf(“PASS”); else printf(“FAIL”); } Double Selection

TK1913-C Programming27 TK1913-C Programming 27 Exercise Given a set of integers with the last one being 999 Display the summation of all the integers. Input example: Output example: Sum = 27 Draw the flowchart for this problem

TK1913-C Programming28 TK1913-C Programming 28 Sentinel-controlled loop true x!=999 sum←sum+x false input x sum=0 input x output sum #include void main() { int sum, x; sum = 0; scanf(“%d”, &x); while (x != 999) { sum = sum + x; scanf(“%d”, &x); } printf(“The sum : %d\n”, sum); }

TK1913-C Programming29 TK1913-C Programming 29 int sum, x; sum = 0; scanf(“%d”, &x); while (x != 999) { sum = sum + x; scanf(“%d”, &x); } printf(“\nThe sum : %d\n”, sum); ? ? x sum 0 1 _1 1 != != != != 999 The sum : 27

TK1913-C Programming30 TK1913-C Programming 30 Do-while Loop Statements in the loop are executed first (at least onc, and condition is tested last Loop is controlled by a condition or counter Syntax do { statement; } while (condition); statement;

TK1913-C Programming31 TK1913-C Programming 31 do-while statement Example : printf(“Input start and end value : “); scanf(“%d %d”, &start, &end); do { printf(“%c (%d)\n“, start, start); start++; } while (start <= end) ; _ ??? start ??? end Input start and end value : _Input start and end value : 65 67_ 6567 Input start and end value : A (65) _ <= 67 Input start and end value : A (65) B (66) _ 67 <= Input start and end value : A (65) B (66) C (67) _ <= 67

TK1913-C Programming32 TK1913-C Programming 32 continue statement Example: for ( i = 0; i <= 5; i++ ) { if ( i % 2 ) continue; else printf(“%d is an even number. ”, i); printf(“Print iff even ! \n”); } ii <= 5i % 2 _ 00 <= 9 true01 <= 5 true12 <= 5 true3 <= 5 true4 <= 5 true55 <= 5 true166 <= 5 false is an even number._0 is an even number. Print iff even ! _ 0 is an even number. Print iff even ! 2 is an even number._ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! _ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! 4 is an even number._ 0 is an even number. Print iff even ! 2 is an even number. Print iff even ! 4 is an even number. Print iff even ! _

TK1913-C Programming33 TK1913-C Programming 33 Now what can you conclude about continue?

TK1913-C Programming34 TK1913-C Programming 34 break statement break statement printf(“Input a value between 1 – 7: ”); scanf(“%d”,&value); for (i = 1; i <= 7; i++) { if ( i = = value ) break; printf(“\n%d”,i); } printf(“YeePee! I’m out of the loop!\n”); value ??? ii <= 7i == value Input a value between 1-7: _Input a value between 1-7: 4 11 <= 7 true1 == 4 false 4 Input a value between 1-7: <= 7 true2 == 4 false Input a value between 1-7: <= 7 true3 == 4 false Input a value between 1-7: <= 7 true4 == 4 true Input a value between 1-7: YeePee! I’m out of the loop!

TK1913-C Programming35 TK1913-C Programming 35 Now what can you conclude about break?

TK1913-C Programming36 TK1913-C Programming 36 End of 1 st half of the semester Yes !! That’s all? What’s next??? 1 week HOLIDAY on the way …