Learning Objective: Student will be able to use comments without changing the program

Line comments only last for a line. They are usually used to make a simple clarification. For example

// This Constant variable holds the total number of classrooms Const Integer TOTAL_CLASSROOMS 

As you can see the comment is only one line long and it is short and simple. Block comments on the other hand occupy multiple lines and are usually used to expand or explain in a bit more detail the functionality of a piece of code. The symbol for block commenting varies from language to language but in this book the symbol we will use is “/*....*/” For example let’s take a look at a refined revision of the earlier program.

Program

/*This program accepts the number of students in a class from the user and uses that input to calculate the sum and average and then displays it to the user.*/ Integer Class1, Class2, Class3 // stores class size Integer average // stores the average Integer sum // stores the sum Const Integer TOTAL_CLASSROOMS // stores number of classrooms // Prompt the user Display “Enter the number of students in class 1” Class1 = Input Display “Enter the number of students in class 2” Class2 = Input Display “Enter the number of students in class 3” Class3 = Input Sum = Class1 + Class2 + Class3 Average = sum/TOTOAL_CLASSROOMS Display “The sum is “, sum, “ and the average is ”, average 

Output

 Enter the number of students in class 1 → input: 15 Enter the number of students in class 2 → input: 20 Enter the number of students in class 3 → input: 19 The sum is 54 and the average is 18

In C++, to write a line of comment we use //. And for a block comment we use /* code */“

Activity2.9.1.Activity Coding Exercise.

Save and Run the below program.