CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of...

18
CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By: Asma Alosaimi Edited By: Rabab Almobty, Fatimah Alakeel, Noor Alhareqi

Transcript of CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of...

Page 1: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

CHAPTER 2PART #1C++ PROGRAM STRUCTURE

1st semester 1433-1434 H

1King Saud University College of Applied studies and Community ServiceCsc 1101By: Asma AlosaimiEdited By: Rabab Almobty, Fatimah Alakeel, Noor Alhareqi

Page 2: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Outline

C++ Program structure First Program in C++: Printing a Line of Text

Comments Preprocessor Directive Main Output Statement Exit

Printing a Single Line of Text with Multiple Statements

Printing Multiple Lines of Text with a Single Statement

C ++ program compilation

2

Page 3: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

C++ Program StructurePage 3

#include <iostream> // Preprocessor Commands

int main( ) // main function

{

// Declaration section – Declare needed variables

…...

// Input section – Enter required data

…..

// Processing section – Processing Statements

…...

// Output section – Display expected results

…....

return 0;

}// end main

Page 4: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

First Program in C++: Printing a Line of Text

4

// Preprocessor Commands

#include <iostream>

int main( ) // main function

{

// Output section

std::cout << “Assalamo Alaikom \n"; // Display a message

return 0;

}// end mainAssalamo Alaikom The output

A:Comments

B:Preprocessor directives

C:Main

D:Output statement

E:Exit

Page 5: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Comments are used to describe what your code does and aid reading your code.

C++ compiler ignores them. Comments are made using

// for single line comment, which comments to the end of the line.

or /* */ for multi line comment, everything inside of it is considered a comment . The comment begins after the first /*. It ends just before the first */.

Examples:/* This comment begins at this line.This line is included in this commentIt ends at this line. */

// This comment starts here and ends at the end of this line

5

A - Comments

Page 6: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

B – Preprocessor Directive

#include < iostream >

is a directive to the C++ preprocessor.

Lines beginning with # are processed by the preprocessor before the program is compiled.

#include < iostream > tells the preprocessor to include the contents of the input/output stream header (< iostream >) in the program.

This header must be included for any program that outputs data to the screen or inputs data from the key board using C++’s stream input/output.

6

Page 7: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

C – main function

int main()

It is a part of every C++ program.

The parentheses after main indicate that main is a program building block called a function.

C++ programs contain one or more functions, one of which must be main.

Every program in C++ begins executing at the function main.

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.

The left brace ,{ , must begin the body of every function . A corresponding right brace ,} , must end each function’s body.

7

Page 8: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

D – Output Statement

std::cout<<”Assalamo Alaikom \n”;

This instructs the computer to perform an action, namely to print on the screen the string of characters marked by the quotation marks.

The entire line, including std::cout, the<<operator, the string “Assalamo Alaikom \n“ and the semicolon(;), is called a statement.

The statement is a specification of an action to be taken by the computer as the program executes.

Each statement in C++ needs to be terminated with semicolon (;)

8

Page 9: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

D – Output Statement

Preprocessor directives (like #include) do not end with a semicolon.

The << operator is referred to as the stream insertion operator. When this program executes, the value to the operator’s right, the right operand, is inserted in the output stream.

The right operand’s characters normally print exactly as they appear between the double quotes. However, the characters \n are not printed on the screen. The backslash (\) is called an escape character. It indicates that a “special” character is to be output.

9

Page 10: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

D – Output Statement

When a backslash is encountered in a string of characters, the next character is combined with the backslash to form an escape sequence. The escape sequence \n means new line. It causes the cursor (i.e., the current screen position indicator) to move to the beginning of the next line on the screen.

10

Page 11: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

D – Output Statement

The std:: before cout is required when we use names that we’ve brought into the program by the preprocessor directive #include<iostream>.

The notation std::cout specifies that we are using a name, in this case cout, that belongs to “namespace” std.

We can avoid writing the std:: by writing using namespace std; as shown in following.

11

Page 12: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Printing a Line of Text12

// Preprocessor Commands

#include <iostream>

using namespace std;

int main( ) // main function

{

// Output section

cout << “Assalamo Alaikom \n"; // Display a message

return 0;

}// end main Assalamo Alaikom The output

Page 13: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

E – Exit

return 0;

is included at the end of every main function.

The keyword return is one of several means we’ll use to exit a function.

When the return statement is used at the end of main as shown here, the value 0 indicates that the program has terminated successfully.

13

Page 14: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Printing a Single Line of Text with Multiple Statements

14

// Preprocessor Commands

#include <iostream>

using namespace std;

int main( ) // main function

{

// Output section

cout << “Assalamo “

cout << “Alaikom \n"; // Display a message

return 0;

}// end main Assalamo Alaikom The output

Page 15: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Printing Multiple Lines of Text with a Single Statement

15

// Preprocessor Commands

#include <iostream>

using namespace std;

int main( ) // main function

{

// Output section

cout << “Assalamo \n Alaikom \n"; // Display a message

return 0;

}// end mainAssalamoAlaikom

The output

Page 16: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

C++ Program Compilation16

Page 17: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

Steps to Write, Compile, and Run a Program

Editor Source code or file (.cpp)

Compile file Object code or file (.obj)

Linker Executable code or file (.exe)

Loader Executable code is loaded into memory ready for execution

17

Page 18: CHAPTER 2 PART #1 C++ PROGRAM STRUCTURE 1 st semester 1433-1434 H 1 King Saud University College of Applied studies and Community Service Csc 1101 By:

18Reference : C++ How to Program (8th Edition) , By: Deitel& Deitel