KEMBAR78
Looping Construct C# | PDF | Control Flow | C Sharp (Programming Language)
0% found this document useful (0 votes)
43 views18 pages

Looping Construct C#

The document discusses loop constructs in C#, detailing their purpose and types, including for, while, do-while, and foreach loops. It also covers jump statements such as break, continue, goto, and return, explaining their functions in controlling program flow. Additionally, it highlights the concept of nested loops and the importance of conditions in loop execution.

Uploaded by

Arjun khanal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
43 views18 pages

Looping Construct C#

The document discusses loop constructs in C#, detailing their purpose and types, including for, while, do-while, and foreach loops. It also covers jump statements such as break, continue, goto, and return, explaining their functions in controlling program flow. Additionally, it highlights the concept of nested loops and the importance of conditions in loop execution.

Uploaded by

Arjun khanal
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 18

LOOP CONSTRUCTS OR

STATEMENTS IN C#
LOOP CONSTRUCTS
• Loops allow you to execute a single statement or a block of statements
repetitively.
• The most common uses of loops include displaying a series of numbers
and taking repetitive input.
• In software programming, a loop construct contains a condition that
helps the compiler identify the number of times a specific block will be
executed.
• If the condition is not specified, the loop continues infinitely and is
termed as an infinite loop.
LOOP CONSTRUCTS

• The loop constructs are also referred to as iteration statements.


• C# supports four types of loop constructs such as:
• 1. The for loop
• 2. The while loop
• 3. The do..while loop
• 4. The foreach loop
THE FOR LOOP

• The for statement is similar to the while statement in its function.


• The statements within the body of the loop are executed as long as
the condition is true.
• Here too, the condition is checked before the statements are
executed.
THE FOR LOOP

• There are 3 things in FOR LOOP:


• Initialization
• Condition
• Increment/decrement
THE WHILE LOOP

• The while loop is used to execute a block of code repetitively as


long as the condition of the loop remains true.
• The while loop consists of the while statement, which begins with
the while keyword followed by a boolean condition.
• If the condition evaluates to true, the block of statements after the
while statement is executed.
THE WHILE LOOP

• After each iteration, the control is transferred back to the while


statement and the condition is checked again for another round of
execution.
• When the condition is evaluated to false, the block of statements
following the while statement is ignored and the statement
appearing after the block is executed by the compiler.
THE DO-WHILE LOOP

• The do-while loop is similar to the while loop; however, it is always


executed at least once without the condition being checked.
• The loop starts with the do keyword and is followed by a block of
executable statements.
• The while statement along with the condition appears at the end
of this block.
THE DO-WHILE LOOP

• The statements in the do-while loop are executed as long as the


specified condition remains true.
• When the condition evaluates to false, the block of statements
after the do keyword are ignored and the immediate statement
after the while statement is executed.
DIFFERENCE BETWEEN FOR
LOOP AND WHILE LOOP IN
C#
NESTED FOR LOOP

• The nested for loop consists of multiple for statements.


• When one for loop is enclosed inside another for loop, the loops
are said to be nested.
• The for loop that encloses the other for loop is referred to as the
outer for loop whereas the enclosed for loop is referred to as the
inner for loop.
• The outer for loop determines the number of times the inner for
loop will be invoked.
JUMP
STATEMENTS
IN C#
JUMP STATEMENTS IN C#
• Jump statements are used to transfer control from one point in a
program to another.
• There will be situations where you need to exit out of a loop
prematurely and continue with the program.
• In such cases, jump statements are used. Jump statement
unconditionally transfer control of a program to a different
location.
• The location to which a jump statement transfers control is called
the target of the jump statement.
JUMP STATEMENTS IN C#

• C# supports four types of jump statements. These are as


follows:
• break
• continue
• goto
• return
BREAK STATEMENT

• The break statement is used in the selection and loop constructs.


• It is most widely used in the switch...case construct and in the for and
while loops.
• The break statement is denoted by the break keyword. In the
switch...case construct, it is used to terminate the execution of the
construct.
• In loops, it is used to exit the loop without testing the loop condition.
• In this case, the control passes to the next statement following the loop.
CONTINUE STATEMENT

• The continue statement is most widely used in the loop constructs and
is denoted by the continue keyword.
• The continue statement is used to end the current iteration of the loop
and transfer the program control back to the beginning of the loop.
• The statements of the loop following the continue statement are
ignored in the current iteration.
GOTO STATEMENT

• The goto statement allows you to directly execute a labeled statement


or a labeled block of statements.
• A labeled block or a labeled statement starts with a label. A label is an
identifier ending with a colon.
• A single labeled block can be referred by more than one goto
statements.
• The goto statement is denoted by the goto keyword
HOW TO RESTART C#
PROGRAM WITH USER
INPUT

You might also like