SlideShare uma empresa Scribd logo
1 de 16
Core Programming นายสมเกียรติ สอนนวล Cimatt Business Group Co.,LTD.
Core Programming Understand computerstorage and data types Understand computerdecision structures Identify the appropriatemethod for handlingrepetition Understand errorhandling
Identify the appropriatemethod for handlingrepetition Lesson Overview Students will identify the appropriate method for handling repetition. In this lesson, you will learn: for loops while loops do..while loops Recursion
Identify the appropriatemethod for handlingrepetition Iterations in Real Life  An iteration is the act of repeating a set of steps to perform a task. 		For example: Turn the screwdriver until the screw is tight. Rub my hands under the air dryer until they are dry. Iterations are modeled in computers. 		For example: (C#) for(int i = 0; i < 10; i++) Console.WriteLine(“I repeat ten times”);
Identify the appropriatemethod for handlingrepetition The while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated before the code is executed. Can be used when the number of iterations is not known before executing the loop. inti = 0;			// Initialization while (i < 5)		// Condition { Console.WriteLine(i); i = i + 1;			// Increment }
Identify the appropriatemethod for handlingrepetition The for Loop Allows code to be repeated using a loop counter to control how many times it repeats. Used when the number of iterations is known before executing the loop. Initialization ConditionIncrement for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
Identify the appropriatemethod for handlingrepetition while Loop vs. for Loop string line = "default"; while (line != "") { Console.Write("Enter a word (while):"); 	line = Console.ReadLine(); } Console.WriteLine(); for (int i = 0; i < 5; i++) { Console.Write("Enter a word (for) :"); line = Console.ReadLine(); }
Identify the appropriatemethod for handlingrepetition The do-while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated after the code is already executed once. Can be used when the number of iterations is not known before string line = “default”; do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null);
Identify the appropriatemethod for handlingrepetition while Loop vs. do-while Loop A do-while loop will execute at least once. A while loop might not execute at all. Console.WriteLine(“Enter a word:”); String line = Console.ReadLine(); while (line != null) { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null)
Identify the appropriatemethod for handlingrepetition Counting from 1 to 10 with Different Loops for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } inti = 1; while (i <= 10) { Console.WriteLine(i); i++; } inti = 1; do { Console.WriteLine(i); i++; } while (i <= 10);
Identify the appropriatemethod for handlingrepetition Scope Errors for(int i = 0; i < 5; i++) { Console.Writeline(i); } Console.WriteLine(i);  // syntax error // i is a local variable
Identify the appropriatemethod for handlingrepetition Recursion Recursion occurs when a method calls itself to solve another version of the same problem. With each recursive call, the problem becomes simpler and moves toward a base case. The base case is reached when no other recursive call is required. A base case is the point when no other recursive calls are made.
Identify the appropriatemethod for handlingrepetition Factorials public int fact(int n) { if (n == 1) 	return 1; else 	return n * fact(n - 1); } fact (4) 		24 fact (4) 4 * fact (3) 		6 fact (3) 3 * fact (2) 		2 fact (2) 2 * fact (1) 		1 fact (1)
Identify the appropriatemethod for handlingrepetition public int identity(int num) { if(num < 1) 	return 10; else 	return num + identity(num - 2); }
Assignment 1. Transform the following while loop into a for loop. int num = 1; while (num < 20) { Console.WriteLine(num); num = num + 1; } 2. Transform the following for loop into a while loop. for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 3. Transform the following while loop into a for loop int num = 10; while (num >= 0) { Console.WriteLine(num); num = num - 1; } 4. Whatoutput is produced by the following code? for (int x = 0; x < 3; x++) Console.WriteLine(x*3); 5.  How many lines of output will the following code produce? for (int x = 0; x < 13; x=x+2) Console.WriteLine(“line”);
Answer 1. 	for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 2.	int num = 1; 		while (num < 10) 		{ Console.WriteLine(num); 			num = num + 1; 		} 3.	 for (int num = 10; num >= 0; num = num-1) Console.WriteLine(num); 4.	0 		3 		6 5.	7

Mais conteúdo relacionado

Mais procurados

Mais procurados (20)

Semaphores OS Basics
Semaphores OS BasicsSemaphores OS Basics
Semaphores OS Basics
 
Producer consumer
Producer consumerProducer consumer
Producer consumer
 
DAA Notes.pdf
DAA Notes.pdfDAA Notes.pdf
DAA Notes.pdf
 
Computer architecture instruction formats
Computer architecture instruction formatsComputer architecture instruction formats
Computer architecture instruction formats
 
Multithreading computer architecture
 Multithreading computer architecture  Multithreading computer architecture
Multithreading computer architecture
 
Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?Actor Model and C++: what, why and how?
Actor Model and C++: what, why and how?
 
Operating system Dead lock
Operating system Dead lockOperating system Dead lock
Operating system Dead lock
 
