Ch3 repetition

16
Chapter 3 CONTROL STRUCTURES: REPETITION

description

 

Transcript of Ch3 repetition

Page 1: Ch3 repetition

Chapter 3

CONTROL STRUCTURES:

REPETITION

Page 2: Ch3 repetition

REPETITION STRUCTURES

Programmer specifies an action to be repeated while some condition remains true

Essential of Repetition :

• Loop – Group of instructions computer executes repeatedly while some condition

remains true

• Counter-controlled repetition – Definite repetition: know how many times loop will execute – Control variable used to count repetitions

• Sentinel-controlled repetition – Indefinite repetition – Used when number of repetitions not known – Sentinel value indicates "end of data"

Page 3: Ch3 repetition

3.3 REPETITION STRUCTURES

Any steps repeated?

No loop required

Known in advance how many times to repeat?

Use one of the conditional loops: Sentinel-controlled Endfile-controlled Input validation General conditional

Use a counting

loop

Yes

No

Yes

No

Page 4: Ch3 repetition
Page 5: Ch3 repetition

The while Repetition Structure

The while statement in C specifies that a section of code should be executed while a certain condition holds true.

The syntax is:while (condition)

statement;

The condition is an

expression that has a true or false value. The statement is the statement to be repeatedly

executed while the condition is true.

condition statementTrue

False

Page 6: Ch3 repetition

Example 3.12 This program will print number 1 to 3.

#include <stdio.h>void main(){

int count = 1; while (count < 4)

{ printf (“%d ”, count)";

count++;}

}

Page 7: Ch3 repetition

The do...while Repetition Structure

There is another repetition control structure which is very similar to the while statement.

The only difference is that the expression which determines whether to carry on looping is evaluated at the end of each loop. The syntax is:

do

{

statement;

} while(condition);

Page 8: Ch3 repetition

The do...while Repetition Structure

This structure is used much more rarely than the while statement,

but is occasionally useful if we want to ensure that the loop statement is executed at least once.

condition

statement

False

True

Page 9: Ch3 repetition

Example 3.13: This program will print “This is cycle ?” 6 times.

#include <stdio.h>

void main()

{

int ctr = 1;

do

{

printf(“This is cycle %d\n”, ctr);

ctr++;

} while (ctr<= 6);

}

Output:

This is cycle 1

This is cycle 2

This is cycle 3

This is cycle 4

This is cycle 5

This is cycle 6

Page 10: Ch3 repetition

The for Repetition Structure Syntax :

for (initialization ; loop_control_expression ; update_expression)statement ;

for repetition structure handles all the details of counter-controlled repetition automatically.

In executing a for statement, the computer does the following:

(a) initialization is executed.(b) then, the loop_control_expression is evaluated.

If it computes to zero, the loop is exited.(c) if the loop_control_expression yields a nonzero value, the loop body is

executed and then the update_expression is evaluated.(d) The loop_control_expression is again tested.

Thus, the loopbody is repeated until the loop_control_expression computes to a zero value.

Page 11: Ch3 repetition

Example 3.14: This program will print “Hello” 5 times.

#include <stdio.h>void main(){

for(i=1; i<=5; i=i+1)printf("Hello\n");

}

Page 12: Ch3 repetition

Example 3.15: This program will print “Hello” and “Goodbye” twice.

#include <stdio.h>void main(){

for(int i=1; i<=2; i=i+1){

printf("Hello\n");printf("Goodbye\n");

}}

Page 13: Ch3 repetition

The break statement A break statement inside the body of a loop breaks completely out of

the loop. No more instructions in the body of the loop are executed, and the next

statement after the loop will be executed.

#include <stdio.h>void main(){

for (int x = 1 ; x <= 10 ; x++){

if( x == 5) break; /*break loop only if x== 5 */printf(“%d\t”,x);

}printf(“\nBroke out of loop at x==%d\n”,x);

}

// Output

1 2 3 4Broke out of loop at x == 5

Page 14: Ch3 repetition

The continue statement The continue statement just skips any instructions after it on that iteration of the

loop. The current iteration of the loop is terminated, and the loop statement is

executed again as if the last instruction of the loop body has been reached.

#include <stdio.h>void main(){

for (int x=1 ; x<=10 ; x++) { if ( x == 5) continue ; /* skip remaining code in loop only if x==5 */ printf ( “%d\t”, x) ;}printf(“\nBroke out of loop at x==%d\n”,x) ;

}

//output :1 2 3 4 6 7 8 9 10

Page 15: Ch3 repetition

The exit function The exit function forces a program to terminate as if it executed

normally. The function often is used to terminate a program when an error is

detected in the input or if file to be processed by the program cannot be opened.

#include <stdio.h>#include<stdlib.h>main ( ){

exit (1); /* program will end here*/printf (“ C programming is fun\n”);printf (“ C is a powerful language”);return 0;

}

Page 16: Ch3 repetition