Itc Lab 7 - Intro To C++
Itc Lab 7 - Intro To C++
Lab
LAB###07
0
Introduction to IDE for C++ and structure of a Basic
C++ Program
Objective
Theory
IDE Basics:
General Overview:
1
Introduction to Computing Lab 7: Introduction to programming C++
To enter the IDE, double click on the CodeBlocks icon at the desktop or select from the start menu.
This will make you enter into the IDE interface. Click on the new icon or select it from the File
Menu which will look like the figure given below. Start writing your program where cursor is
currently placed. Press F5 to compile and run your program.
2
Introduction to Computing Lab 7: Introduction to programming C++
If your source file does not have any error then an output window will pop up and display the
result.
Using Menus
If the menu bar is inactive, you can invoke by pressing the ALT function key. To select different
menu, move the highlight left or right with cursor (arrow) keys. You can also revoke the selection
by pressing the key combination for the specificmenu.
To type in program you will need to open an Edit window. For this, open file menu and click
„new‟. A window will appear within the CodeBlocks screen with line numbers on the left. If the
code is too long you can see the code by scrolling the window.
3
Introduction to Computing Lab 7: Introduction to programming C++
Writing a Window
For writing program the Edit window will be active. Type the program code with the proper syntax
and command. Characters will appear where the cursor is positioned. Press [Enter] to move tothe
next line. Use the cursor keys to move to any position on the screen. You can delete characters by
using [Del] key. You can delete the entire line by selecting the line and pressing the [Del] key or
by pressing CTRL+Y keycombination.
Saving a Program
After typing the program code, you should save it to the disk. To perform this operation, select
Save from the File menu. Pressing the [CTRL+S] combination can also complete this operation.
Save the file and provide an appropriate and unique name to the file. Make sure you add the “.cpp”
extension to your filename. You can save the code after compiling the program but saving it before
is more appropriate.
Executing a Program
If the program is compiled and linked without errors, the program is executed by selecting Run
from the Build menu or by pressing the [F5] key.
Correcting Error
If the compiler, recognize some error, it will let you know through the Build window, which is at
the bottom of the program window. The error will be listed and the line containing the error will
be highlighted.
Now you have to see your Edit window and remove the error by appropriate actions. These errors
are due to different reasons. For example; wrong termination, wrong syntax, etc.
Exiting IDE
Before exiting IDE, programmer must save the necessary source file. If the file is not closed and
you try to exit the IDE, it will ask whether you wish to save the program or exit anyway.
An Edit window can be closed by different ways. You can click on the small cross in the upper
right corner, you can select Close from the Window menu, you can right click on the tab of the
program and click „close‟ or you can press the [Alt] [X] key combination.
You can close the CodeBlocks IDE by selecting EXIT from the File menu or by pressing the keys
[Alt] [F4].
4
Introduction to Computing Lab 7: Introduction to programming C++
In order to a write program in any programming language, it is necessary to know about its
command and syntax. Programmers must also know the basic usage of commands and other
programming structures. Programs written in C++ language are really not much difficult to
understand than one written in any other language, once you become familiar with the basic syntax.
Function Definition:
All C++ programs are divided into units called „functions‟. Every C++ program consists of one
or more functions. Consider the following program:
#include <iostream>
int main ()
return 0; }
The above program has a function named “main”. This function is one to which control is passed
from the Operating System when the program is invoked, i.e. it is the first function which will be
executed. The word “void” preceding “main” specifies that the function “main” will not return a
value. The second “void” in parentheses specifies that the function takes no arguments.
Delimiters:
The braces after the function definition signal the beginning and the end of the body of the
function. The opening brace { indicates the start of a block of code whereas the closing brace }
indicates the end of block or it terminates the block of code.
Braces are also used to delimit blocks of code in situations other than functions. They are used in
loops and decision-making statements within a program.
Statement Terminator:
Statement in C++ language are terminated with a semicolon “;”. Semicolon terminates the line not
the carriage return you type afterwards. C++ language pays no attention to carriage return in your
program listing. The compiler pays no attention to any of the so called “white space” characters:
the carriage return, the space and the tab.
5
Introduction to Computing Lab 7: Introduction to programming C++
Programmers can place as many of few white space characters in a program; they will be invisible
and will be ignored by the compiler.
Program Indentation:
As you can place as many white space characters in the program, therefore, they can be used to
make the program easier to read. Consider the previous program, which can also be written as
follows without proper indentation.
Indentation of block of code enclosed in braces is an important aspect of making C++ program
readable. Indentation the line of code is not critical in small programs but when there are many
sets of nested braces in a program, indentation becomes increasingly important.
The cout statement causes the phrase in quotes to be printed on screen. The cout statement is
always followed by insertion operator << i.e. every element in the cout statement must be separated
with << operator as it is used to separate the quoted phrase and manipulation operator “endl”. As
C++ language distinguishes between uppercase and lowercase characters, thus the statements
„COUT‟ and „cout‟ are not the same.
SYNTAX
Here e stands for an element. As syntax indicates, cout can take either one element or multiple
elements separated by << operators. Don‟t forget ; (semicolon) at the end of cout statement as it
terminates the statement.
Printing strings:
Any symbol or sequence of characters and symbols enclosed within double quotation is treated as
strings in C++. Consider the following program:
6
Introduction to Computing Lab 7: Introduction to programming C++
#include <iostream>
Using namespace std ;
int main (void)
{
cout << “Welcome”
<< “ To “
<< “Hamdard University” ;
Return 0;
Compile and run the aforementioned program and note down the output.
Printing numbers:
cout behaves differently when it encounters numerical expressions instead of strings. Recall that
anything enclosed within double quotation is printed as it is on the output screen. Numerical
expressions on the other hand are substituted with the result of expression and resulting answer is
than printed on the output screen. Consider the following program:
Compile and run the above mentioned program and note down the output.
7
Introduction to Computing Lab 7: Introduction to programming C++
Lab Task
Lab Task. 7.1) Write a program which prints your bio data and also show it.
8x1=8
8 x 2 = 16
8 x 3 = 24
8 x 4 = 32
8 x 5 = 40
8 x 6 = 48
8 x 7 = 56
8 x 8 = 64
8 x 9 = 72
8 x 10 = 80
Lab Task. 7.3) Write a program which reads marks for three subjects and then prints total
marks and percentage.
Sample Output