SlideShare a Scribd company logo
1 of 51
COM1407
Computer Programming
Lecture 07
Program Control Structures – Repetition
and Loops
K.A.S.H. Kulathilake
B.Sc. (Hons) IT, MCS , M.Phil., SEDA(UK)
Rajarata University of Sri Lanka
Department of Physical Sciences
Objectives
• At the end of this lecture students should be able
to;
▫ Describe the looping structures in C programming
language.
▫ Practice the control flow of different looping
structures in C programming language.
▫ Practice the variants in control flow of different
looping structures in C programming language.
▫ Apply taught concepts for writing programs.
2
Introduction
• One of the fundamental properties of a computer is its
ability to repetitively execute a set of statements.
• These looping capabilities enable you to develop concise
programs containing repetitive processes that could
otherwise require thousands or even millions of program
statements to perform.
• If a loop continue forever, it is called infinite loop.
• A Looping consist of 2 segments.
▫ Body of the Loop
▫ Control statement
• A control structure may be classified either,
▫ As the entry controlled loop or
▫ Exit-controlled loop
3
Introduction (Cont…)
• Entry controlled loop
▫ In entry controlled loop, the
control conditions are tested
before the start of loop
execution.
▫ If the conditions are not
satisfied, then the body of the
loop will not be executed.
4
Introduction (Cont…)
• Exit-controlled loop
▫ The test is performed at the end
of the body of the loop and
therefore the body is executed
unconditionally for the first
time.
5
Introduction (Cont…)
• There are five parts of a looping construct;
▫ Loop control variable (Declaration)
▫ Initial value of the loop control variable
(Initialization)
▫ Loop control condition (Test condition)
▫ Repetitive statements (Body of the Loop)
▫ Increment the loop control variable (Increment)
6
Introduction (Cont…)
• The C programming language contains three
different program statements for program
looping.
▫ For statement,
▫ While statement,
▫ Do statement.
7
The while Statement
• The while is an entry-controlled loop statement.
• The basic format of the while statement is,
8
initialization;
while (test condition)
{
body of the loop
}
The while Statement (Cont…)
#include <stdio.h>
int main ()
{
int x =1;
while (x <= 10)
{
printf ("%i ,",x);
x =x+1;
}
return 0;
}
9
Increment
Loop body
Loop variable declaration and
initialization
test condition
The while Statement (Cont…)
• The test_condition specified inside the parentheses is
evaluated.
• If the result of the expression evaluation is TRUE, the body of
the loop that immediately follows is executed.
• After execution of this statement (or statements if enclosed in
braces), the test_condition is once again evaluated.
• If the result of the evaluation is TRUE, the body of the loop is
once again executed.
• This process continues until the test_condition finally
evaluates as FALSE, at which point the loop is terminated.
• Execution of the program then continues with the statement
that follows the body of the loop.
10
The while Statement (Cont…)
#include <stdio.h>
int main (void)
{
int count = 1;
while ( count <= 5 )
{
printf ("%in", count);
++count;
}
return 0;
}
11
The while Statement (Cont…)
• The next program computes the greatest
common divisor of two integer values.
• The greatest common divisor (gcd) of two
integers is the largest integer value that evenly
divides the two integers.
12
The while Statement (Cont…)
#include <stdio.h>
int main (void)
{
int u, v, temp;
printf ("Please type in two nonnegative integers.n");
scanf ("%i%i", &u, &v);
while ( v != 0 )
{
temp = u % v;
u = v;
v = temp;
}
printf ("Their greatest common divisor is %in", u);
return 0;
}
13
The while Statement (Cont…)
#include <stdio.h>
int main (void)
{
int number, right_digit;
printf ("Enter your number.n");
scanf ("%i", &number);
while ( number != 0 )
{
right_digit = number % 10;
printf ("%i", right_digit);
number = number / 10;
}
printf ("n");
return 0;
}
14
The do- while Statement
• A do loop is much like a while loop, except that
the expression is tested at the bottom of the
loop, rather than at the top.
15
initialization;
do
{
Body of the loop
}
while (test condition);
The do- while Statement (Cont…)
#include <stdio.h>
int main ()
{
int x =1;
do
{
printf ("%i ,",x);
x =x+1;
}
while (x <= 10);
return 0;
}
16
Increment
Loop body
Loop variable declaration and
initialization
test condition
The do- while Statement (Cont…)
• Execution of the do statement proceeds as follows:
▫ The body of the loop is executed first.
▫ Next, the test_condition inside the parentheses is
evaluated.
▫ If the result of evaluating the test_condition is TRUE,
the loop continues and the program statement is once
again executed.
▫ As long as evaluation of the test_condition continues
to be TRUE, the body of the loop is repeatedly
executed.
▫ When evaluation of the expression proves FALSE, the
loop is terminated, and the next statement in the
program is executed in the normal sequential manner.
17
The do- while Statement (Cont…)
#include <stdio.h>
int main ()
{
int number, right_digit;
printf ("Enter your number.n");
scanf ("%i", &number);
do
{
right_digit = number % 10;
printf ("%i", right_digit);
number = number / 10;
}
while ( number != 0 );
printf ("n");
return 0;
}
18
The For Statement
• The for loop is an entry-controlled loop that
provides a more concise loop control structure.
• The general form of the for loop is,
19
declaration
for (initialization ; test condition; increment)
{
Body of the loop
}
The For Statement (Cont…)
#include <stdio.h>
int main ()
{
int x;
for (x = 0; x<10; x= x+1 )
printf ("%i ,",x);
return 0;
}
20
x++ is also
possible
Braces are not mandatory to state the
scope/ boundary of the loop body if it
has single statement to be executed.
Loop variable
Initialization, test condition, increment
The For Statement (Cont…)
• The for statement allows for negative
increments.
#include <stdio.h>
int main ()
{
int x;
for (x = 10; x>0; x-- )
printf ("%i ,",x);
return 0;
}
21
The For Statement (Cont…)
• In summary, execution of the for statement proceeds as
follows:
1. The initialization expression is evaluated first. This expression usually
sets a variable that will be used inside the loop, generally referred to as
an loop variable and it has an initial value.
2. The looping condition is evaluated. If the condition is not satisfied (the
expression is FALSE), the loop is immediately terminated. Otherwise,
execution continues with the program statement that immediately
follows the loop.
3. The program statement that constitutes the body of the loop is executed.
4. The looping expression is evaluated. This expression is generally used to
change the value of the index variable, frequently by adding 1 to it or
subtracting 1 from it.
5. Return to step 2.
22
The For Statement (Cont…)
• Triangular Number Generator
#include <stdio.h>
int main (void)
{
int n, triangularNumber;
printf ("TABLE OF TRIANGULAR NUMBERSnn");
printf (" n Sum from 1 to nn");
printf ("--- ---------------n");
triangularNumber = 0;
for ( n = 1; n <= 10; ++n ) {
triangularNumber += n;
printf (" %2i %in", n, triangularNumber);
}
return 0;
}
23
The For Statement (Cont…)
#include <stdio.h>
int main (void)
{
int n, number, triangularNumber;
printf ("What triangular number do you want? ");
scanf ("%i", &number);
triangularNumber = 0;
for ( n = 1; n <= number; ++n )
triangularNumber += n;
printf ("Triangular number %i is %in", number,
triangularNumber);
return 0;
}
24
The For Statement (Cont…)
#include <stdio.h>
int main (void)
{
int n, number, triangularNumber, counter;
for ( counter = 1; counter <= 5; ++counter ) {
printf ("What triangular number do you want? ");
scanf ("%i", &number);
triangularNumber = 0;
for ( n = 1; n <= number; ++n )
triangularNumber += n;
printf ("Triangular number %i is %inn", number,
triangularNumber);
}
return 0;
}
25
for Loop Variants
• Multiple Expressions
▫ You can include multiple expressions in any of the
fields of the for loop, provided that you separate
such expressions by commas.
▫ For example, in the for statement that begins
for ( i = 0, j = 0; i < 10; ++i )
...
▫ The two expressions i = 0 and j = 0 are separated
from each other by a comma, and both
expressions are considered part of the init_expression
or variable initialization field of the loop.
26
for Loop Variants (Cont…)
▫ As another example, the for loop that starts
for ( i = 0, j = 100; i < 10; ++i, j = j - 10 )
...
▫ sets up two index variables, i and j; the former
initialized to 0 and the latter to 100 before the loop
begins.
▫ Each time after the body of the loop is executed,
the value of I is incremented by 1, whereas the
value of j is decremented by 10.
27
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
int x, y ;
for (x = 1, y = 100; x<=10; x++, y=y-10 )
printf ("%i ,",x* y);
return 0;
}
28
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
int x, y ;
for (x = 1, y = 100; x<=10 && y>=50; x++, y=y-10 )
printf ("%i ,",x* y);
return 0;
}
29
for Loop Variants (Cont…)
• Omitting Fields
▫ This can be done simply by omitting the desired field
and marking its place with a semicolon.
for ( ; j != 100; ++j )
...
▫ Or
for (j = 0 ; j != 100; ++j )
...
▫ Or
for ( ; j != 100; )
...
30
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
int x =1;
for (x ; x<=10; x++)
printf ("%i ,",x);
return 0;
}
31
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
int x;
for (x =1 ; x<=10;){
printf ("%i ,",x);
x++;
}
return 0;
}
32
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
int x =1;
for ( ; x<=10;){
printf ("%i ,",x);
x++;
}
return 0;
}
33
for Loop Variants (Cont…)
• Infinite loop
▫ A loop becomes an infinite loop if a condition
never becomes false.
▫ The for loop is traditionally used for this purpose.
▫ Since none of the three expressions that form the
'for' loop are required, you can make an endless
loop by leaving the conditional expression empty.
34
for Loop Variants (Cont…)
#include <stdio.h>
int main ()
{
for( ; ; )
{
printf("This loop will run forever.n");
}
return 0;
}
35
Nesting of for Loop
36
Nesting of for Loop (Cont…)
• A program segment to print a multiplication table.
……………………….
for (row = 1; row <= ROWMAX; ++row)
{
for (column = 1; column <= COLMAX; ++column)
{
y = row * column;
printf (“%4i ”, y) ;
}
printf(“n”);
}
…………………
• The outer loop controls the rows while the inner one controls the
columns.
37
Nesting of for Loop (Cont…)
#include <stdio.h>
int main ()
{
const int ROWMAX =12;
const int COLMAX = 12;
int row, column, y;
for (row = 1; row <= ROWMAX; ++row)
for (column = 1; column <= COLMAX; ++column)
{
y = row * column;
printf ("%4i ", y) ;
}
printf ("n");
return 0;
}
38
Pay your attention for
scope declaration.
When to use for Loop
• In general, a loop executed a predetermined
number of times is a prime candidate for
implementation as a for statement.
• Also, if the initial expression, looping
expression, and looping condition all involve the
same variable, the for statement is probably the
right choice.
39
The break Statement
• The break Statement Sometimes when executing a loop, it
becomes desirable to leave the loop as soon as a certain
condition occurs (for instance, you detect an error condition,
or you reach the end of your data prematurely).
• The break statement can be used for this purpose.
• Execution of the break statement causes the program to
immediately exit from the loop it is executing, whether it’s a
for, while, or do loop.
• Subsequent statements in the loop are skipped, and execution
of the loop is terminated.
• Execution continues with whatever statement follows the
loop.
• If a break is executed from within a set of nested loops, only
the innermost loop in which the break is executed is
terminated.
40
The break Statement (Cont…)
41
The break Statement (Cont…)
#include <stdio.h>
int main ()
{
int x = 1;
while (1)
{
printf ("%i ", x );
if (x == 5)
break;
x =x+1;
}
return 0;
}
42
The break Statement (Cont…)
#include <stdio.h>
int main ()
{
int x, y;
for ( x = 1; x<10; x++)
{
for ( y = 1; y<10; y++)
{
printf ("%2i ", x );
if (y == x)
break;
}
printf ("n");
}
return 0;
}
43
The continue Statement
• The continue statement is similar to the break statement
except it doesn’t cause the loop to terminate.
• Rather, as its name implies, this statement causes the
loop in which it is executed to be continued.
• At the point that the continue statement is executed, any
statements in the loop that appear after the continue
statement are automatically skipped.
• Execution of the loop otherwise continues as normal.
• The continue statement is most often used to bypass a
group of statements inside a loop based upon some
condition, but to otherwise continue execution of the
loop.
44
The continue Statement (Cont…)
45
The continue Statement (Cont…)
#include <stdio.h>
int main ()
{
int x, y;
for ( x = 1; x<10; x++)
{
for ( y = 1; y<10; y++)
{
if (y == x)
continue;
printf ("%2i ", x );
}
printf ("n");
}
return 0;
}
46
The goto Statement
• A goto statement in C programming provides an
unconditional jump from the 'goto' to a labeled
statement in the same function.
• NOTE − Use of goto statement is highly
discouraged in any programming language
because it makes difficult to trace the control
flow of a program, making the program hard to
understand and hard to modify.
• Any program that uses a goto can be rewritten to
avoid them.
47
The goto Statement (Cont…)
#include <stdio.h>
int main ()
{
int a = 10;
LOOP:do {
if( a == 15) {
/* skip the iteration */
a = a + 1;
goto LOOP;
}
printf("value of a: %dn", a);
a++;
}while( a < 20 );
return 0;
}
48
Objective Re-cap
• Now you should be able to:
▫ Describe the looping structures in C programming
language.
▫ Practice the control flow of different looping
structures in C programming language.
▫ Practice the variants in control flow of different
looping structures in C programming language.
▫ Apply taught concepts for writing programs.
49
References
• Chapter 05, - Programming in C, 3rd Edition,
Stephen G. Kochan
50
Next: Arrays
51