Pipelining
PipeliningPipelining
Pipelining
 
Memory Reference Instructions
Memory Reference InstructionsMemory Reference Instructions
Memory Reference Instructions
 
What is Cache and how it works
What is Cache and how it worksWhat is Cache and how it works
What is Cache and how it works
 
Interrupt 8085
Interrupt 8085Interrupt 8085
Interrupt 8085
 
Risc cisc Difference
Risc cisc DifferenceRisc cisc Difference
Risc cisc Difference
 
Unit 5 Advanced Computer Architecture
Unit 5 Advanced Computer ArchitectureUnit 5 Advanced Computer Architecture
Unit 5 Advanced Computer Architecture
 
Distributed systems scheduling
Distributed systems schedulingDistributed systems scheduling
Distributed systems scheduling
 
Consistency in NoSQL
Consistency in NoSQLConsistency in NoSQL
Consistency in NoSQL
 
Pipelining slides
Pipelining slides Pipelining slides
Pipelining slides
 
Semaphores
SemaphoresSemaphores
Semaphores
 
Topic 1 Data Representation
Topic 1 Data RepresentationTopic 1 Data Representation
Topic 1 Data Representation
 
Unit vi
Unit viUnit vi
Unit vi
 
design of accumlator
design of accumlatordesign of accumlator
design of accumlator
 

Destaque

1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]tototo147
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]tototo147
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development FundamentalsChris Farrell
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressionsvinay arora
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programmingManoj Tyagi
 

Destaque (9)

1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]1.2 core programming [understand computer decision structures]
1.2 core programming [understand computer decision structures]
 
1.4 core programming [understand error handling]
1.4 core programming [understand error handling]1.4 core programming [understand error handling]
1.4 core programming [understand error handling]
 
CPP03 - Repetition
CPP03 - RepetitionCPP03 - Repetition
CPP03 - Repetition
 
Software Development Fundamentals 1
Software Development Fundamentals 1Software Development Fundamentals 1
Software Development Fundamentals 1
 
Software Development Fundamentals
Software Development FundamentalsSoftware Development Fundamentals
Software Development Fundamentals
 
Presentation1
Presentation1Presentation1
Presentation1
 
Programming loop
Programming loopProgramming loop
Programming loop
 
C Prog. - Operators and Expressions
C Prog. - Operators and ExpressionsC Prog. - Operators and Expressions
C Prog. - Operators and Expressions
 
Operator in c programming
Operator in c programmingOperator in c programming
Operator in c programming
 

Semelhante a 1.3 core programming [identify the appropriate method for handling repetition]

C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6Vince Vo
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languagetanmaymodi4
 
Loops in c language
Loops in c languageLoops in c language
Loops in c languageTanmay Modi
 
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.docxhumphrieskalyn
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio Zoppi
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statementsSaad Sheikh
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02CIMAP
 
Control structures ii
Control structures ii Control structures ii
Control structures ii Ahmad Idrees
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++msharshitha03s
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1Mohamed Ahmed
 

Semelhante a 1.3 core programming [identify the appropriate method for handling repetition] (20)

Loops
LoopsLoops
Loops
 
06 Loops
06 Loops06 Loops
06 Loops
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
06.Loops
06.Loops06.Loops
06.Loops
 
Loops (1)
Loops (1)Loops (1)
Loops (1)
 
CPP Homework Help
CPP Homework HelpCPP Homework Help
CPP Homework Help
 
Java căn bản - Chapter6
Java căn bản - Chapter6Java căn bản - Chapter6
Java căn bản - Chapter6
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Loops in c language
Loops in c languageLoops in c language
Loops in c language
 
Thread
ThreadThread
Thread
 
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
 
Giorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrencyGiorgio zoppi cpp11concurrency
Giorgio zoppi cpp11concurrency
 
Loops and conditional statements
Loops and conditional statementsLoops and conditional statements
Loops and conditional statements
 
Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02Claguage 110226222227-phpapp02
Claguage 110226222227-phpapp02
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
while loop in C.pptx
while loop in C.pptxwhile loop in C.pptx
while loop in C.pptx
 
Object oriented programming system with C++
Object oriented programming system with C++Object oriented programming system with C++
Object oriented programming system with C++
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
C++ Course - Lesson 1
C++ Course - Lesson 1C++ Course - Lesson 1
C++ Course - Lesson 1
 
130707833146508191
130707833146508191130707833146508191
130707833146508191
 

Último

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin ClassesCeline George
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxcallscotland1987
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdfssuserdda66b
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxDr. Sarita Anand
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfNirmal Dwivedi
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 

Último (20)

TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdfVishram Singh - Textbook of Anatomy  Upper Limb and Thorax.. Volume 1 (1).pdf
Vishram Singh - Textbook of Anatomy Upper Limb and Thorax.. Volume 1 (1).pdf
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Google Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptxGoogle Gemini An AI Revolution in Education.pptx
Google Gemini An AI Revolution in Education.pptx
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdfUGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
UGC NET Paper 1 Mathematical Reasoning & Aptitude.pdf
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 

1.3 core programming [identify the appropriate method for handling repetition]

  • 1. Core Programming นายสมเกียรติ สอนนวล Cimatt Business Group Co.,LTD.
  • 2. Core Programming Understand computerstorage and data types Understand computerdecision structures Identify the appropriatemethod for handlingrepetition Understand errorhandling
  • 3. Identify the appropriatemethod for handlingrepetition Lesson Overview Students will identify the appropriate method for handling repetition. In this lesson, you will learn: for loops while loops do..while loops Recursion
  • 4. Identify the appropriatemethod for handlingrepetition Iterations in Real Life An iteration is the act of repeating a set of steps to perform a task. For example: Turn the screwdriver until the screw is tight. Rub my hands under the air dryer until they are dry. Iterations are modeled in computers. For example: (C#) for(int i = 0; i < 10; i++) Console.WriteLine(“I repeat ten times”);
  • 5. Identify the appropriatemethod for handlingrepetition The while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated before the code is executed. Can be used when the number of iterations is not known before executing the loop. inti = 0; // Initialization while (i < 5) // Condition { Console.WriteLine(i); i = i + 1; // Increment }
  • 6. Identify the appropriatemethod for handlingrepetition The for Loop Allows code to be repeated using a loop counter to control how many times it repeats. Used when the number of iterations is known before executing the loop. Initialization ConditionIncrement for (int i = 0; i < 5; i++) { Console.WriteLine(i); }
  • 7. Identify the appropriatemethod for handlingrepetition while Loop vs. for Loop string line = "default"; while (line != "") { Console.Write("Enter a word (while):"); line = Console.ReadLine(); } Console.WriteLine(); for (int i = 0; i < 5; i++) { Console.Write("Enter a word (for) :"); line = Console.ReadLine(); }
  • 8. Identify the appropriatemethod for handlingrepetition The do-while Loop Allows code to be repeated so long as a Boolean condition is met. The condition is evaluated after the code is already executed once. Can be used when the number of iterations is not known before string line = “default”; do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null);
  • 9. Identify the appropriatemethod for handlingrepetition while Loop vs. do-while Loop A do-while loop will execute at least once. A while loop might not execute at all. Console.WriteLine(“Enter a word:”); String line = Console.ReadLine(); while (line != null) { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } do { Console.WriteLine(“Enter a word:”); line = Console.ReadLine(); } while (line != null)
  • 10. Identify the appropriatemethod for handlingrepetition Counting from 1 to 10 with Different Loops for (int i = 1; i <= 10; i++) { Console.WriteLine(i); } inti = 1; while (i <= 10) { Console.WriteLine(i); i++; } inti = 1; do { Console.WriteLine(i); i++; } while (i <= 10);
  • 11. Identify the appropriatemethod for handlingrepetition Scope Errors for(int i = 0; i < 5; i++) { Console.Writeline(i); } Console.WriteLine(i); // syntax error // i is a local variable
  • 12. Identify the appropriatemethod for handlingrepetition Recursion Recursion occurs when a method calls itself to solve another version of the same problem. With each recursive call, the problem becomes simpler and moves toward a base case. The base case is reached when no other recursive call is required. A base case is the point when no other recursive calls are made.
  • 13. Identify the appropriatemethod for handlingrepetition Factorials public int fact(int n) { if (n == 1) return 1; else return n * fact(n - 1); } fact (4) 24 fact (4) 4 * fact (3) 6 fact (3) 3 * fact (2) 2 fact (2) 2 * fact (1) 1 fact (1)
  • 14. Identify the appropriatemethod for handlingrepetition public int identity(int num) { if(num < 1) return 10; else return num + identity(num - 2); }
  • 15. Assignment 1. Transform the following while loop into a for loop. int num = 1; while (num < 20) { Console.WriteLine(num); num = num + 1; } 2. Transform the following for loop into a while loop. for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 3. Transform the following while loop into a for loop int num = 10; while (num >= 0) { Console.WriteLine(num); num = num - 1; } 4. Whatoutput is produced by the following code? for (int x = 0; x < 3; x++) Console.WriteLine(x*3); 5. How many lines of output will the following code produce? for (int x = 0; x < 13; x=x+2) Console.WriteLine(“line”);
  • 16. Answer 1. for (int num = 1; num < 20; num = num+1) Console.WriteLine(num); 2. int num = 1; while (num < 10) { Console.WriteLine(num); num = num + 1; } 3. for (int num = 10; num >= 0; num = num-1) Console.WriteLine(num); 4. 0 3 6 5. 7