programming fundamentals lecture 8

9
08 Repetition Control Structure C - Loops You may encounter situations, when a block of code needs to be executed 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. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Or Loops are used to repeat a block of code. Given below is the general form of a loop statement in most of the programming languages − Types of loops 1. for 2. while 3. do while 1. 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 f or ( init; condition; increment ) { statement(s); }

Upload: rehan-ijaz

Post on 21-Jan-2018

55 views

Category:

Engineering


3 download

TRANSCRIPT

Page 1: Programming Fundamentals lecture 8

08

Repetition Control Structure

C - Loops You may encounter situations, when a block of code needs to be executed 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. Programming languages provide various control structures that allow for more complicated execution paths. A loop statement allows us to execute a statement or group of statements multiple times. Or Loops are used to repeat a block of code.

Given below is the general form of a loop statement in most of the programming languages −

Types of loops

1. for

2. while

3. do while

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

for ( init; condition; increment ) { statement(s);

}

Page 2: Programming Fundamentals lecture 8

Here is the flow of control in a 'for' loop − The init 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 the flow of control jumps to the next statement just after the 'for' loop.

After the body of the 'for' loop executes, the flow of control jumps back up to

the increment 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.

Flow Chart

Page 3: Programming Fundamentals lecture 8

Example

#include <stdio.h> int main () { int a; /* for loop execution */ for( a = 10; a < 20; a = a + 1 ) { printf("value of a: %d\n", a); } return 0; }

Output

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

while loop in C A while loop in C programming repeatedly executes a target statement as long as a given condition is true. Syntax

while(condition) { statement(s); }

Here, 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.

Page 4: Programming Fundamentals lecture 8

Flow Diagram

Here, the key point to note is that a while loop might not execute at all. When the condition is

tested and the result is false, the loop body will be skipped and the first statement after the while

loop will be executed.

Example #include <stdio.h>

int main () { /* local variable definition */ int a = 10; /* while loop execution */ while( a < 20 ) { printf("value of a: %d\n", a); a++; } return 0; }

Output

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

Page 5: Programming Fundamentals lecture 8

value of a: 19

More about loops

Based on the nature of the control variables and the kind of value assigned to, the loops may be

classified into following two general categories;

a) Counter controlled loop

b) Sentinel Value controlled loop

a) Counter controlled loops:

The type of loops, where the number of the execution is known in advance are termed by the

counter controlled loop. That means, in this case, the value of the variable which controls the

execution of the loop is previously known. The control variable is known as counter. A counter

controlled loop is also called definite repetition loop.

Example : A while loop is an example of counter controlled loop.

sum = 0;

n = 1;

while (n <= 10)

{ sum = sum + n*n;

n = n+ 1;

}

Here, the loop will be executed exactly 10 times for n = 1,2,3,......,10.

b) Sentinel Value controlled loop :

The type of loop where the number of execution of the loop is unknown, is termed by sentinel controlled loop. In this case, the value of the control variable differs within a limitation and the

execution can be terminated at any moment as the value of the variable is not controlled by the

loop. The control variable in this case is termed by sentinel variable.

Example : The following do....while loop is an example of sentinel controlled loop.

do

{

printf(“Input a number.\n”);

scanf("%d", &num);

}

while(num>0);

Page 6: Programming Fundamentals lecture 8

In the above example, the loop will be executed till the entered value of the variable num is not 0

or less then 0. This is a sentinel controlled loop and here the variable num is a sentinel variable.

goto statement in C A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. Syntax

goto label; .. . label: statement;

Example #include <stdio.h> int main () { /* local variable definition */ int a = 10; /* do loop execution */ LOOP:do { if( a == 15) { /* skip the iteration */

a = a + 1; goto LOOP; } printf("value of a: %d\n", a); a++; } while( a < 20 ); return 0; }

Output

value of a: 10 value of a: 11 value of a: 12

Page 7: Programming Fundamentals lecture 8

value of a: 13 value of a: 14 value of a: 16 value of a: 17

value of a: 18 value of a: 19

Page 8: Programming Fundamentals lecture 8

Constant In the C programming languages, const is a type qualifier:[a] a keyword applied to a data type that

indicates that the data is constant (does not vary). You can use const prefix to declare constants

with a specific type as follows −

const type variable = value;

Example

#include <stdio.h>

int main() {

const int LENGTH = 10;

const int WIDTH = 5;

const char NEWLINE = '\n';

int area;

area = LENGTH * WIDTH;

printf("value of area : %d", area);

printf("%c", NEWLINE);

return 0;

}

Output

value of area : 50

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

const int std_reg_no; std_reg_no = 5; printf(“Value of you have stored is= %d”, std_reg_no); std_reg_no = 7; .

}

Page 9: Programming Fundamentals lecture 8

Error: Cannot modify a const object