



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




1 // Analysis.cs 2 // Analysis of Examination Results. 3 4 using System; 5 6 class Analysis 7 { 8 static void Main( string[] args ) 9 { 10 int passes = 0, // number of passes 11 failures = 0, // number of failures 12 student = 1, // student counter 13 grade; // one exam grade 14 15 // process 10 students; counter-controlled loop 16 while ( student <= 10 ) 17 { 18 Console.Write( "Enter grade (0-100): " ); 19 grade = Int32.Parse( Console.ReadLine() ); 20 21 if ( result >= 60 ) 22 passes = passes + 1; 23 24 else 25 failures = failures + 1; 26 27 student = student + 1; 28 } 29
30 // termination phase 31 Console.WriteLine(); 32 Console.WriteLine( "Passed: " + passes ); 33 Console.WriteLine( "Failed: " + failures ); 34 35 if ( passes > 8 ) 36 Console.WriteLine( "Raise Tuition\n" ); 37 38 } // end of method Main 39 40 } // end of class Analysis
1 // ForCounter.cs 2 // Counter-controlled repetition with the for structure. 3 4 using System; 5 6 class ForCounter 7 { 8 static void Main( string[] args ) 9 { 10 // initialization, repetition condition and incrementing 11 // are all included in the for structure 12 for ( int counter = 1; counter <= 5; counter++ ) 13 Console.WriteLine( counter ); 14 } 15 }
1 // BreakTester.cs 2 // Using the break statement in a for structure. 3 4 using System; 5 6 7 class BreakTester 8 { 9 static void Main( string[] args ) 10 { 11 string output = ""; 12 int count; 13 14 for ( count = 1; count <= 10; count++ ) 15 { 16 if ( count == 3 ) 17 break; // skip remaining code in loop 18 // if count == 3 19 20 output += count + " "; 21 22 } // end for loop 23 24 output += "\nBroke out of loop at count = " + count; 25 26 Console.WriteLine( output ); 28 29 } // end method Main 30 31 } // end class BreakTester
1 // ContinueTester.cs 2 // Using the continue statement in a for structure. 3 4 using System; 5 6 7 class ContinueTester 8 { 9 static void Main( string[] args ) 10 { 11 string output = ""; 12 13 for ( int count = 1; count <= 10; count++ ) 14 { 15 if ( count == 5 ) 16 continue; // skip remaining code in loop 17 // only if count == 5 18 19 output += count + " "; 20 } 21 22 output += "\nUsed continue to skip printing 5"; 23 24 Console.WriteLine( output ); 26 27 } // end method Main 28 29 } // end class ContinueTester