29
C++ C++ SUBMITTED BY-RAJANDEEP KAUR SUBMITTED BY-RAJANDEEP KAUR ROLL NO- 115332 ROLL NO- 115332 BRANCH- CSE (2 BRANCH- CSE (2 nd nd SEM) SEM) SUBMITTED TO- Er. JAGDEEP SINGH SUBMITTED TO- Er. JAGDEEP SINGH MALHI MALHI

C++ rajan

Embed Size (px)

DESCRIPTION

C++

Citation preview

Page 1: C++ rajan

C++C++SUBMITTED BY-RAJANDEEP KAURSUBMITTED BY-RAJANDEEP KAUR

ROLL NO- 115332ROLL NO- 115332

BRANCH- CSE (2BRANCH- CSE (2ndnd SEM) SEM)

SUBMITTED TO- Er. JAGDEEP SINGH SUBMITTED TO- Er. JAGDEEP SINGH MALHIMALHI

Page 2: C++ rajan

The Task of ProgrammingThe Task of Programming

► ProgrammingProgramming a computer involves writing a computer involves writing instructions that enable a computer to carry instructions that enable a computer to carry out a single task or a group of tasksout a single task or a group of tasks

► A computer A computer programming languageprogramming language requires requires learning both vocabulary and syntaxlearning both vocabulary and syntax

► Programmers use many different Programmers use many different programming languages, including BASIC, programming languages, including BASIC, Pascal, COBOL, RPG, and Pascal, COBOL, RPG, and C++C++

► The rules of any language make up its The rules of any language make up its syntaxsyntax► Machine languageMachine language is the language that is the language that

computers can understand; it consists of 1s computers can understand; it consists of 1s and 0sand 0s

1

Page 3: C++ rajan

The Task of ProgrammingThe Task of Programming

►A translator (called either a compiler or A translator (called either a compiler or an interpreter) checks your program an interpreter) checks your program for syntax errorsfor syntax errors

►A A logical errorlogical error occurs when you use a occurs when you use a statement that, although syntactically statement that, although syntactically correct, doesn’t do what you intendedcorrect, doesn’t do what you intended

► You You runrun a program by issuing a a program by issuing a command to execute the program command to execute the program statementsstatements

► You You testtest a program by using sample a program by using sample data to determine whether the data to determine whether the program results are correctprogram results are correct

1

Page 4: C++ rajan

Procedural ProgrammingProcedural Programming

► Procedural programsProcedural programs consist of a series of consist of a series of steps or procedures that take place one after steps or procedures that take place one after the otherthe other

► The programmer determines the exact The programmer determines the exact conditions under which a procedure takes conditions under which a procedure takes place, how often it takes place, and when the place, how often it takes place, and when the program stopsprogram stops

► Programmers write procedural programs in Programmers write procedural programs in many programming languages, such as COBOL, many programming languages, such as COBOL, BASIC, FORTRAN, and RPGBASIC, FORTRAN, and RPG

► You can also write procedural programs in C++ You can also write procedural programs in C++

1

Page 5: C++ rajan

A A main( )main( ) Function in C++ Function in C++

► C++ programs consist of modules called C++ programs consist of modules called functionsfunctions

► Every statement within every C++ Every statement within every C++ program is contained in a functionprogram is contained in a function

► Every function consists of two parts:Every function consists of two parts: A A function headerfunction header is the initial line of code in a is the initial line of code in a

C++ which always has three parts:C++ which always has three parts:► Return type of the functionReturn type of the function► Name of the functionName of the function► Types and names of any variables enclosed in Types and names of any variables enclosed in

parentheses, and which the function receivesparentheses, and which the function receives A A function bodyfunction body

1

Page 6: C++ rajan

Creating a Creating a main( )main( ) Function Function

► Every complete C++ statement ends with a Every complete C++ statement ends with a semicolonsemicolon

► Often several statements must be grouped Often several statements must be grouped together, as when several statements must together, as when several statements must occur in a loopoccur in a loop

► In such a case, the statements have their own In such a case, the statements have their own set of opening and closing braces within the set of opening and closing braces within the main braces, forming a main braces, forming a blockblock

1

Page 7: C++ rajan

Working with VariablesWorking with Variables

► In C++, you must name and give a type In C++, you must name and give a type to variables (sometimes called to variables (sometimes called identifiersidentifiers) before you can use them) before you can use them

► Names of C++ variables can include Names of C++ variables can include letters, numbers, and underscores, but letters, numbers, and underscores, but must begin with a letter or underscoremust begin with a letter or underscore

► No spaces or other special characters are No spaces or other special characters are allowed within a C++ variable nameallowed within a C++ variable name

► Every programming language contains a Every programming language contains a few vocabulary words, or few vocabulary words, or keywordskeywords, that , that you need in order to use the languageyou need in order to use the language

1

Page 8: C++ rajan

