



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 // Addition.cs 2 // An addition program. 3 4 using System; 5 6 class Addition 7 { 8 static void Main( string[] args ) 9 { 10 string firstNumber, // first string entered by user 11 secondNumber; // second string entered by user 12 13 int number1, // first number to add 14 number2, // second number to add 15 sum; // sum of number1 and number 16 17 // prompt for and read first number from user as string 18 Console.Write( "Please enter the first integer: " ); 19 firstNumber = Console.ReadLine(); 20 21 // read second number from user as string 22 Console.Write( "\nPlease enter the second integer: " ); 23 secondNumber = Console.ReadLine(); 24 25 // convert numbers from type string to type int 26 number1 = Int32.Parse( firstNumber ); 27 number2 = Int32.Parse( secondNumber ); 28 29 // add numbers 30 sum = number1 + number2; 31 32 // display results 33 Console.WriteLine( "\nThe sum is {0}.", sum ); 34 35 } // end method Main 36 37 } // end class Addition
. .
Each of these statements could be:
or more complex statements (to be covered later)
?e 2 :e 3
add grade to total
add 1 to counter
total = total + grade;
counter = counter + 1;
T
F
if structure (single selection)
if/else structure (double selection) F T
switch structure (multiple selections)
.
.
break
break
break
break
T
T
T
F
F
F
.
T
F
while structure
T
F
do/while structure F
T
for structure/foreach structure
Grade >= 60 print āPassedā
true
false
if (studentGrade >= 60)
Console.WriteLine (āPassedā);
// beginning of the next statement
string result;
int numQ;
ā¦ā¦ā¦ā¦
result = (numQ==1)? āQuarterā : āQuartersā;
// beginning of the next statement
true
false
Product <= 1000 Product = 2 * product
int product;
product = 2;
while (product <= 1000)
product = 2 * product;
// beginning of the next statement
true
false
Product <= 1000 Product = 2 * product
int product;
product = 2;
while (product <= 1000)
{
product = 2 * product;
}
// beginning of the next statement
WhileCounter.cs
Program Output
1 // WhileCounter.cs 2 // Counter-controlled repetition. 3 4 using System; 5 6 class WhileCounter 7 { 8 static void Main( string[] args ) 9 { 10 int counter = 1; // initialization 11 12 while ( counter <= 5 ) // repetition condition 13 { 14 Console.WriteLine( counter ); 15 counter++; // increment 16 17 } // end while 18 19 } // end method Main 20 21 } // end class WhileCounter
This is where the counter variable is initialized. It is set to 1.
The loop will continue until counter is greater than five (it will stop once it gets to six)
The counter is incremented and 1 is added to it
Pseudocode algorithm that uses counter-controlled repetition to solve the class-average problem.
Average2.cs Program Output
Enter Integer Grade, -1 to Quit: 97 Enter Integer Grade, -1 to Quit: 88 Enter Integer Grade, -1 to Quit: 72 Enter Integer Grade, -1 to Quit: -
Class average is 85.