CCS102_Fall 2024-2025_Lec2.pdf

Full Transcript

Faculty of Information Technology Fall 2024-2025 Introduction to Computer Programming CS 102 Lecture (2) 1 Outline First Program in C++: Printing a Line of Text Modifying Our First C++ Program Another C++ Program: Adding In...

Faculty of Information Technology Fall 2024-2025 Introduction to Computer Programming CS 102 Lecture (2) 1 Outline First Program in C++: Printing a Line of Text Modifying Our First C++ Program Another C++ Program: Adding Integers Memory Concepts Arithmetic 2 Outline First Program in C++: Printing a Line of Text Modifying Our First C++ Program Another C++ Program: Adding Integers Memory Concepts Arithmetic 3 First Program in C++: Printing a Line of Text Consider a simple program that prints a line of text (Fig. 2.1). This program illustrates several important features of the C++ language. The text in lines 1-11 is the program’s source code (or code). The line numbers are not part of the source code. 4 First Program in C++: Printing a Line of Text (Cont.) Comments “Lines 1 and 2”:- – Each begin with //, indicting that the reminder of each line is a comment. You insert comments to document your programs and to help other people read and understand them. Comments do not cause the computer to perform any action when the program is run—they’re ignored by the C++ compiler and do not cause any machine-language object code to be generated. The comment Text-printing program describes the purpose of the program. 5 First Program in C++: Printing a Line of Text (Cont.) Comments “Lines 1 and 2”:- – A comment beginning with // is called a single-line comment because it terminates at the end of the current line. – You also may use comments containing one or more lines enclosed in. 6 First Program in C++: Printing a Line of Text (Cont.) #include Preprocessor Directive “Line 3”:- – A preprocessing directive is a message to the C++ preprocessor. – Lines that begin with # are processed by the preprocessor before the program is compiled. – This line notifies the preprocessor to include in the program the contents of the input/output stream header file. This header is a file containing information used by the compiler when compiling any program that outputs data to the screen or inputs data from the keyboard using C++-style stream input/output. 7 First Program in C++: Printing a Line of Text (Cont.) The main Function “Line 6”:- – main is a part of every C++ program. – The parentheses after main indicate that main is a program building block called a function. – C++ programs typically consist of one or more functions and classes. – Exactly one function in every program must be named main. Figure 2.1 contains only one function. 8 First Program in C++: Printing a Line of Text (Cont.) The main Function “Line 6”:- – C++ programs begin executing at function main, even if main is not the first function defined in the program. – The keyword int to the left of main indicates that main “returns” an integer (whole number) value. A keyword is a word in code that is reserved by C++ for a specific use. For now, simply include the keyword int to the left of main in each of your programs. – A left brace, {, (line 7) must begin the body of every function. A corresponding right brace, }, (line 11) must end each function’s body. 9 First Program in C++: Printing a Line of Text (Cont.) An Output Statement “Line 8”:- – The entire line 8, including std::cout, the , to obtain a value from the keyboard. – Using the stream extraction operator with std::cin takes character input from the standard input stream, which is usually the keyboard. 30 Another C++ Program: Adding Integers (Cont.) Obtaining the Second Value from the User “Line 16”:- – A statement prints Enter second integer: followed by a space on the screen, prompting the user to take action. Obtaining the Second Value from the User “Line 17”:- – A statement obtains a value for variable number2 from the user. 31 Another C++ Program: Adding Integers (Cont.) Calculating the Sum of the Values Input by the User “Line 19”:- – The assignment statement in line 19 adds the values of variables number1 and number2 and assigns the result to variable sum using the assignment operator =. Most calculations are performed in assignment statements. – The = operator and the + operator are called binary operators because each has two operands. In the case of the + operator, the two operands are number1 and number2. the case of the preceding = operator, the two operands are sum and the value of the expression number1 and number2. 32 Another C++ Program: Adding Integers (Cont.) Displaying the Result “Line 21”:- – A statement displays the character string Sum is followed by the numerical value of variable sum followed by std::endl—a so-called stream manipulator. – The name endl is an abbreviation for “end line” and belongs to namespace std. – The std::endl stream manipulator outputs a newline, then “flushes the output buffer.” This simply means that, on some systems where outputs accumulate in the machine until there are enough to “make it worthwhile” to display them on the screen, std::endl forces any accumulated outputs to be displayed at that moment. This can be important when the outputs are prompting the user for an action, such as entering data. 33 Another C++ Program: Adding Integers (Cont.) Displaying the Result “Line 21”:- – The preceding statement outputs multiple values of different types. – The stream insertion operator “Knows” how to output each type of data. – Using multiple stream insertion operators (

Use Quizgecko on...
Browser
Browser