Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.

Similar presentations


Presentation on theme: " 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming."— Presentation transcript:

1  2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming

2  2003 Prentice Hall, Inc. All rights reserved. 2 Basic C++ Programming Programs –Built from pieces (e.g., functions) C++ standard library –Rich collections of existing pieces (e.g., functions) “Building block approach” to creating programs –“Software reuse”

3  2003 Prentice Hall, Inc. All rights reserved. 3 A Simple Program: Printing a Line of Text Comments –Document programs –Improve program readability –Ignored by compiler –Single-line comment Begin with // Preprocessor directives –Processed before compiling –Begin with #

4  2003 Prentice Hall, Inc. All rights reserved. 4 // prog1.cpp // A first program in C++. #include // function main begins program execution int main() { cout << "Welcome to C++!\n"; return 0; // indicate that program ended successfully } // end function main Welcome to C++! commentsPreprocessor directive to include pieces (e.g., functions) from the iostream library Function main appears exactly once in every C++ program.. Function main returns an integer value. Left brace { begins function body. Corresponding right brace } ends function body. Statements end with a semicolon ;. Keyword return is one of several means to exit function; value 0 indicates program terminated successfully.

5  2003 Prentice Hall, Inc. All rights reserved. 5 Memory Concepts Variables –Location in memory where value can be stored –Common data types int - integer numbers char - characters double - decimal numbers –Declare variables with name and data type before use int integer1; int integer2; int sum;

6  2003 Prentice Hall, Inc. All rights reserved. 6 Memory Concepts cin >> integer1; –Assume user entered 45 cin >> integer2; –Assume user entered 72 sum = integer1 + integer2; integer1 45 integer1 45 integer2 72 integer1 45 integer2 72 sum 117

7  2003 Prentice Hall, Inc. All rights reserved. 7 // prog2.cpp // Addition program. #include // function main begins program execution int main() { int integer1; // first number to be input by user int integer2; // second number to be input by user int sum; // variable in which sum will be stored cout << "Enter first integer\n"; // prompt cin >> integer1; // read an integer cout << "Enter second integer\n"; // prompt cin >> integer2; // read an integer sum = integer1 + integer2; // assign result to sum cout << "Sum is " << sum << "\n" // print sum return 0; // indicate that program ended successfully } // end function main Declare integer variables.obtain user input.

8  2003 Prentice Hall, Inc. All rights reserved. 8 Enter first integer 45 Enter second integer 72 Sum is 117

9  2003 Prentice Hall, Inc. All rights reserved. 9 Control Structures Sequential execution –Statements executed in order Transfer of control –Next statement executed not next one in sequence 3 control structures –Sequence structure Programs executed sequentially by default –Selection structures if, if/else, switch –Repetition structures while, do/while, for

10  2003 Prentice Hall, Inc. All rights reserved. 10 if Selection Structure if structure –Make decision based on truth or falsity of condition If condition met, body executed Else, body not executed

11  2003 Prentice Hall, Inc. All rights reserved. 11 if Selection Structure Selection structure –Choose among alternative courses of action –Pseudocode example: If student’s grade is greater than or equal to 60 Print “Passed” –If the condition is true Print statement executed, program continues to next statement –If the condition is false Print statement ignored, program continues

12  2003 Prentice Hall, Inc. All rights reserved. 12 if Selection Structure Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed";

13  2003 Prentice Hall, Inc. All rights reserved. 13 if Selection Structure Flowchart of pseudocode statement true false grade >= 60 print “Passed”

14  2003 Prentice Hall, Inc. All rights reserved. 14 // prog3.cpp // Using if statements, relational // operators, and equality operators. #include // function main begins program execution int main() { int num1; // first number to be read from user int num2; // second number to be read from user cout << "Enter two integers, and I will tell you\n" << "the relationships they satisfy: "; cin >> num1 >> num2; // read two integers if ( num1 == num2 ) cout << num1 << " is equal to " << num2 << "\n"; if ( num1 != num2 ) cout << num1 << " is not equal to " << num2 << "\n"; Declare variables. if structure compares values of num1 and num2 to test for equality. If condition is true (i.e., values are equal), execute this statement. if structure compares values of num1 and num2 to test for inequality. If condition is true (i.e., values are not equal), execute this statement.

15  2003 Prentice Hall, Inc. All rights reserved. 15 if ( num1 < num2 ) cout << num1 << " is less than " << num2 << "\n"; if ( num1 > num2 ) cout << num1 << " is greater than " << num2 << "\n"; if ( num1 <= num2 ) cout << num1 << " is less than or equal to " << num2 << "\n"; if ( num1 >= num2 ) cout << num1 << " is greater than or equal to " << num2 << "\n"; return 0; // indicate that program ended successfully } // end function main Enter two integers, and I will tell you the relationships they satisfy: 22 12 22 is not equal to 12 22 is greater than 12 22 is greater than or equal to 12

16  2003 Prentice Hall, Inc. All rights reserved. 16 Enter two integers, and I will tell you the relationships they satisfy: 7 7 7 is equal to 7 7 is less than or equal to 7 7 is greater than or equal to 7

17  2003 Prentice Hall, Inc. All rights reserved. 17 if/else Selection Structure if –Performs action if condition true if/else –Different actions if conditions true or false Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” C++ code if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";


Download ppt " 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming."

Similar presentations


Ads by Google