Docsity

Chapter - 03 - Programming fundamental - Loops and Decisions, Slides of Programming Languages

Partial preview of the text

Download Chapter - 03 - Programming fundamental - Loops and Decisions and more Slides Programming Languages in PDF only on Docsity!

INTRODUCTION TO PROGRAMMING

CHAPTER 02

C++ PROGRAMMING BASICS

BASIC PROGRAM CONSTRUCTION

//first.cpp #include #include <stdlib.h> using namespace std; int main(){ cout << "Welcome to this course\n"; system("PAUSE"); return 0; }

Explanation: ◼ This program consists of a single function called main().

BASIC PROGRAM CONSTRUCTION

◼ Every function must use this pair of braces around the function body. ◼ A function body can consist of many statements but this function has only three statements (line number 6, 7 and 8) ◼ You can put several statements on one line and one statement in over two or more lines. cout <<"Welcome to this course\n" ;

1 cout <<"Welcome Students\n" ; return 0 ;

BASIC PROGRAM CONSTRUCTION

◼ We don’t recommend these syntax. it’s nonstandard and hard to read. but it does compile correctly. ◼ #include is a preprocessor directive , which must be written on one line. ◼ #include tells the compiler to first include standard C++ library named iostream to our project. ◼ A string constant "Welcome to this course\n" can also be broken into separate lines if u insert a backslash () at the line break or ◼ divide the string into two separate strings, each surrounded by quotes cout <<"Welcome
to this
course\n" ; cout <<"Welcome " "to this " "course\n" ;

PROGRAM STATEMENTS

◼ There are two statements this program cout <<"Welcome to this course\n"; return 0; ◼ The first statement tells the computer to display the quoted phrase. ◼ A semicolon ; signals the end of the statement. If you leave out the semicolon, the compiler will often signal an error. ◼ The return 0; tells main() to return the value 0 to whoever called it, in this case the operating system or compiler. ◼ you can not give main() the return type of void , this is an error in new compilers.

OUTPUT USING COUT

◼ (If you know C, you’ll recognize << as the left-shift bit-wise operator and wonder how it can also be used to direct output. ◼ In C++, operators can be overloaded. That is, they can perform different activities, depending on the context.

STRING CONSTANTS

◼ The phrase in quotation marks, "Welcome to this course\n", is an example of a string constant. ◼ A constant , unlike a variable, cannot be given a new value as the program runs. Its value is set when the program is written, and it retains this value throughout the program’s existence. ◼ The '\n' character at the end of the string constant is an example of an escape sequence. ◼ The '\n' causes the next text output to be displayed on a new line. ◼ Line no. 2 and 3 in our program are called directives. The first is a preprocessor directive ,and the second is a using directive.

#INCLUDE DIRECTIVE

◼ The preprocessor directive #include tells the compiler to insert another file into your source file. In effect, the #include directive is replaced by the contents of the file indicated. ◼ Using an #include directive to insert another file into your source file is similar to pasting a block of text into a document with your word processor. ◼ #include is only one of many preprocessor directives, all of which can be identified by the initial # sign. ◼ The type file usually included by #include is called a header file.

HEADER FILES

◼ In our program the preprocessor directive #include tells the compiler to add the source file IOSTREAM to the FIRST.CPP source file before compiling. ◼ IOSTREAM is an example of a header file (sometimes called an include file). It’s concerned with basic input/output operations, and contains declarations that are needed by the cout identifier and the << operator. ◼ Without these declarations, the compiler won’t recognize cout and will think << is being used incorrectly. ◼ The newer Standard C++ header files don’t have a file extension, but some older header files have the extension .H.

COMMENTS

◼ Comments help the person writing a program, and anyone else who must read the source file, understand what’s going on. ◼ The compiler ignores comments, so they do not add to the file size or execution time of the executable program. ◼ Comments start with a double slash symbol ( // ) and terminate at the end of the line //first.cpp #include // preprocessor directive using namespace std; // "using" directive int main() // function name "main" { // start function body cout <<"Welcome to this course\n"; // statement return 0; // statement } // end of function body 1 2 3 4 5 6 7 8

OTHER STYLES OF COMMENTS

/ this is an old-style comment / / this is a potentially very long multiline comment / int main(/ a comment within parentheses/) { /* this is comment into the function body / } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16*

DEFINING AN INTEGER VARIABLE

◼ Integer variables exist in several sizes, but the most commonly used is type int. ◼ The amount of memory occupied by the integer types is system dependent. ◼ On a 32 - bit system such as Windows, an int occupies 4 bytes (which is 32 bits) of memory. This allows an int to hold numbers in the range from – 2,147,483,648 to 2,147,483,. ◼ The type int occupies 4 bytes on current Windows computers, it occupied only 2 bytes in MS-DOS and earlier versions of Windows.

◼ Figure: A variable of type int in memory