TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# continue Keyword

Apply the continue keyword in a loop to end the current loop iteration.
Continue. This statement alters control flow. It is used in loop bodies. It allows you to skip the execution of the rest of the iteration.
Control flow. With the continue statement, we jump immediately to the next iteration in the loop. This keyword is often useful in while-loops, but can be used in any loop.Keywords
An example. This program uses the continue statement. In a while-true loop, the loop continues infinitely. We Sleep() to make the program easier to watch as it executes.WhileSleep

Part 1: A random number is acquired on each iteration through the loop, using the Next method on the Random type.

Part 2: The modulo division operator is applied to test for divisibility by 2 and 3.

RandomModulo

And: If the number is evenly divisible, the rest of the iteration is aborted. The loop restarts.

C# program that uses continue keyword using System; using System.Threading; class Program { static void Main() { Random random = new Random(); while (true) { // Part 1: get a random number. int value = random.Next(); // Part 2: if number is divisible by 2, skip the rest of the iteration. if ((value % 2) == 0) { continue; } // If number is divisible by three, skip the rest of the iteration. if ((value % 3) == 0) { continue; } Console.WriteLine("Not divisible by 2 or 3: {0}", value); // Pause. Thread.Sleep(100); } } } Output Not divisible by 2 or 3: 710081881 Not divisible by 2 or 3: 1155441983 Not divisible by 2 or 3: 1558706543 Not divisible by 2 or 3: 1531461115 Not divisible by 2 or 3: 64503937 Not divisible by 2 or 3: 498668099 Not divisible by 2 or 3: 85365569 Not divisible by 2 or 3: 184007165 Not divisible by 2 or 3: 1759735855 Not divisible by 2 or 3: 1927432795 Not divisible by 2 or 3: 648758581 Not divisible by 2 or 3: 1131091151 Not divisible by 2 or 3: 1931772589 Not divisible by 2 or 3: 283344547 Not divisible by 2 or 3: 1727688571 Not divisible by 2 or 3: 64235879 Not divisible by 2 or 3: 818135261...
No enclosing loop. We must use a continue statement within a loop. We get an error from the compiler if no enclosing loop is found. The same restriction applies to break.Break
C# program that causes continue error class Program { static void Main() { continue; } } Output Error CS0139 No enclosing loop out of which to break or continue
Some notes. The C# language is a high-level language. When it is compiled, it is flattened into a sequence of instructions. These are intermediate language opcodes.IL

And: When we specify a continue in a loop, branch statements (which jump to other instructions) are generated.

Some notes, branches. In branch statements, a condition is tested. And based on whether the condition is true, the runtime jumps to another instruction in the sequence.

Offset: This new location is indicated by an offset in the opcode. So all branches "jump" to other parts of an instruction stream.

Note: The continue statement could be implemented by branching to the top of the loop construct if the result of the expression is true.

True, False
A summary. The continue statement exits a single iteration of a loop. It does not terminate the enclosing loop entirely or leave the enclosing function body.
Continue uses. Continue is often most useful in while or do-while loops. For-loops, with well-defined exit conditions, may not benefit as much.Do
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf