SlideShare una empresa de Scribd logo
1 de 22
Valid variable names? Spot the invalid variable names int   2counter; string   $myString; char   Initial; class   else; int   __hwnd;
Valid variable names? int   2counter; Invalid. Names cannot begin with a number. string   $myString; Invalid. Names cannot start with $ char   Initial; Valid class   else; Invalid. “else” is a keyword. int   __hwnd; Valid.
Chapter 6 C# .NET Loops
What will you be learning? For loops While loops Do loops
What is a loop? A loop is a way to execute a piece of code repeatedly  Go round and round until an end condition is met
Why use loops? Eg: you need to add from 0 to 10    Solution not using loop: int answer;      answer = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10;  The above solution not suitable for very large numbers (says from 0 to 10000) – programmers are lazy, what would be a faster way to solve?
Flowchart “for” loop Start with i = 0 End when i is not < 101 Update i by adding 1
for loop for (      )   // start, end, update conditions {     // Processing }
for loop for (inti=0;  i < 101;  i++  )   {     // Processing }
for (inti=0;  i < 101;  i++  )   Write syntax of the following for loops From j = 10 until j = 1000 (inclusive), update j by 1 for each loop From t = 2 until t = 1000 (inclusive), update t by 2 for each loop From m = 5 until m = 0 (inclusive), update m by -1 for each loop
Exercise Part 1 – For Loops Add a new project to “Part 1 For Loops” Put one button and add the following codes for its click method (pg 112):
Exercise Part 1 – For Loops
Exercise Part 2Loop Start Values and Loop End Values  Add a project “Part 2” to the solution
Do it yourself - TimeTable Modify the previous project To add item to the listBox:        listBox1.Items.Add("xxxx") To clear the listBox:        listBox1.Items.Clear( );
break     continue Add new project to SpfChapter6 solution: “Extra – break and continue” Add a button and add codes into its Click method: for (int x=0; x < 11; x++) {    if (x==5) break; MessageBox.Show(“x =”+x); }
break     continue for (int x=0; x < 11; x++) {    if (x==5) continue; MessageBox.Show(“x =”+x); }
while loop while (   ) // End condition  { }
while loop inti = 0; while (i < 101) { // Processing    : i++;  // at the end }
do loop do { } while (   );    // End condition // The difference between the while(){.. } and Do {..} while(); loops is that the code in a Do loop will get executed at least once, because its while part is at the end.
do loop inti = 0; do { // Processing    : i++;  // at the end } while (i < 101);
Part 3 Modify the previous project Change from "for" loop to "do" loop
Summary A loop is a way to execute a piece of code repeatedly  We shall be using loops again when we cover Arrays There is "foreach" loop that applies for string, array and objects

Más contenido relacionado

La actualidad más candente

please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...
hwbloom27
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
jahanullah
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
Jd Mercado
 
Do...until loop structure
Do...until loop structureDo...until loop structure
Do...until loop structure
Jd Mercado
 

La actualidad más candente (20)

C lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshareC lecture 4 nested loops and jumping statements slideshare
C lecture 4 nested loops and jumping statements slideshare
 
please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...please sir i want to comments of every code what i do in eachline . in this w...
please sir i want to comments of every code what i do in eachline . in this w...
 
Nested loops
Nested loopsNested loops
Nested loops
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
Working of while loop
Working of while loopWorking of while loop
Working of while loop
 
Loops
LoopsLoops
Loops
 
For Loops and Nesting in Python
For Loops and Nesting in PythonFor Loops and Nesting in Python
For Loops and Nesting in Python
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
applet.docx
applet.docxapplet.docx
applet.docx
 
04a intro while
04a intro while04a intro while
04a intro while
 
Loop control in c++
Loop control in c++Loop control in c++
Loop control in c++
 
Iterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop workingIterative control structures, looping, types of loops, loop working
Iterative control structures, looping, types of loops, loop working
 
Call by value and call by reference and varaiable length arguments
Call by value and call by reference and  varaiable length argumentsCall by value and call by reference and  varaiable length arguments
Call by value and call by reference and varaiable length arguments
 
Lecture 4
Lecture 4Lecture 4
Lecture 4
 
for loop in java
for loop in java for loop in java
for loop in java
 
Loops in R
Loops in RLoops in R
Loops in R
 
Implement ERC20 on TestRPC
Implement ERC20 on TestRPCImplement ERC20 on TestRPC
Implement ERC20 on TestRPC
 
Looping Statements and Control Statements in Python
Looping Statements and Control Statements in PythonLooping Statements and Control Statements in Python
Looping Statements and Control Statements in Python
 
Do...while loop structure
Do...while loop structureDo...while loop structure
Do...while loop structure
 
Do...until loop structure
Do...until loop structureDo...until loop structure
Do...until loop structure
 

Similar a C# Loops

Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
IIUM
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
humphrieskalyn
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
monicafrancis71118
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
bluejayjunior
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
Vince Vo
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
Mohamed Ahmed
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
amrit47
 

Similar a C# Loops (20)

Csc1100 lecture05 ch05
Csc1100 lecture05 ch05Csc1100 lecture05 ch05
Csc1100 lecture05 ch05
 
C++ Loops General Discussion of Loops A loop is a.docx
C++ Loops  General Discussion of Loops A loop is a.docxC++ Loops  General Discussion of Loops A loop is a.docx
C++ Loops General Discussion of Loops A loop is a.docx
 
C++ loop
C++ loop C++ loop
C++ loop
 
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docxCMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
CMIS 102 Hands-On Lab Week 4OverviewThis hands-on lab all.docx
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Python programing
Python programingPython programing
Python programing
 
pythonQuick.pdf
pythonQuick.pdfpythonQuick.pdf
pythonQuick.pdf
 
python notes.pdf
python notes.pdfpython notes.pdf
python notes.pdf
 
python 34💭.pdf
python 34💭.pdfpython 34💭.pdf
python 34💭.pdf
 
C++ Course - Lesson 2
C++ Course - Lesson 2C++ Course - Lesson 2
C++ Course - Lesson 2
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)Python Training in Chandigarh(Mohali)
Python Training in Chandigarh(Mohali)
 
Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)Python Training Course in Chandigarh(Mohali)
Python Training Course in Chandigarh(Mohali)
 
Lecture 2
Lecture 2Lecture 2
Lecture 2
 
lecture 2.pptx
lecture 2.pptxlecture 2.pptx
lecture 2.pptx
 
Ecs 10 programming assignment 4 loopapalooza
Ecs 10 programming assignment 4   loopapaloozaEcs 10 programming assignment 4   loopapalooza
Ecs 10 programming assignment 4 loopapalooza
 
06.Loops
06.Loops06.Loops
06.Loops
 
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docxPROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
PROVIDE COMMENTS TO FELLOW STUDENTS ANSWERS AND PLEASE DON’T SAY G.docx
 
03b loops
03b   loops03b   loops
03b loops
 

C# Loops

  • 1. Valid variable names? Spot the invalid variable names int 2counter; string $myString; char Initial; class else; int __hwnd;
  • 2. Valid variable names? int 2counter; Invalid. Names cannot begin with a number. string $myString; Invalid. Names cannot start with $ char Initial; Valid class else; Invalid. “else” is a keyword. int __hwnd; Valid.
  • 3. Chapter 6 C# .NET Loops
  • 4. What will you be learning? For loops While loops Do loops
  • 5. What is a loop? A loop is a way to execute a piece of code repeatedly Go round and round until an end condition is met
  • 6. Why use loops? Eg: you need to add from 0 to 10 Solution not using loop: int answer; answer = 0 + 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 10; The above solution not suitable for very large numbers (says from 0 to 10000) – programmers are lazy, what would be a faster way to solve?
  • 7. Flowchart “for” loop Start with i = 0 End when i is not < 101 Update i by adding 1
  • 8. for loop for ( ) // start, end, update conditions { // Processing }
  • 9. for loop for (inti=0; i < 101; i++ ) { // Processing }
  • 10. for (inti=0; i < 101; i++ ) Write syntax of the following for loops From j = 10 until j = 1000 (inclusive), update j by 1 for each loop From t = 2 until t = 1000 (inclusive), update t by 2 for each loop From m = 5 until m = 0 (inclusive), update m by -1 for each loop
  • 11. Exercise Part 1 – For Loops Add a new project to “Part 1 For Loops” Put one button and add the following codes for its click method (pg 112):
  • 12. Exercise Part 1 – For Loops
  • 13. Exercise Part 2Loop Start Values and Loop End Values Add a project “Part 2” to the solution
  • 14. Do it yourself - TimeTable Modify the previous project To add item to the listBox: listBox1.Items.Add("xxxx") To clear the listBox: listBox1.Items.Clear( );
  • 15. break continue Add new project to SpfChapter6 solution: “Extra – break and continue” Add a button and add codes into its Click method: for (int x=0; x < 11; x++) { if (x==5) break; MessageBox.Show(“x =”+x); }
  • 16. break continue for (int x=0; x < 11; x++) { if (x==5) continue; MessageBox.Show(“x =”+x); }
  • 17. while loop while ( ) // End condition { }
  • 18. while loop inti = 0; while (i < 101) { // Processing : i++; // at the end }
  • 19. do loop do { } while ( ); // End condition // The difference between the while(){.. } and Do {..} while(); loops is that the code in a Do loop will get executed at least once, because its while part is at the end.
  • 20. do loop inti = 0; do { // Processing : i++; // at the end } while (i < 101);
  • 21. Part 3 Modify the previous project Change from "for" loop to "do" loop
  • 22. Summary A loop is a way to execute a piece of code repeatedly We shall be using loops again when we cover Arrays There is "foreach" loop that applies for string, array and objects