More Related Content

What's hot

Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statementsrajshreemuthiah
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or javaSamsil Arefin
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c languageshhanks
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.Haard Shah
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements loopingMomenMostafa
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02Wingston
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshareGagan Deep
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c languagetanmaymodi4
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)Way2itech
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c languagetanmaymodi4
 
Control structure C++
Control structure C++Control structure C++
Control structure C++Anil Kumar
 

What's hot (18)

Control and conditional statements
Control and conditional statementsControl and conditional statements
Control and conditional statements
 
Ch3 selection
Ch3 selectionCh3 selection
Ch3 selection
 
Structure in programming in c or c++ or c# or java
Structure in programming  in c or c++ or c# or javaStructure in programming  in c or c++ or c# or java
Structure in programming in c or c++ or c# or java
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
Control structure
Control structureControl structure
Control structure
 
C decision making and looping.
C decision making and looping.C decision making and looping.
C decision making and looping.
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
5 c control statements looping
5  c control statements looping5  c control statements looping
5 c control statements looping
 
Introduction to Basic C programming 02
Introduction to Basic C programming 02Introduction to Basic C programming 02
Introduction to Basic C programming 02
 
C lecture 3 control statements slideshare
C lecture 3 control statements slideshareC lecture 3 control statements slideshare
C lecture 3 control statements slideshare
 
