Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6: Loops.

Similar presentations


Presentation on theme: "Chapter 6: Loops."— Presentation transcript:

1 Chapter 6: Loops

2

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

4 Loops type in c

5 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.

6 While: Flow Diagram

7 Example 1

8 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)

9 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 * ** *** **** *****

10 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);

11 Example 3: Program to count number of integer

12 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; }

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

14

15

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

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

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

19 Example 5: Generate series of prime number

20 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

21 Example 6

22 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;

23 Example 7

24


Download ppt "Chapter 6: Loops."

Similar presentations


Ads by Google