Program Looping EE2372 Software Design I Dr. Gerardo Rosiles.

Slides:



Advertisements
Similar presentations
Selection Feature of C: Decision making statements: It allow us to take decisions as to which code is to be executed next. Since these statements control.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Repetition Statements Perform the same task repeatedly Allow the computer to do the tedious, boring things.
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
CS 101 Introductory Programming - Lecture 7: Loops In C & Good Coding Practices Presenter: Ankur Chattopadhyay.
Functions Function: The strength of C language is to define and use function. The strength of C language is that C function are easy to define and use.
How SAS implements structured programming constructs
Do-while Loops Programming. COMP102 Prog Fundamentals I: do-while Loops /Slide 2 The do-while Statement l Syntax do action while (condition) l How it.
Loops –Do while Do While Reading for this Lecture, L&L, 5.7.
Decision Making EE2372 Software Design I Dr. Gerardo Rosiles.
Programming In C++ Spring Semester 2013 Lecture 4 Programming In C++, Lecture 3 By Umer Rana.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Computer programming Lecture 3. Lecture 3: Outline Program Looping [Kochan – chap.5] –The for Statement –Relational Operators –Nested for Loops –Increment.
A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
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 Data Structures Appendix 1 How to transfer a simple loop- expression to a recursive function (factorial calculation)
Repetition Statements repeat block of code until a condition is satisfied also called loops Java supports 3 kinds of loops: while statement – repeats a.
1 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
Introduction to Computer Programming in c
Principles of programming languages 5: An operational semantics of a small subset of C Department of Information Science and Engineering Isao Sasano.
Repetition Statements.  Often it is necessary to repeat statements many times  Java has two ways of doing this  while statements  for statements.
Computer Science Department LOOPS. Computer Science Department Loops Loops Cause a section of your program to be repeated a certain number of times. The.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Iterations Very Useful: Ability to repeat a block of code Example:
Computer programming Outline Functions [chap 8 – Kochan] –Defining a Function –Arguments and Local Variables Automatic Local.
 In computer programming, a loop is a sequence of instruction s that is continually repeated until a certain condition is reached.  PHP Loops :  In.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
For Loop Lecture No 8. Definition In computer science a for loop is a programming language statement which allows code to be repeatedly executed. A for.
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.
Computer C programming Chapter 3. CHAPTER 3 Program Looping –The for Statement –Nested for Loops –for Loop Variants –The while Statement –The do Statement.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Follow up from lab See Magic8Ball.java Issues that you ran into.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
PHP Condtions and Loops Prepared by Dr. Maher Abuhamdeh.
Lesson #5 Repetition and Loops.
Chapter 4 Repetition Statements (loops)
Loops in Java.
Decisions Chapter 4.
Lesson #5 Repetition and Loops.
Branching Constructs Review
Chapter 2.2 Control Structures (Iteration)
For Loops October 12, 2017.
Lesson #5 Repetition and Loops.
Conditional Construct
Lecture Notes – Week 3 Lecture-2
1) C program development 2) Selection structure
Pages Section: Control2: Repetition
Chapter 8: More on the Repetition Structure
Week 6 CPS125.
Computer programming Lecture 3.
while while (condition) { statements }
Seating “chart” Front - Screen rows Back DOOR.
CS149D Elements of Computer Science
Lesson #5 Repetition and Loops.
PROGRAM FLOWCHART Iteration Statements.
Statements in C Programming
The for-statement.
Pages Section: Control2: Repetition
Repetition Structures
Types of loops definite loop: A loop that executes a known number of times. Examples: Repeat these statements 10 times. Repeat these statements k times.
Pages Section: Control2: Repetition
Week 3 – Repetition (ctd.)
Looping and Repetition
Presentation transcript:

Program Looping EE2372 Software Design I Dr. Gerardo Rosiles

Program Looping Looping programming elements are one of the most powerful feature in programming languages. Consider writing program to sum 10 numbers… easy Consider writing a program to sum 1,000,000 numbers => one million sums … Looping constructs make this easy In C-language we have three types of loops: – for loops – while loops – do loops

for loops Similar to the MATLAB for loops Syntax for (init_expression, loop_condition, loop_expression) program statement Syntax with block of statements for (init_expression, loop_condition, loop_expression) { program statements }

for loops init expression – set any initial values before the loop begins. Typically initializes a variable to control the loop condition. loop condition(s) – a condition that continues the loop as long as it evaluated to TRUE. loop expression(s) – evaluated each time after the body of the loop is executed. Typically involved a variable controlling the loop.

for loops - example #include int main(void) { int n, number, triangularNumber; printf( What triangular number do you want? ); scanf ( %i, &number); printf( TABLE OF TRIANGULAR NUMBERS \n\n); printf( n Sum from 1 to n \n); printf( \n); triangularNumber = 0; for( n = 1; n<=number; ++n) { triangularNumber += n; printf( %i %i \n, n, triangularNumber); } return(0); }

Some variants Nested for loops for( n = 1; n<=number; ++n) { sum = 0; for ( m = number; m >= n; m--) { sum = sum + m; printf( sum %d to %d = %d \n, n, number, sum); } Multiple expressions, for( i=0, j=100; i < 10;++i, j=j-10)

Some variants Omitting fields for( ; ; ){.... } // infinite loop for( ; j!= 0 ; j++ ){... } Declaring variables for( int i; i <= 10 ; i++ ){... } i is a local variable and cant be accessed outside of the loop. Disappears after the loop finished.

while loops Syntax while( expression ) { program statements } Equivalency with for loops init_expression; while (loop_condition) { program statements loop_expression; } while statements are useful when there is not a predetermined number of times the loop needs to be executed.

do-while loops Syntax: do { program statements } while( expression ); do-while loops are a transpositions of while loops. However, do-while loops ensure the program statement is executed at least once.

Loop interruption statements In some situations it may be desirable to stop the loop or to finish early the current iteration of the loop. break – can finish the execution of a loop before the loop condition is not satisfied. continue – finishes early the current iteration of the loop if some specific condition arises. Otherwise the loop continues executing.