Decision statements in c language
Decision statements in c languageDecision statements in c language
Decision statements in c language
 
Looping statements
Looping statementsLooping statements
Looping statements
 
9. statements (conditional statements)
9. statements (conditional statements)9. statements (conditional statements)
9. statements (conditional statements)
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
Operators and expressions in c language
Operators and expressions in c languageOperators and expressions in c language
Operators and expressions in c language
 
Control statements
Control statementsControl statements
Control statements
 
Control structure C++
Control structure C++Control structure C++
Control structure C++
 

Viewers also liked

Pseudocode-Flowchart
Pseudocode-FlowchartPseudocode-Flowchart
Pseudocode-Flowchartlotlot
 
Basic Programming Concept
Basic Programming ConceptBasic Programming Concept
Basic Programming ConceptCma Mohd
 
Programming process and flowchart
Programming process and flowchartProgramming process and flowchart
Programming process and flowcharthermiraguilar
 
Algorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and SortingAlgorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and SortingRishabh Mehan
 
Pseudocode flowcharts
Pseudocode flowchartsPseudocode flowcharts
Pseudocode flowchartsnicky_walters
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codeshermiraguilar
 
pseudo code basics
pseudo code basicspseudo code basics
pseudo code basicsSabik T S
 
Flowchart pseudocode-examples
Flowchart pseudocode-examplesFlowchart pseudocode-examples
Flowchart pseudocode-examplesGautam Roy
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languagesVarun Garg
 

