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

The basics of C++ programming, including program construction, output using cout, string constants, directives, comments, defining an integer variable, integer constant, and C++ data types. It provides code examples and explanations for each topic.

Typology: Slides

2021/2022

Available from 11/16/2022

razaroghani
razaroghani 🇵🇰

4.5

(4)

151 documents

1 / 64

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
INTRODUCTION TO PROGRAMMING
CHAPTER 02
C++ PROGRAMMING BASICS
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40

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