SlideShare a Scribd company logo
2
Most read
4
Most read
@ 2010 Tata McGraw-Hill Education
1
Education
Decision Making and LoopingDecision Making and Looping
@ 2010 Tata McGraw-Hill Education
2
Education
Introduction
A computer is well suited to perform repetitive operations. It can do so
tirelessly ten, hundred or even ten thousand times. Every computer language
must have features that instruct a computer to perform such repetitive tasks.
The process of repeatedly executing a block of statements is known as
looping. The statements in the block may be executed any number of times,
from zero to an infinite
number. If a loop continues forever, it is called an infinite loop. C# supports
such looping features that enable us to develop concise programs containing
repetitive processes without using unconditional branching statements like
the goto statement.
@ 2010 Tata McGraw-Hill Education
3
Education
A looping process, in general, would include the following four steps:
1. Setting and initialization of a counter.
2. Execution of the statements in the loop.
3. Test for a specifi ed condition for execution of the loop.
4. Incrementing the counter.
The test may be either to determine whether the loop has been repeated the
specified number of times or to determine whether a particular condition has
been met with. The C# language provides for four constructs for performing
loop operations. They are:
1. The while statement
2. The do statement
3. The for statement
4. The foreach statement
@ 2010 Tata McGraw-Hill Education
4
Education
These statements are known as iteration or looping statements. We shall
discuss the features and applications of each of these statements in this
chapter.
@ 2010 Tata McGraw-Hill Education
5
Education
THE WHILE STATEMENT
The simplest of all the looping structures in C# is the while statement. The basic
format of the while statement is
initialization;
while(test condition)
{
Body of the loop
}
while is an entry-controlled loop statement. The test condition is evaluated and if the
condition is true, then the body of the loop is executed
@ 2010 Tata McGraw-Hill Education
6
Education
THE DO STATEMENT
The while loop construct that we have discussed in the previous section makes a test
condition before the loop is executed. Therefore, the body of the loop may not be
executed at all if the condition is not satisfied at the very fi rst attempt. On some
occasions it might be necessary to execute the body of the loop before the test is
performed. Such situations can be handled with the help of the do statement. This
takes the form:
initialization;
do
{
Body of the loop
}
While (test conditions);
@ 2010 Tata McGraw-Hill Education
7
Education
THE FOR STATEMENT
for is another entry-controlled loop that provides a more concise loop-control structure. The
general form of the for loop is
for (initialization ; test condition ; increment)
{
Body of the loop
}
@ 2010 Tata McGraw-Hill Education
8
Education
THE FOREACH STATEMENT
The foreach statement is similar to the for statement but implemented differently. It
enables us to iterate the elements in arrays and collection classes such as List and
HashTable. The general form of the foreach statement is:
foreach (type variable in expression)
{
Body of the loop
}
The type and variable declare the iteration variable. During execution, the iteration
variable represents the array element (or collection element in case of collections) for
which an iteration is currently being performed. in is a keyword
@ 2010 Tata McGraw-Hill Education
9
Education
JUMPS IN LOOPS
Loops perform a set of operations repeatedly until the control variable fails to satisfy
the test condition. The number of times a loop is repeated is decided in advance and the
test condition is written to achieve this.
Jumping Out of a Loop
An early exit from a loop can be accomplished by using the break and goto
statements. We have already seen the use of the break in the switch statement.
These statements can also be used within while, do or for loops for an early exit
Skipping a Part of a Loop
During the loop operations, it may be necessary to skip a part of the body of the loop
under certain conditions
@ 2010 Tata McGraw-Hill Education
10
Education

More Related Content

PPTX
Introduction to C# Programming
PDF
Internationalization
PPTX
C# classes objects
PPTX
Introduction to Java -unit-1
PPTX
Java literals
ODP
Datatype in JavaScript
PPT
PPT
Command line arguments.21
Introduction to C# Programming
Internationalization
C# classes objects
Introduction to Java -unit-1
Java literals
Datatype in JavaScript
Command line arguments.21

What's hot (20)

PPTX
C# lecture 2: Literals , Variables and Data Types in C#
PPT
Constants in C Programming
PPTX
C# programming language
PPTX
Java constructors
PPTX
Java applet - java
PPTX
Pointers,virtual functions and polymorphism cpp
PPT
Java features
PPT
UML Diagrams
PPTX
Interfaces in java
PPTX
C# 101: Intro to Programming with C#
PPTX
Introduction to c++
PDF
Event Driven programming(ch1 and ch2).pdf
PPTX
Methods in java
PPT
Java tutorial PPT
PDF
Java threads
PPTX
Event handling
PPT
Exception Handling in JAVA
PPT
sets and maps
PPTX
Constructor in java
PDF
Learn C# Programming - Decision Making & Loops
C# lecture 2: Literals , Variables and Data Types in C#
Constants in C Programming
C# programming language
Java constructors
Java applet - java
Pointers,virtual functions and polymorphism cpp
Java features
UML Diagrams
Interfaces in java
C# 101: Intro to Programming with C#
Introduction to c++
Event Driven programming(ch1 and ch2).pdf
Methods in java
Java tutorial PPT
Java threads
Event handling
Exception Handling in JAVA
sets and maps
Constructor in java
Learn C# Programming - Decision Making & Loops
Ad

Similar to Decision making and loop in C# (20)

PPT
Visula C# Programming Lecture 4
PPT
C++ CH3-P2 using c++ in all other parts.ppt
PPTX
lecture-06 Lopikjjiu8iuuiujijijijioop.pptx
PPT
Csc153 chapter 05
 
