Chapter 6: Loops.

Slides:



Advertisements
Similar presentations
CS0004: Introduction to Programming Repetition – Do Loops.
Advertisements

CHAPTER 5: LOOP STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
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.
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved The switch Multiple-Selection Statement switch.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Lecture Set 5 Control Structures Part D - Repetition with Loops.
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
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.
Chapter 4 C Program Control. Objectives In this chapter, you will learn: –To be able to use the for and do … while repetition statements. –To understand.
Chapter 8 Iteration Dept of Computer Engineering Khon Kaen University.
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,
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.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Decision Making and Branching (cont.)
Chapter 7: Repetition Structure (Loop) Department of Computer Science Foundation Year Program Umm Alqura University, Makkah Computer Programming Skills.
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Loop Control อ. ยืนยง กันทะเนตร คณะเทคโนโลยีสารสนเทศและการสื่อสาร มหาวิทยาลัยพะเยา Chapter 8.
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.
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.
Chapter 4 – C Program Control
CSE 220 – C Programming Loops.
Fundamentals of PL/SQL part 2 (Basics)
Chapter 4 Repetition Statements (loops)
C Program Controls + Flow Structure
Control Statements: Part 2
Warm-up Program Use the same method as your first fortune cookie project and write a program that reads in a string from the user and, at random, will.
Chapter 2.2 Control Structures (Iteration)
JavaScript: Control Statements.
The Linux Command Line Chapter 29
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Chapter 4 Repetition Structures
Arrays, For loop While loop Do while loop
Loop Control Structure.
Looping and Repetition
FLOW OF CONTROL.
MSIS 655 Advanced Business Applications Programming
Chapter 8 JavaScript: Control Statements, Part 2
Chapter 6: Repetition Structures
Chapter 5: Repetition Structures
Chapter 6 Decision Making and Looping
Chapter 4 - Program Control
Exam 1 Date: Feb. 2nd, 2015 during class time (50 minutes) Coverage
Loops in C.
Iteration: Beyond the Basic PERFORM
Iteration: Beyond the Basic PERFORM
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Chapter 2.2 Control Structures (Iteration)
CMPT 102 Introduction to Scientific Computer Programming
Chapter 6: Repetition Statements
CS2011 Introduction to Programming I Loop Statements (II)
Lab5 PROGRAMMING 1 Loop chapter4.
© 2016 Pearson Education, Ltd. All rights reserved.
2.6 The if/else Selection Structure
Introduction to Computer Science
Chapter 4 - Program Control
PROGRAM FLOWCHART Iteration Statements.
CSC215 Lecture Control Flow.
Programming Fundamental
Chapter 8 JavaScript: Control Statements, Part 2
LOOP Basics.
Looping and Repetition
Presentation transcript:

Chapter 6: Loops

Introduction A loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

Loops type in c

The While statement A while loop in C programming repeatedly executes a target statement as long as a given condition is true. Syntax: while(condition) { statement(s); } Statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any nonzero value. The loop iterates while the condition is true. When the condition becomes false, the program control passes to the line immediately following the loop.

While: Flow Diagram

Example 1

Infinite Loop Using while While loop won’t terminate if the controlling expression always has a nonzero value Eg: while(1) Will execute forever unless its body contains a statement that transfers control out of loop (break, goto, return)

Example 2: Program to find sum on n natural number Program to display multiplication table on n Program to check whether a number is palindrome Program to generate fibonachhi series Program to display * ** *** **** *****

The do… while statement do.... While is essentially just a while statement whose controlling expression is tested after each execution of the loop body Syntax do{ statements } while (expression);

Example 3: Program to count number of integer

for statement 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 for(init; condition; increment/decrement { statements; }

Infinite Loop using for Like while infinite loop can be created using for Syntax(or code): for(;;)

Example 4: Display KIST 50 time Display series of even number Display fibonachhi series Check whether a number is prime or not

The Comma operator Can be used to initialize two variable at a time Eg:

Exiting from loop The break statement Example: Program to check whether a number is prime or not

Example 5: Generate series of prime number

The continue statement Similar to break break transfer control just past the end of loop where as continue transfer control to a point just before the end of the loop body  Instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. break, leaves the loop where as with continue control remains inside the loop

Example 6

The goto Statement break passes control outside loop and continue escapes the remaining part of loop and continues loop But goto is capable of jumping to any statement in a function, provided that the statement has a label A label is just an identifier placed at the beginning of a statement Syntax identifier: statement ………………………… goto identifier;

Example 7