Viewers also liked (10)

Pseudocode-Flowchart
Pseudocode-FlowchartPseudocode-Flowchart
Pseudocode-Flowchart
 
Basic Programming Concept
Basic Programming ConceptBasic Programming Concept
Basic Programming Concept
 
Programming process and flowchart
Programming process and flowchartProgramming process and flowchart
Programming process and flowchart
 
Algorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and SortingAlgorithms presentation on Path Matrix, Bell Number and Sorting
Algorithms presentation on Path Matrix, Bell Number and Sorting
 
Pseudocode flowcharts
Pseudocode flowchartsPseudocode flowcharts
Pseudocode flowcharts
 
COM1407: File Processing
COM1407: File Processing COM1407: File Processing
COM1407: File Processing
 
Algorithm and pseudo codes
Algorithm and pseudo codesAlgorithm and pseudo codes
Algorithm and pseudo codes
 
pseudo code basics
pseudo code basicspseudo code basics
pseudo code basics
 
Flowchart pseudocode-examples
Flowchart pseudocode-examplesFlowchart pseudocode-examples
Flowchart pseudocode-examples
 
Lect 1. introduction to programming languages
Lect 1. introduction to programming languagesLect 1. introduction to programming languages
Lect 1. introduction to programming languages
 

Similar to COM1407: Program Control Structures – Repetition and Loops

Similar to COM1407: Program Control Structures – Repetition and Loops (20)

12 lec 12 loop
12 lec 12 loop 12 lec 12 loop
12 lec 12 loop
 
Looping
LoopingLooping
Looping
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
Control Structures in C
Control Structures in CControl Structures in C
Control Structures in C
 
Session05 iteration structure
Session05 iteration structureSession05 iteration structure
Session05 iteration structure
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
Code optimization
Code optimization Code optimization
Code optimization
 
Code optimization
Code optimization Code optimization
Code optimization
 
Introduction to c part -1
Introduction to c   part -1Introduction to c   part -1
Introduction to c part -1
 
Session 3
Session 3Session 3
Session 3
 
Loop control structure
Loop control structureLoop control structure
Loop control structure
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Programming ppt files (final)
Programming ppt files (final)Programming ppt files (final)
Programming ppt files (final)
 
1 introduction to c program
1 introduction to c program1 introduction to c program
1 introduction to c program
 
What is c
What is cWhat is c
What is c
 
C++ decision making
C++ decision makingC++ decision making
C++ decision making
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
Control structuresin c
Control structuresin cControl structuresin c
Control structuresin c
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Programming fundamental 02
Programming fundamental 02Programming fundamental 02
Programming fundamental 02
 

More from Hemantha Kulathilake

NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar Hemantha Kulathilake
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishHemantha Kulathilake
 
NLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelNLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelHemantha Kulathilake
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingHemantha Kulathilake
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation Hemantha Kulathilake
 

More from Hemantha Kulathilake (20)

NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar NLP_KASHK:Parsing with Context-Free Grammar
NLP_KASHK:Parsing with Context-Free Grammar
 
NLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for EnglishNLP_KASHK:Context-Free Grammar for English
NLP_KASHK:Context-Free Grammar for English
 
NLP_KASHK:POS Tagging
NLP_KASHK:POS TaggingNLP_KASHK:POS Tagging
NLP_KASHK:POS Tagging
 
NLP_KASHK:Markov Models
NLP_KASHK:Markov ModelsNLP_KASHK:Markov Models
NLP_KASHK:Markov Models
 
NLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram ModelsNLP_KASHK:Smoothing N-gram Models
NLP_KASHK:Smoothing N-gram Models
 
NLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language ModelNLP_KASHK:Evaluating Language Model
NLP_KASHK:Evaluating Language Model
 
NLP_KASHK:N-Grams
NLP_KASHK:N-GramsNLP_KASHK:N-Grams
NLP_KASHK:N-Grams
 
NLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit DistanceNLP_KASHK:Minimum Edit Distance
NLP_KASHK:Minimum Edit Distance
 
NLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological ParsingNLP_KASHK:Finite-State Morphological Parsing
NLP_KASHK:Finite-State Morphological Parsing
 
NLP_KASHK:Morphology
NLP_KASHK:MorphologyNLP_KASHK:Morphology
NLP_KASHK:Morphology
 
NLP_KASHK:Text Normalization
NLP_KASHK:Text NormalizationNLP_KASHK:Text Normalization
NLP_KASHK:Text Normalization
 
NLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State AutomataNLP_KASHK:Finite-State Automata
NLP_KASHK:Finite-State Automata
 
NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions NLP_KASHK:Regular Expressions
NLP_KASHK:Regular Expressions
 
NLP_KASHK: Introduction
NLP_KASHK: Introduction NLP_KASHK: Introduction
NLP_KASHK: Introduction
 
COm1407: Character & Strings
COm1407: Character & StringsCOm1407: Character & Strings
COm1407: Character & Strings
 
COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation COM1407: Structures, Unions & Dynamic Memory Allocation
COM1407: Structures, Unions & Dynamic Memory Allocation
 
COM1407: Input/ Output Functions
COM1407: Input/ Output FunctionsCOM1407: Input/ Output Functions
COM1407: Input/ Output Functions
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
COM1407: Arrays
COM1407: ArraysCOM1407: Arrays
COM1407: Arrays
 
COM1407: C Operators
COM1407: C OperatorsCOM1407: C Operators
COM1407: C Operators
 

Recently uploaded

4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptxmary850239
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipKarl Donert
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQuiz Club NITW
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfChristalin Nelson
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxAnupam32727
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfChristalin Nelson
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxMadhavi Dharankar
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...Nguyen Thanh Tu Collection
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Association for Project Management
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDhatriParmar
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Projectjordimapav
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfPrerana Jadhav
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEMISSRITIMABIOLOGYEXP
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineCeline George
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...HetalPathak10
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWQuiz Club NITW
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesVijayaLaxmi84
 

Recently uploaded (20)

4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx4.11.24 Poverty and Inequality in America.pptx
4.11.24 Poverty and Inequality in America.pptx
 
The role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenshipThe role of Geography in climate education: science and active citizenship
The role of Geography in climate education: science and active citizenship
 
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITWQ-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
Q-Factor HISPOL Quiz-6th April 2024, Quiz Club NITW
 
Indexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdfIndexing Structures in Database Management system.pdf
Indexing Structures in Database Management system.pdf
 
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptxCLASSIFICATION OF ANTI - CANCER DRUGS.pptx
CLASSIFICATION OF ANTI - CANCER DRUGS.pptx
 
DBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdfDBMSArchitecture_QueryProcessingandOptimization.pdf
DBMSArchitecture_QueryProcessingandOptimization.pdf
 
Objectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptxObjectives n learning outcoms - MD 20240404.pptx
Objectives n learning outcoms - MD 20240404.pptx
 
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
31 ĐỀ THI THỬ VÀO LỚP 10 - TIẾNG ANH - FORM MỚI 2025 - 40 CÂU HỎI - BÙI VĂN V...
 
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
Team Lead Succeed – Helping you and your team achieve high-performance teamwo...
 
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptxDecoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
Decoding the Tweet _ Practical Criticism in the Age of Hashtag.pptx
 
ClimART Action | eTwinning Project
ClimART Action    |    eTwinning ProjectClimART Action    |    eTwinning Project
ClimART Action | eTwinning Project
 
prashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Professionprashanth updated resume 2024 for Teaching Profession
prashanth updated resume 2024 for Teaching Profession
 
Narcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdfNarcotic and Non Narcotic Analgesic..pdf
Narcotic and Non Narcotic Analgesic..pdf
 
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFEPART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
PART 1 - CHAPTER 1 - CELL THE FUNDAMENTAL UNIT OF LIFE
 
Paradigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTAParadigm shift in nursing research by RS MEHTA
Paradigm shift in nursing research by RS MEHTA
 
How to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command LineHow to Uninstall a Module in Odoo 17 Using Command Line
How to Uninstall a Module in Odoo 17 Using Command Line
 
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
Satirical Depths - A Study of Gabriel Okara's Poem - 'You Laughed and Laughed...
 
Mythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITWMythology Quiz-4th April 2024, Quiz Club NITW
Mythology Quiz-4th April 2024, Quiz Club NITW
 
CARNAVAL COM MAGIA E EUFORIA _
CARNAVAL COM MAGIA E EUFORIA            _CARNAVAL COM MAGIA E EUFORIA            _
CARNAVAL COM MAGIA E EUFORIA _
 
Sulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their usesSulphonamides, mechanisms and their uses
Sulphonamides, mechanisms and their uses
 

COM1407: Program Control Structures – Repetition and Loops

  • 1. COM1407 Computer Programming Lecture 07 Program Control Structures – Repetition and Loops K.A.S.H. Kulathilake B.Sc. (Hons) IT, MCS , M.Phil., SEDA(UK) Rajarata University of Sri Lanka Department of Physical Sciences
  • 2. Objectives • At the end of this lecture students should be able to; ▫ Describe the looping structures in C programming language. ▫ Practice the control flow of different looping structures in C programming language. ▫ Practice the variants in control flow of different looping structures in C programming language. ▫ Apply taught concepts for writing programs. 2
  • 3. Introduction • One of the fundamental properties of a computer is its ability to repetitively execute a set of statements. • These looping capabilities enable you to develop concise programs containing repetitive processes that could otherwise require thousands or even millions of program statements to perform. • If a loop continue forever, it is called infinite loop. • A Looping consist of 2 segments. ▫ Body of the Loop ▫ Control statement • A control structure may be classified either, ▫ As the entry controlled loop or ▫ Exit-controlled loop 3
  • 4. Introduction (Cont…) • Entry controlled loop ▫ In entry controlled loop, the control conditions are tested before the start of loop execution. ▫ If the conditions are not satisfied, then the body of the loop will not be executed. 4
  • 5. Introduction (Cont…) • Exit-controlled loop ▫ The test is performed at the end of the body of the loop and therefore the body is executed unconditionally for the first time. 5
  • 6. Introduction (Cont…) • There are five parts of a looping construct; ▫ Loop control variable (Declaration) ▫ Initial value of the loop control variable (Initialization) ▫ Loop control condition (Test condition) ▫ Repetitive statements (Body of the Loop) ▫ Increment the loop control variable (Increment) 6
  • 7. Introduction (Cont…) • The C programming language contains three different program statements for program looping. ▫ For statement, ▫ While statement, ▫ Do statement. 7
  • 8. The while Statement • The while is an entry-controlled loop statement. • The basic format of the while statement is, 8 initialization; while (test condition) { body of the loop }
  • 9. The while Statement (Cont…) #include <stdio.h> int main () { int x =1; while (x <= 10) { printf ("%i ,",x); x =x+1; } return 0; } 9 Increment Loop body Loop variable declaration and initialization test condition
  • 10. The while Statement (Cont…) • The test_condition specified inside the parentheses is evaluated. • If the result of the expression evaluation is TRUE, the body of the loop that immediately follows is executed. • After execution of this statement (or statements if enclosed in braces), the test_condition is once again evaluated. • If the result of the evaluation is TRUE, the body of the loop is once again executed. • This process continues until the test_condition finally evaluates as FALSE, at which point the loop is terminated. • Execution of the program then continues with the statement that follows the body of the loop. 10
  • 11. The while Statement (Cont…) #include <stdio.h> int main (void) { int count = 1; while ( count <= 5 ) { printf ("%in", count); ++count; } return 0; } 11
  • 12. The while Statement (Cont…) • The next program computes the greatest common divisor of two integer values. • The greatest common divisor (gcd) of two integers is the largest integer value that evenly divides the two integers. 12
  • 13. The while Statement (Cont…) #include <stdio.h> int main (void) { int u, v, temp; printf ("Please type in two nonnegative integers.n"); scanf ("%i%i", &u, &v); while ( v != 0 ) { temp = u % v; u = v; v = temp; } printf ("Their greatest common divisor is %in", u); return 0; } 13
  • 14. The while Statement (Cont…) #include <stdio.h> int main (void) { int number, right_digit; printf ("Enter your number.n"); scanf ("%i", &number); while ( number != 0 ) { right_digit = number % 10; printf ("%i", right_digit); number = number / 10; } printf ("n"); return 0; } 14
  • 15. The do- while Statement • A do loop is much like a while loop, except that the expression is tested at the bottom of the loop, rather than at the top. 15 initialization; do { Body of the loop } while (test condition);
  • 16. The do- while Statement (Cont…) #include <stdio.h> int main () { int x =1; do { printf ("%i ,",x); x =x+1; } while (x <= 10); return 0; } 16 Increment Loop body Loop variable declaration and initialization test condition
  • 17. The do- while Statement (Cont…) • Execution of the do statement proceeds as follows: ▫ The body of the loop is executed first. ▫ Next, the test_condition inside the parentheses is evaluated. ▫ If the result of evaluating the test_condition is TRUE, the loop continues and the program statement is once again executed. ▫ As long as evaluation of the test_condition continues to be TRUE, the body of the loop is repeatedly executed. ▫ When evaluation of the expression proves FALSE, the loop is terminated, and the next statement in the program is executed in the normal sequential manner. 17
  • 18. The do- while Statement (Cont…) #include <stdio.h> int main () { int number, right_digit; printf ("Enter your number.n"); scanf ("%i", &number); do { right_digit = number % 10; printf ("%i", right_digit); number = number / 10; } while ( number != 0 ); printf ("n"); return 0; } 18
  • 19. The For Statement • The for loop is an entry-controlled loop that provides a more concise loop control structure. • The general form of the for loop is, 19 declaration for (initialization ; test condition; increment) { Body of the loop }
  • 20. The For Statement (Cont…) #include <stdio.h> int main () { int x; for (x = 0; x<10; x= x+1 ) printf ("%i ,",x); return 0; } 20 x++ is also possible Braces are not mandatory to state the scope/ boundary of the loop body if it has single statement to be executed. Loop variable Initialization, test condition, increment
  • 21. The For Statement (Cont…) • The for statement allows for negative increments. #include <stdio.h> int main () { int x; for (x = 10; x>0; x-- ) printf ("%i ,",x); return 0; } 21
  • 22. The For Statement (Cont…) • In summary, execution of the for statement proceeds as follows: 1. The initialization expression is evaluated first. This expression usually sets a variable that will be used inside the loop, generally referred to as an loop variable and it has an initial value. 2. The looping condition is evaluated. If the condition is not satisfied (the expression is FALSE), the loop is immediately terminated. Otherwise, execution continues with the program statement that immediately follows the loop. 3. The program statement that constitutes the body of the loop is executed. 4. The looping expression is evaluated. This expression is generally used to change the value of the index variable, frequently by adding 1 to it or subtracting 1 from it. 5. Return to step 2. 22
  • 23. The For Statement (Cont…) • Triangular Number Generator #include <stdio.h> int main (void) { int n, triangularNumber; printf ("TABLE OF TRIANGULAR NUMBERSnn"); printf (" n Sum from 1 to nn"); printf ("--- ---------------n"); triangularNumber = 0; for ( n = 1; n <= 10; ++n ) { triangularNumber += n; printf (" %2i %in", n, triangularNumber); } return 0; } 23
  • 24. The For Statement (Cont…) #include <stdio.h> int main (void) { int n, number, triangularNumber; printf ("What triangular number do you want? "); scanf ("%i", &number); triangularNumber = 0; for ( n = 1; n <= number; ++n ) triangularNumber += n; printf ("Triangular number %i is %in", number, triangularNumber); return 0; } 24
  • 25. The For Statement (Cont…) #include <stdio.h> int main (void) { int n, number, triangularNumber, counter; for ( counter = 1; counter <= 5; ++counter ) { printf ("What triangular number do you want? "); scanf ("%i", &number); triangularNumber = 0; for ( n = 1; n <= number; ++n ) triangularNumber += n; printf ("Triangular number %i is %inn", number, triangularNumber); } return 0; } 25
  • 26. for Loop Variants • Multiple Expressions ▫ You can include multiple expressions in any of the fields of the for loop, provided that you separate such expressions by commas. ▫ For example, in the for statement that begins for ( i = 0, j = 0; i < 10; ++i ) ... ▫ The two expressions i = 0 and j = 0 are separated from each other by a comma, and both expressions are considered part of the init_expression or variable initialization field of the loop. 26
  • 27. for Loop Variants (Cont…) ▫ As another example, the for loop that starts for ( i = 0, j = 100; i < 10; ++i, j = j - 10 ) ... ▫ sets up two index variables, i and j; the former initialized to 0 and the latter to 100 before the loop begins. ▫ Each time after the body of the loop is executed, the value of I is incremented by 1, whereas the value of j is decremented by 10. 27
  • 28. for Loop Variants (Cont…) #include <stdio.h> int main () { int x, y ; for (x = 1, y = 100; x<=10; x++, y=y-10 ) printf ("%i ,",x* y); return 0; } 28
  • 29. for Loop Variants (Cont…) #include <stdio.h> int main () { int x, y ; for (x = 1, y = 100; x<=10 && y>=50; x++, y=y-10 ) printf ("%i ,",x* y); return 0; } 29
  • 30. for Loop Variants (Cont…) • Omitting Fields ▫ This can be done simply by omitting the desired field and marking its place with a semicolon. for ( ; j != 100; ++j ) ... ▫ Or for (j = 0 ; j != 100; ++j ) ... ▫ Or for ( ; j != 100; ) ... 30
  • 31. for Loop Variants (Cont…) #include <stdio.h> int main () { int x =1; for (x ; x<=10; x++) printf ("%i ,",x); return 0; } 31
  • 32. for Loop Variants (Cont…) #include <stdio.h> int main () { int x; for (x =1 ; x<=10;){ printf ("%i ,",x); x++; } return 0; } 32
  • 33. for Loop Variants (Cont…) #include <stdio.h> int main () { int x =1; for ( ; x<=10;){ printf ("%i ,",x); x++; } return 0; } 33
  • 34. for Loop Variants (Cont…) • Infinite loop ▫ A loop becomes an infinite loop if a condition never becomes false. ▫ The for loop is traditionally used for this purpose. ▫ Since none of the three expressions that form the 'for' loop are required, you can make an endless loop by leaving the conditional expression empty. 34
  • 35. for Loop Variants (Cont…) #include <stdio.h> int main () { for( ; ; ) { printf("This loop will run forever.n"); } return 0; } 35
  • 36. Nesting of for Loop 36
  • 37. Nesting of for Loop (Cont…) • A program segment to print a multiplication table. ………………………. for (row = 1; row <= ROWMAX; ++row) { for (column = 1; column <= COLMAX; ++column) { y = row * column; printf (“%4i ”, y) ; } printf(“n”); } ………………… • The outer loop controls the rows while the inner one controls the columns. 37
  • 38. Nesting of for Loop (Cont…) #include <stdio.h> int main () { const int ROWMAX =12; const int COLMAX = 12; int row, column, y; for (row = 1; row <= ROWMAX; ++row) for (column = 1; column <= COLMAX; ++column) { y = row * column; printf ("%4i ", y) ; } printf ("n"); return 0; } 38 Pay your attention for scope declaration.
  • 39. When to use for Loop • In general, a loop executed a predetermined number of times is a prime candidate for implementation as a for statement. • Also, if the initial expression, looping expression, and looping condition all involve the same variable, the for statement is probably the right choice. 39
  • 40. The break Statement • The break Statement Sometimes when executing a loop, it becomes desirable to leave the loop as soon as a certain condition occurs (for instance, you detect an error condition, or you reach the end of your data prematurely). • The break statement can be used for this purpose. • Execution of the break statement causes the program to immediately exit from the loop it is executing, whether it’s a for, while, or do loop. • Subsequent statements in the loop are skipped, and execution of the loop is terminated. • Execution continues with whatever statement follows the loop. • If a break is executed from within a set of nested loops, only the innermost loop in which the break is executed is terminated. 40
  • 41. The break Statement (Cont…) 41
  • 42. The break Statement (Cont…) #include <stdio.h> int main () { int x = 1; while (1) { printf ("%i ", x ); if (x == 5) break; x =x+1; } return 0; } 42
  • 43. The break Statement (Cont…) #include <stdio.h> int main () { int x, y; for ( x = 1; x<10; x++) { for ( y = 1; y<10; y++) { printf ("%2i ", x ); if (y == x) break; } printf ("n"); } return 0; } 43
  • 44. The continue Statement • The continue statement is similar to the break statement except it doesn’t cause the loop to terminate. • Rather, as its name implies, this statement causes the loop in which it is executed to be continued. • At the point that the continue statement is executed, any statements in the loop that appear after the continue statement are automatically skipped. • Execution of the loop otherwise continues as normal. • The continue statement is most often used to bypass a group of statements inside a loop based upon some condition, but to otherwise continue execution of the loop. 44
  • 45. The continue Statement (Cont…) 45
  • 46. The continue Statement (Cont…) #include <stdio.h> int main () { int x, y; for ( x = 1; x<10; x++) { for ( y = 1; y<10; y++) { if (y == x) continue; printf ("%2i ", x ); } printf ("n"); } return 0; } 46
  • 47. The goto Statement • A goto statement in C programming provides an unconditional jump from the 'goto' to a labeled statement in the same function. • NOTE − Use of goto statement is highly discouraged in any programming language because it makes difficult to trace the control flow of a program, making the program hard to understand and hard to modify. • Any program that uses a goto can be rewritten to avoid them. 47
  • 48. The goto Statement (Cont…) #include <stdio.h> int main () { int a = 10; LOOP:do { if( a == 15) { /* skip the iteration */ a = a + 1; goto LOOP; } printf("value of a: %dn", a); a++; }while( a < 20 ); return 0; } 48
  • 49. Objective Re-cap • Now you should be able to: ▫ Describe the looping structures in C programming language. ▫ Practice the control flow of different looping structures in C programming language. ▫ Practice the variants in control flow of different looping structures in C programming language. ▫ Apply taught concepts for writing programs. 49
  • 50. References • Chapter 05, - Programming in C, 3rd Edition, Stephen G. Kochan 50