CNIT 15501 Lecture Notes - Lecture 8: For Loop, While Loop
Loops in C# Notes
Terminology
● One Cycle – Iteration
● Loop Condition –
○ Controls the loop
● Loop Variable
○ Used in the condition
● Infinite loop
In Programming
● There are 3 main coding structures in high level programming languages:
○ Sequential
○ Selection
○ Repetition
● A computer program is a combination of these three coding structures
Repetition Structure
● A repetition structure is a segment of code that is executed repeatedly
● It is also referred to as loops
● Applications:
○ Compute the annual yield on an investment for 10 years
○ Read every line from the file until the end of the file is reached
While loop
● While is a pretest loop the loop condition is checked at the beginning of each
cycle
● Syntax
○ while ( condition )
○ {
○ ------
○ }
For loop
● For loop is a pretest loop
● For loop is also known as a counting loop.
● Use it when number of iterations is known, ex:
○ Adding numbers 1 to 100.
find more resources at oneclass.com
find more resources at oneclass.com
Document Summary
There are 3 main coding structures in high level programming languages: A computer program is a combination of these three coding structures. A repetition structure is a segment of code that is executed repeatedly. Compute the annual yield on an investment for 10 years. Read every line from the file until the end of the file is reached. While is a pretest loop the loop condition is checked at the beginning of each cycle. For loop is also known as a counting loop. Use it when number of iterations is known, ex: Infinite loop int n = 1; int sum = 0; // n++; is missing ---> infinite loop break; statement. Break; can be used inside a for loop or a while loop. Takes the execution to the statement after the loop. Will skip the rest of the current cycle and moves on to the next cycle.