Common C++ KeywordsCommon C++ Keywords

Page 9: C++ rajan

Working with VariablesWorking with Variables

► A C++ keyword cannot be used as a variable A C++ keyword cannot be used as a variable namename

► Each named variable must have a type Each named variable must have a type ► C++ supports three simple types:C++ supports three simple types:

IntegerInteger — Floating point— Floating point — Character— Character

► An An integerinteger is a whole number, either positive is a whole number, either positive or negativeor negative

► An integer value may be stored in an An integer value may be stored in an integer integer variablevariable declared with the keyword declared with the keyword intint

► You can also declare an integer variable You can also declare an integer variable using short int and long intusing short int and long int

1

Page 10: C++ rajan

Working with VariablesWorking with Variables

► Explicitly stating the value of a variable is Explicitly stating the value of a variable is called called assignmentassignment, and is achieved with the , and is achieved with the assignment operator =assignment operator =

► The variable finalScore is declared and The variable finalScore is declared and assigned a value at the same timeassigned a value at the same time

► Assigning a value to a variable upon creation Assigning a value to a variable upon creation is often referred to as is often referred to as initializinginitializing the variable the variable

1

Page 11: C++ rajan

Creating CommentsCreating Comments

► A A line commentline comment begins with two slashes (//) and begins with two slashes (//) and continues to the end of the line on which it is placedcontinues to the end of the line on which it is placed

► A A block commentblock comment begins with a single slash and an begins with a single slash and an asterisk (/*) and ends with an asterisk and a slash (*/); asterisk (/*) and ends with an asterisk and a slash (*/); it might be contained on a single line or continued it might be contained on a single line or continued across many linesacross many lines

1

Page 12: C++ rajan

Using Libraries and Using Libraries and Preprocessor DirectivesPreprocessor Directives

► Header filesHeader files are files that contain predefined are files that contain predefined values and routines, such as sqrt( )values and routines, such as sqrt( )

► Their filenames usually end in .hTheir filenames usually end in .h► In order for your C++ program to use these In order for your C++ program to use these

predefined routines, you must include a predefined routines, you must include a preprocessor directivepreprocessor directive, a statement that tells , a statement that tells the compiler what to do before compiling the the compiler what to do before compiling the programprogram

► In C++, all preprocessor directives begin with In C++, all preprocessor directives begin with a pound sign (#), which is also called an a pound sign (#), which is also called an octothorpoctothorp

► The The #include#include preprocessor directive tells the preprocessor directive tells the compiler to include a file as part of the compiler to include a file as part of the finished productfinished product

1

Page 13: C++ rajan

C++ Binary Arithmetic C++ Binary Arithmetic OperatorsOperators

► Often after data values are input, you perform Often after data values are input, you perform calculations with themcalculations with them

► C++ provides five simple arithmetic operators C++ provides five simple arithmetic operators for creating arithmetic expressions:for creating arithmetic expressions: addition (+)addition (+) – subtraction (-)– subtraction (-) multiplication (*)multiplication (*) – division (/)– division (/) modulus (%)modulus (%)

► Each of these arithmetic operators is a binary Each of these arithmetic operators is a binary operator; each takes two operands, one on each operator; each takes two operands, one on each side of the operator, as in 12 + 9 or 16.2*1.5side of the operator, as in 12 + 9 or 16.2*1.5

► The results of an arithmetic operation can be The results of an arithmetic operation can be stored in memorystored in memory

2

Page 14: C++ rajan

Shortcut Arithmetic OperatorsShortcut Arithmetic Operators

► As you might expect, you can use two As you might expect, you can use two minus signs (--) before or after a variable minus signs (--) before or after a variable to to decrementdecrement it it

2

Page 15: C++ rajan

Shortcut Arithmetic OperatorsShortcut Arithmetic Operators

► The prefix and postfix increment and decrement The prefix and postfix increment and decrement operators are examples of unary operatorsoperators are examples of unary operators

► Unary operatorsUnary operators are those that require only one are those that require only one operand, such as num in the expression ++numoperand, such as num in the expression ++num

► When an expression includes a prefix operator, When an expression includes a prefix operator, the mathematical operation takes place before the mathematical operation takes place before the expression is evaluatedthe expression is evaluated

► When an expression includes a postfix When an expression includes a postfix operator, the mathematical operation takes operator, the mathematical operation takes place after the expression is evaluatedplace after the expression is evaluated

2

Page 16: C++ rajan

Shortcut Arithmetic OperatorsShortcut Arithmetic Operators

►The difference between the The difference between the results produced by the prefix and results produced by the prefix and postfix operators can be subtle, postfix operators can be subtle, but the outcome of a program can but the outcome of a program can vary greatly depending on which vary greatly depending on which increment operator you use in an increment operator you use in an expressionexpression

2

Page 17: C++ rajan

Evaluating Boolean ExpressionsEvaluating Boolean Expressions

► The unary operator ! Means not, and essentially The unary operator ! Means not, and essentially reverses the true/false value of an expressionreverses the true/false value of an expression

2

Page 18: C++ rajan

SelectionSelection

► Computer programs seem smart because Computer programs seem smart because of their ability to use selections or make of their ability to use selections or make decisions decisions

► C++ lets you perform selections in a C++ lets you perform selections in a number of ways:number of ways: The if statementThe if statement

The switch statementThe switch statement

The if operatorThe if operator

Logical AND and Logical ORLogical AND and Logical OR

2

Page 19: C++ rajan

Some Sample Selection Some Sample Selection Statements within a C++ ProgramStatements within a C++ Program2

Page 20: C++ rajan

The ifThe if StatementStatement

► Any C++ expression can be evaluated as Any C++ expression can be evaluated as part of an if statementpart of an if statement

2

Page 21: C++ rajan

The switch StatementThe switch Statement

► When you want to create different outcomes depending When you want to create different outcomes depending on specific values of a variable, you can use a series of ifs on specific values of a variable, you can use a series of ifs shown in the program statement in Figure 2-14shown in the program statement in Figure 2-14

► As an alternative to the long string of ifs shown in Figure As an alternative to the long string of ifs shown in Figure 2-14, you can use the 2-14, you can use the switch statementswitch statement

► The switch can contain any number of cases in any orderThe switch can contain any number of cases in any order

2

Page 22: C++ rajan

The if OperatorThe if Operator

► Another alternative to the if statement involves Another alternative to the if statement involves the the if operatorif operator (also called the (also called the conditional conditional operatoroperator), which is represented by a question ), which is represented by a question mark (?)mark (?)

► E.g. E.g.

► cout<<(driveAge<26)?”The driver is under cout<<(driveAge<26)?”The driver is under 26”:”The driver is at least 26”;26”:”The driver is at least 26”;

► The if operator provides a concise way to express The if operator provides a concise way to express two alternativestwo alternatives

► The conditional operator is an example of a The conditional operator is an example of a ternary operator, one that takes three operands ternary operator, one that takes three operands instead of just one or twoinstead of just one or two

2

Page 23: C++ rajan

Logical AND and Logical ORLogical AND and Logical OR

► In some programming situations, two or more In some programming situations, two or more conditions must be true to initiate an actionconditions must be true to initiate an action

► Figure 2-16 works correctly using a Figure 2-16 works correctly using a nested ifnested if—that is, one if statement within another if —that is, one if statement within another if statementstatement

► If numVisits is not greater than 5, the If numVisits is not greater than 5, the statement is finished—the second comparison statement is finished—the second comparison does not even take placedoes not even take place

► Alternatively, a Alternatively, a logical AND (&&)logical AND (&&) can be used, can be used, as shown in Figure 2-17as shown in Figure 2-17

2

Page 24: C++ rajan

Logical AND and Logical ORLogical AND and Logical OR2

Page 25: C++ rajan

Using the Logical ORUsing the Logical OR

► In certain programming situations, only one In certain programming situations, only one of two alternatives must be true for some of two alternatives must be true for some action to take placeaction to take place

► A logical OR (||) could also be usedA logical OR (||) could also be used► A logical OR is a compound boolean A logical OR is a compound boolean

expression in which either of two conditions expression in which either of two conditions must be true for the entire expression to must be true for the entire expression to evaluate as trueevaluate as true

► Table 2-4 shows how C++ evaluates any Table 2-4 shows how C++ evaluates any expression that uses the || operatorexpression that uses the || operator

2

Page 26: C++ rajan

Using the Logical ORUsing the Logical OR2

Page 27: C++ rajan

The while LoopThe while Loop

► Loops provide a mechanism with which to Loops provide a mechanism with which to perform statements repeatedly and, just as perform statements repeatedly and, just as important, to stop that performance when important, to stop that performance when warrantedwarrantedwhile (boolean expression)while (boolean expression)

statement;statement;

► In C++, the while statement can be used to In C++, the while statement can be used to looploop

► The variable count, shown in the program in The variable count, shown in the program in Figure 2-21, is often called a Figure 2-21, is often called a loop-control loop-control variablevariable, because it is the value of count that , because it is the value of count that controls whether the loop body continues to controls whether the loop body continues to executeexecute

2

Page 28: C++ rajan

The for StatementThe for Statement

► The The for statementfor statement represents an alternative to the represents an alternative to the while statementwhile statement

► It is most often used in a It is most often used in a definite loopdefinite loop, or a loop , or a loop that must execute a definite number of timesthat must execute a definite number of times

► It takes the form:It takes the form:for (for (initialize; evaluate; alter)initialize; evaluate; alter)

statementstatement;;

2

Page 29: C++ rajan

ThanksThanks