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”

Slides:



Advertisements
Similar presentations
Programming In C++ Spring Semester 2013 Lecture 3 Programming In C++, Lecture 3 By Umer Rana.
Advertisements

Loops (Part 1) Computer Science Erwin High School Fall 2014.
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.
Some loop programs 1: Read in 10 integers and output their sum
Loops – While, Do, For Repetition Statements Introduction to Arrays
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
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.
ECE122 L9: While loops March 1, 2007 ECE 122 Engineering Problem Solving with Java Lecture 9 While Loops.
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) 
Lecture 10: Reviews. Control Structures All C programs written in term of 3 control structures Sequence structures Programs executed sequentially by default.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
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.
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.
Repetitive Structures BBS514 Structured Programming (Yapısal Programlama)1.
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.
Control Statements (Decision Making)
Copyright © Nancy Acemian 2004 For Loops-Break-Continue COMP For loop is a counter controlled loop. For loop is a pretest loop. Used when number.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
Program Flow Control - Looping Addis Ababa Institute of Technology Yared Semu April 2012.
Chapter 5: Structured Programming
Chapter 5: Structured Programming
© 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
ECE 103 Engineering Programming Chapter 18 Iteration Herbert G. Mayer, PSU CS Status 7/19/2015 Initial content copied verbatim from ECE 103 material developed.
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.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 5: 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.
COMP Loop Statements Yi Hong May 21, 2015.
Course Title Object Oriented Programming with C++ Course instructor ADEEL ANJUM Chapter No: 04 loops 1 BY ADEEL ANJUM ( MSc - cs, CCNA, WEB DEVELOPER )
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Lecture 7 – Repetition (Loop) FTMK, UTeM – Sem /2014.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
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.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
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.
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 5: Structured Programming
REPETITION CONTROL STRUCTURE
Lecture 4 - Loops UniMAP EKT120 Sem 1 08/09.
Week 4 – Repetition Structures / Loops
Chapter 2.2 Control Structures (Iteration)
Arrays, For loop While loop Do while loop
Outline Altering flow of control Boolean expressions
Chapter 4 - Program Control
Iteration: Beyond the Basic PERFORM
Chapter 2.2 Control Structures (Iteration)
Week 6 CPS125.
Computer programming Lecture 3.
More Loops Topics Counter-Controlled (Definite) Repetition
ECE 103 Engineering Programming Chapter 18 Iteration
More Loops Topics Counter-Controlled (Definite) Repetition
CprE 185: Intro to Problem Solving (using C)
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Presentation transcript:

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”

BY ILTAF MEHDI (MCS, MCSE, CCNA)3 Course Contents Chapter NoChapter NamePage No 1. The Turbo C Programming Environment C Building Blocks Decisions Loops Functions Arrays and Strings 179

LOOPS “Loops” are meant for repeated processing. “To execute a set of instructions repeatedly until a particular condition is being satisfied”. In every programming language, also in the C programming language, there are circumstances where you want to do the same thing many times. For instance you want to print the same words ten times. You could type ten printf functions, but it is easier to use a loop. The only thing you have to do is to setup a loop that execute the same printf function ten times. Three types of looping statements are there: 1) For Loop 2) While Loop 3) Do while Loop BY ILTAF MEHDI (MCS, MCSE, CCNA)4

The for loop “The for loop is frequently used, usually where the loop will be executed to a fixed number of times”. OR “The for loop works well where the number of iterations of the loop is known before the loop is entered”. 5BY ILTAF MEHDI (MCS, MCSE, CCNA)

The for loop The head of the loop consists of three parts separated by semicolons. The C for statement has the following form: for (expression 1 ; expression 2 ; expression 3 ) statement; or { block of statements } 1. The first is run before the loop is entered. This is usually the initialization of the loop variable. 2. The second is a test, the loop is exited when this returns false. 3. The third is a statement to be run every time the loop body is completed. This is usually an increment of the loop counter. 6BY ILTAF MEHDI (MCS, MCSE, CCNA)

The for loop All the following are legal for-statements in C-language. 1. for (int x=0; ((x>3) && (x<9)); x++) 2. for (int x=0,y=4; ((x>3) && (y<9)); x++, y+=2) 3. for (int x=0,y=4,z=40; z; z/=10) The first example shows that multiple conditions can be written. The second example shows that multiple expressions can be separated a comma (,). In the third example the loop will continue to iterate until z becomes 0; 7BY ILTAF MEHDI (MCS, MCSE, CCNA)

A simple C-program using for-loop #include void main() { int x; for (x=0; x<=3; x++) { printf("x=%d", x); } getch(); } 8BY ILTAF MEHDI (MCS, MCSE, CCNA)

The while loop The while loop keeps repeating an action until an associated test returns false. This is useful where the programmer does not know in advance how many times the loop will be traversed. The while loop continues to loop until the conditional expression becomes false. The condition is tested upon entering the loop. while (expression) statement; 9BY ILTAF MEHDI (MCS, MCSE, CCNA)

A simple C-program using while-loop #include void main() { int x=0; while (x<=3) { printf("x=%d", x); x++; } getch(); } 10BY ILTAF MEHDI (MCS, MCSE, CCNA)

Write a C-program for a simple Calculator using while loop #include void main() { clrscr(); float num1=1.0,num2=1.0; char op; while (!(num1==0.0 && num2==0.0)) { printf("\n Type Number, Enter Operator(*, -, +, /), Number\n"); scanf ("%f %c %f", &num1, &op, &num2); if(op=='+') printf("= %f", num1 + num2); else if(op=='-') printf("= %f", num1 - num2); else if(op=='*') printf("= %f", num1 * num2); else if(op=='/') printf("= %f", num1 / num2); } printf("\n\n now the control is out of the while loop"); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)11

Write C-program that will sum the integers #include void main(void) { clrscr(); int sum=0, i=1, count=0; printf("\n Enter the number of integers you want to sum:"); scanf ("%d", &count); while(i<=count) { sum += i++; } printf("Total of the first %d numbers is %d\n", count, sum); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)12

The do while loop The do while loops is similar, but the test occurs after the loop body is executed. This ensures that the loop body is run at least once. The do...while loop checks the conditional expression only after the repetition part is executed. The do...while loop is used when you want to execute the loop body at least once. do statement; while (expression); 13BY ILTAF MEHDI (MCS, MCSE, CCNA)

A simple C-program using while-loop #include void main() { int x=0; do { printf("x=%d", x++); } while (x>0); getch(); } 14BY ILTAF MEHDI (MCS, MCSE, CCNA)

Write a c-program using do-while loop #include #include void main() { int i=0, n = 5; do { printf("the numbers are: %d \n", i); i = i +1; } while( i<n) ; getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)15

Break Statement To exit a loop you can use the break statement at any time. This can be very useful if you want to stop running a loop because a condition has been met other than the loop end condition. Take a look at the following c-program: #include void main() { clrscr(); int i = 0; while ( i < 20 ) { i++; if ( i == 10) { break ; } printf("\n the value of i is: %d", i); } printf("\n\n whlie loop body will not be executed any more."); getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)16 In this example, the while loop will run, as long i is smaller then twenty. In the while loop there is an if statement that states that if i equals ten the while loop must stop (break).

Continue Statement With “continue;” it is possible to skip the rest of the commands in the current loop and start from the top again. (the loop variable must still be incremented). Take a look at the example below: #include void main() { int i = 0; while ( i < 20 ) { i++; continue; printf("Nothing to see\n"); } getch(); } BY ILTAF MEHDI (MCS, MCSE, CCNA)17 In this example, the printf function is never called because of the “continue;”.