PPT
Eo gaddis java_chapter_05_5e
PPT
Eo gaddis java_chapter_05_5e
PDF
Chapter 9 - Loops in C++
PDF
Loops and Files
PPT
Cso gaddis java_chapter4
PPTX
Loops c++
PPTX
10control statement in c#
PDF
PDF
Lo43
PDF
L8
PPT
Java presentation
PPTX
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
PPTX
Looping statements
PPTX
Loop And For Loop in C++ language .pptx
PPTX
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
PDF
final pl paper
Visula C# Programming Lecture 4
C++ CH3-P2 using c++ in all other parts.ppt
lecture-06 Lopikjjiu8iuuiujijijijioop.pptx
Csc153 chapter 05
 
Eo gaddis java_chapter_05_5e
Eo gaddis java_chapter_05_5e
Chapter 9 - Loops in C++
Loops and Files
Cso gaddis java_chapter4
Loops c++
10control statement in c#
Lo43
L8
Java presentation
Adii RDBMS.pptxyfufufufufifigigigiffufufututit
Looping statements
Loop And For Loop in C++ language .pptx
Chapter 5 Loops by z al saeddddddddddddddddddddddddddddddddddd
final pl paper
Ad

More from Prasanna Kumar SM (8)

PPT
C# Introduction brief
PPT
Structure and Enum in c#
PPT
Overview of c#
PPT
Operators and Expressions in C#
PPT
Methods in C#
PPT
Literals,variables,datatype in C#
PPT
Characteristics of c#
PPT
C# Introduction brief
Structure and Enum in c#
Overview of c#
Operators and Expressions in C#
Methods in C#
Literals,variables,datatype in C#
Characteristics of c#

Recently uploaded (20)

PDF
Computing-Curriculum for Schools in Ghana
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
Classroom Observation Tools for Teachers
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Complications of Minimal Access Surgery at WLH
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
Computing-Curriculum for Schools in Ghana
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Complications of Minimal Access Surgery at WLH
STATICS OF THE RIGID BODIES Hibbelers.pdf
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
Module 4: Burden of Disease Tutorial Slides S2 2025
RTP_AR_KS1_Tutor's Guide_English [FOR REPRODUCTION].pdf
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
GDM (1) (1).pptx small presentation for students
Final Presentation General Medicine 03-08-2024.pptx
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
VCE English Exam - Section C Student Revision Booklet
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
O5-L3 Freight Transport Ops (International) V1.pdf
A systematic review of self-coping strategies used by university students to ...

Decision making and loop in C#

  • 1. @ 2010 Tata McGraw-Hill Education 1 Education Decision Making and LoopingDecision Making and Looping
  • 2. @ 2010 Tata McGraw-Hill Education 2 Education Introduction A computer is well suited to perform repetitive operations. It can do so tirelessly ten, hundred or even ten thousand times. Every computer language must have features that instruct a computer to perform such repetitive tasks. The process of repeatedly executing a block of statements is known as looping. The statements in the block may be executed any number of times, from zero to an infinite number. If a loop continues forever, it is called an infinite loop. C# supports such looping features that enable us to develop concise programs containing repetitive processes without using unconditional branching statements like the goto statement.
  • 3. @ 2010 Tata McGraw-Hill Education 3 Education A looping process, in general, would include the following four steps: 1. Setting and initialization of a counter. 2. Execution of the statements in the loop. 3. Test for a specifi ed condition for execution of the loop. 4. Incrementing the counter. The test may be either to determine whether the loop has been repeated the specified number of times or to determine whether a particular condition has been met with. The C# language provides for four constructs for performing loop operations. They are: 1. The while statement 2. The do statement 3. The for statement 4. The foreach statement
  • 4. @ 2010 Tata McGraw-Hill Education 4 Education These statements are known as iteration or looping statements. We shall discuss the features and applications of each of these statements in this chapter.
  • 5. @ 2010 Tata McGraw-Hill Education 5 Education THE WHILE STATEMENT The simplest of all the looping structures in C# is the while statement. The basic format of the while statement is initialization; while(test condition) { Body of the loop } while is an entry-controlled loop statement. The test condition is evaluated and if the condition is true, then the body of the loop is executed
  • 6. @ 2010 Tata McGraw-Hill Education 6 Education THE DO STATEMENT The while loop construct that we have discussed in the previous section makes a test condition before the loop is executed. Therefore, the body of the loop may not be executed at all if the condition is not satisfied at the very fi rst attempt. On some occasions it might be necessary to execute the body of the loop before the test is performed. Such situations can be handled with the help of the do statement. This takes the form: initialization; do { Body of the loop } While (test conditions);
  • 7. @ 2010 Tata McGraw-Hill Education 7 Education THE FOR STATEMENT for is another entry-controlled loop that provides a more concise loop-control structure. The general form of the for loop is for (initialization ; test condition ; increment) { Body of the loop }
  • 8. @ 2010 Tata McGraw-Hill Education 8 Education THE FOREACH STATEMENT The foreach statement is similar to the for statement but implemented differently. It enables us to iterate the elements in arrays and collection classes such as List and HashTable. The general form of the foreach statement is: foreach (type variable in expression) { Body of the loop } The type and variable declare the iteration variable. During execution, the iteration variable represents the array element (or collection element in case of collections) for which an iteration is currently being performed. in is a keyword
  • 9. @ 2010 Tata McGraw-Hill Education 9 Education JUMPS IN LOOPS Loops perform a set of operations repeatedly until the control variable fails to satisfy the test condition. The number of times a loop is repeated is decided in advance and the test condition is written to achieve this. Jumping Out of a Loop An early exit from a loop can be accomplished by using the break and goto statements. We have already seen the use of the break in the switch statement. These statements can also be used within while, do or for loops for an early exit Skipping a Part of a Loop During the loop operations, it may be necessary to skip a part of the body of the loop under certain conditions
  • 10. @ 2010 Tata McGraw-Hill Education 10 Education