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.

Slides:



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

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
1 Parts of a Loop (reminder) Every loop will always contain three main elements: –Priming: initialize your variables. –Testing: test against some known.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 6 - Repetition. Introduction u Many applications require certain operations to be carried out more than once. Such situations require repetition.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control Loops in Java.
Computer Programming 1 Repetition. Computer Programming 2 Objectives Repetition structures Study while and do loops Examine for loops A practical example.
 2007 Pearson Education, Inc. All rights reserved C Program Control.
Flow of Control Recitation – 09/(18,19)/2008 CS 180 Department of Computer Science, Purdue University.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Loops Repetition Statements. Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional.
© 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.
UNIT II Decision Making And Branching Decision Making And Looping
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
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.
Principles of Programming - NI July Chapter 5: Structured Programming In this chapter you will learn about: Sequential structure Selection structure.
Chapter 7 LOOPING OPERATIONS: ITERATION. Chapter 7 The Flow of the while Loop.
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.
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,
L OO P S While writing a program, there may be a situation when you need to perform some action over and over again. In such situation you would need.
DEPARTMENT OF COMPUTER SCIENCE & TECHNOLOGY FACULTY OF SCIENCE & TECHNOLOGY UNIVERSITY OF UWA WELLASSA 1 ‏ Control Structures.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (for) Outline 4.1Introduction 4.2The.
Looping Construct or Statements. Introduction of looping constructs In looping,a sequence of statements are executed until some condition for termination.
Using Java MINISTRY OF EDUCATION & HIGHER EDUCATION COLLEGE OF SCIENCE AND TECHNOLOGY KHANYOUNIS- PALESTINE Lecture 9 & 10 Repetition Statements.
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.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Repetition Control Structure. Introduction Many applications require certain operations to be carried out more than once. Such situations require repetition.
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.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
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.
Repetition Statements (Loops). 2 Introduction to Loops We all know that much of the work a computer does is repeated many times. When a program repeats.
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”
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Loops Tonga Institute of Higher Education. Introduction Programs need to be able to execute tasks repeatedly. Use loops to repeat actions  For Loop 
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
Introduction to Programming G50PRO University of Nottingham Unit 6 : Control Flow Statements 2 Paul Tennent
CONTENTS Loop Statements Parts of a loop Types of Loops Nested Loops
Chapter 7 Control Structures. Java has very flexible three looping mechanisms. You can use one of the following three loops:  while Loop  do...while.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Week 3.  TO PRINT NUMBERS FROM 1 TO 20  TO PRINT EVEN NUMBERS FROM 1 TO 20 2.
Loops causes program to execute the certain block of code repeatedly until some conditions are satisfied. Suppose you want to execute some code/s 10 times.
LESSON 5 Loop Control Structure. Loop Control Structure  Operation made over and over again.  Iterate statement.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
Chapter 6: Loops.
Chapter 4 Repetition Statements (loops)
Loops in Java.
Control Structures.
Programming Fundamentals
ITM 352 Flow-Control: Loops
Arrays, For loop While loop Do while loop
Loop Control Structure.
MSIS 655 Advanced Business Applications Programming
LRobot Game.
Chapter 4 - Program Control
Loops in C.
Repetition Control Structure
Program Control Topics While loop For loop Switch statement
A LESSON IN LOOPING What is a loop?
PROGRAM FLOWCHART Iteration Statements.
Presentation transcript:

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

Y OU C OULD C ODE T HE ‘H A H A ’ L IKE T HIS printf("Ha Ha ") ; But is actually Lame! No good programmer does this!

LOOPS : WHY DO WE NEED LOOPS ??? There may be a situation, when you need to execute a block of code several number of times. In general statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. A loop statement allows us to execute a statement or group of statements multiple times

LOOPS : ( ITERATIVE STATEMENTS ) TYPES OF LOOPS : WHILE LOOP (Counter-Controlled Loop) FOR LOOP (Counter-Controlled Loop) DO-WHILE LOOP (Exit-Controlled Loop) LETS HAVE A CLOSER LOOK

LOOPS => WHILE LOOP (P RE -T EST ) A while loop statement repeatedly executes a target statement as long as a given condition is true. Syntax: The syntax of a while loop in C is: while(condition) { statement(s); }

LOOPS => WHILE LOOP Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop

LOOPS => WHILE LOOP FLOW DAIGRAM

LOOPS => WHILE LOOP EXAMPLE : #include void main () { // Local variable declaration: int a = 10; // while loop execution while( a < 20 ) { printf(“value of a: %d \n”, a); a++; } getch( ); }

LOOPS => WHILE LOOP When the above code is compiled and executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

LOOPS : FOR LOOP: A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times. Syntax: The syntax of a for loop in C is: for ( initialization; condition; updation ) { statement(s); }

LOOPS => FOR LOOP (P RE -T EST ) The initialization step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears. Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.

LOOPS => FOR LOOP After the body of the for loop executes, the flow of control jumps back up to the increment / decrement statement. This statement allows you to update any loop control variables. This statement can be left blank, as long as a semicolon appears after the condition. The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.

LOOPS=> FOR LOOP Flow Diagram:

LOOPS => FOR LOOP Example: #include void main ( ) { int a; // for loop execution for( a = 10; a < 20; a++ ) { printf("value of a: %d /n”, a); } getch(); }

LOOPS => FOR LOOP When the above code is compiled and executed, it produces the following result: value of a: 10 value of a: 11 value of a: 12 value of a: 13 value of a: 14 value of a: 15 value of a: 16 value of a: 17 value of a: 18 value of a: 19

LOOPS : DO-WHILE LOOP: (Post – Test) Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.

DO-WHILE LOOP (P OST -T EST ) Syntax: The syntax of a do...while loop in C is: do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested.

LOOPS => DO-WHILE LOOP Flow Diagram:

LOOPS => DO-WHILE LOOP Example: #include void main () { // Local variable declaration: int a = 10; // do loop execution do { printf( "value of a: %d\n “,a); a = a + 1; } while( a < 10 ); getch(); }

LOOPS => DO-WHILE LOOP When the above code is compiled and executed, it produces the following result: value of a: 10

LOOPS => WHILE LOOP Example: #include void main () { // Local variable declaration: int a = 10; // while loop execution while( a < 10 ) { printf( "value of a: %d\n “,a); a = a + 1; } printf(“\n Exiting from main”); getch( ); }

OUTPUT : Exiting from main

P RETEST VS P OST -T EST L OOPS WHILE DO - WHILE

P RACTICE PROGRAMS : 1. Write a C program to print a string “SREYAS INSTITUTE OF ENGINEERING & TECHNOLOGY” 10 times using ( I ) for loop ( II) while loop ( III ) do-while loop 2. write a C Program to print numbers from 1 to 10 using ( I ) for loop ( II) while loop ( III ) do-while loop