You are on page 1of 23

Introduction to programming I 2007

E.C

By Deres G. RVU Page 1


Introduction to programming I 2007
E.C

Chapter 2
Introduction to C++ programing

By Deres G. RVU Page 2


Introduction to programming I 2007
E.C

CHAPTER-3
Control statements
Boolean Expressions

A Boolean Expressions resolves either true or false. Boolean expressions in C++ is


enclosed with in parentheses often comparing the relations between two or more
operands. C++ seas the value of zero as false and non-zero as true.

Example

int num1=100,num2=4;

(num1>num2); //returns true

!(num2);//returns false.

Selection Statements
Selection statements are mostly used with Boolean expressions. There are two
types of selection statements

 If statements
 Switch statements
1. If statement

The if statements enables the programmer to test the condition and branch to
different parts of the code.

Syntax

if (expression)

(Statements)

Example

//This program Demonstrates if

#include<iostream.h>
By Deres G. RVU Page 3
Introduction to programming I 2007
E.C

void main()

int x;

cout<<"Enter any number : ";

cin>>x;

if(x>0)

cout<<"Your input is positive";

else [ if ]

often the program should take one branch if the condition is true and another
branch if the condition false. In such situation you can use else and or else if to
handle multiple conditions.

Syntax

If(Boolean expression)

Statment(s)

else if (Boolean expression)

Statement (s)

else if (Boolean expression)

Statement(s)

else

By Deres G. RVU Page 4


Introduction to programming I 2007
E.C

Statement(s)

Example

#include<iostream.h>

void main()

int x;

cout<<"Enter any number : ";

cin>>x;

if(x>0)

cout<<"Your input is Positive\n";

else if(x<0)

cout<<"Your input is nagative\n";

else

cout<<"Your input is Zero\n"

Nested if

Syntax

if(Boolean expression)

if(Boolean expression)

Statement(s)

By Deres G. RVU Page 5


Introduction to programming I 2007
E.C

else if (Boolean expression)

Statement(s)

else

Statement(s)

else if (Boolean expression)

Statement(s)

else

Statement(s)

Example

//Demonstrates nested if

#include<iostream.h>

void main()

int x;

cout<<"Enter any number : ";

By Deres G. RVU Page 6


Introduction to programming I 2007
E.C

cin>>x;

if(x>=0)

if(x>0)

cout<<"Your input is positive\n";

else

cout<<"Your input is zero\n";

else

cout<<"Your input is negative\n";

The Switch statement


A switch statements select one of several actions according to constant
expressions.

Syntax

switch expression

case value 1: statement(s);break;

case value 2: statement(s);break;

By Deres G. RVU Page 7


Introduction to programming I 2007
E.C

case value n: statement(s);break;

default: statement(s);

Switch evaluates expression or equality with case values. If much is found the
corresponding statement is executed. The key word break prevents execution from
following through the next statement. The default statement will be executed only
if expression does not any of case statement.

Example

//Demonstrates use of switch

#include<iostream.h>

void main()

char grade;

cout<<"Enter students grade : ";

cin>>grade;

swich (grade)

case 'A': cout<<"Excelent\n"; break;

case 'B': cout<<"Very good\"; break;

case 'C': cout<<"Average\n"; break;

case 'D' cout<<"Below Average\"; break;

case 'F' cout<<"Not Satisfactory\n"; break;

default: cout<<"Invalid\n";

By Deres G. RVU Page 8


Introduction to programming I 2007
E.C

cout<<"\n\n Have a nice week end!";


}

LOOPS
Loops statements are useful when you want the same piece of code to be executed
a number of times. There are different loops statements; each one is designed for
slightly different programming situations.

Here we will see three different kind of looping statement.

1. The for statement


2. The while statement
3. The do statement

The for statement


The for statement is used when there are specified number of items to iterate
through.

Syntax

for(initial -stmt; expr-1; expr-2)

statement;

Example

/* A program to display all capital letters using for loop*/

#include<iostream.h>

void main()

cout<<"Here are list of capital letter\n";

By Deres G. RVU Page 9


Introduction to programming I 2007
E.C

for (char ch='A';ch<='Z';ch++)

cout<<ch<<endl;

The while statement


In some cases we may not know how many times the loop should be executed. In
these cases a different type of loop should be used. one possibility is to use a while
loop.

syntax

while(expression)

statement(s);

Example

/* A program to display all lower case letter */

#include<iostream.h>

void main()

char ch;

ch='a';

while (ch<='z')

cout<<ch<<endl;
By Deres G. RVU Page 10
Introduction to programming I 2007
E.C

ch++;

cout<<"Have a nice week end";

Exercise

Write a program that accepts any non-negative integer, and return the factorial of
the number.

The Do statement
with the do loop the evaluation expression comes after the execution statement.
hence the do statement will always executed the loop body at least once.

Syntax

do

statement(s);

}while (expression)

Example

/* A program to display sum of the first n positive integer */

#include<iostream.h>

void main()

int n, i=1;

long sum=0;

cout<<"Enter any positive integer : ";

By Deres G. RVU Page 11


Introduction to programming I 2007
E.C

cin>>n;

if(n>0)

do

sum=sum+i;

i++;

}while (i<=n);

cout<<"Sum of the first "<<n<<" Positive integer is "<<sum;

else

cout<<"Your input is non-positive ";

Using Nested Loop


Nesting loops is the process of placing one loop inside another. a nested loops
iterates trough its enter cycle with each iteration of its parent loop.

Example

for (int i=0;i<4;i++)

for(int j=0;j<4;j++)

cout<<(i+j)<<" ";

cout<<endl;

}
By Deres G. RVU Page 12
Introduction to programming I 2007
E.C

CHAPTER -4
Functions (Modules)
Function is a subprogram that can act on data return value. a function is always
called by another function(exception of main function) which is called by operator
system. The called function must often able to receive data from calling function.
This data is passed to called function through its parentheses.

There are various type of function such as void functions and a function that return
a value

1. Declaring and Defining function

Using function in a program requires declaring and defining the function. The
declaration is called the function prototype. It tells the compiler Name, return type
and parameters of the function. Function definition tells how the function works.

Syntax

function prototype

calling function head

function call

function Definition

Example

By Deres G. RVU Page 13


Introduction to programming I 2007
E.C

int sample( );

void main( )

int y=sample( );

.
}

Example

/* A program that incorporates function to return sum of two sum


of two integer */
#include <iostream.h>

void main( )

int sum(int a, int b); // prototype

void main ( )

int num1, num2;

cout<<"Enter two Integers "<<endl<<endl;


By Deres G. RVU Page 14
Introduction to programming I 2007
E.C

cout<<"Input 1: ";

cin>>num1;

cout<<" Input 2: ";

cin>>num2;

int s=sum(num1, num2);

cout<<"Sum of "<<num1 <<" and "<<num2<<" is "<<s<<endl;

cout<<" ********** HAVE A NICE DAY **********";

//Start defining function sum

int sum(int j, int k)

int add;

add=j+k;

return(add)

Function prototype is declaration of a statement in the calling argument.

syntax

type function name( type of argument);

Type of argument contain data types of argument that must be passed to the
function and optional names of arguments.

e.g. float sum-up(float x, float y, float z);

float sum-up(float x,y,z); illegal.

The calling statement should not include type name in the argument list.
By Deres G. RVU Page 15
Introduction to programming I 2007
E.C

Defining the Function

The definition of a function consists of function header and its body. the header is
exactly like a function prototype.

The body of a function is a set of statement enclosed in Brasses. every function has
a return type. The function that has not return value its return type will be void.

//Program print a triangle of stars

#include<iostream.h>

void printstars(int blanks, int stats)

void main( )

int nolines;

int counter, noblanks;

cout<<"Enter the number of stars lines (1 to 20) to be printed ";

cin>>nolines;

while(nolines<0 || nolines>20

cout<<"Number of star line should be between 1 and 20 "<<endl;

cout<<"Enter the number of star lines to be printed :";

cin>>nolines;

cout<<endl<<endl;

noblanks=30;

for (counter=1; counter<=nolines; counter++)

By Deres G. RVU Page 16


Introduction to programming I 2007
E.C

printstars (noblanks, counter);

noblanks- -;

void printstars(int blanks, int starline)

int count

for (count=1; count<=blanks; count++)

cout<<" ";

for (count=1; count<=starline; count++)

cout<<" * ";

cout<<endl;

By Deres G. RVU Page 17


Introduction to programming I 2007
E.C

Chapter 5
Arrays
What is array?
Array is a structured with homogenous elements of data type. It is a collection of
data storage locations each of which holds the same type of data. It is storage
location is called an element of the array.

The program declares an array by writing data type followed by array name and
subscript. The subscript (index) is number of elements in array sorrowended by
square brackets[ ].

1. One dimensional array

One dimensional array is column data items. all of them have the same data type
that are collectively referred by the same name.

Example

float result[50]

The program access each of the array element by specifying array name followed
by index enclosed by square brackets [ ]. Array elements are count from zero to n-
1 when n is the size of the array.

Example

Example

By Deres G. RVU Page 18


Introduction to programming I 2007
E.C

/* A program that accepts result of five students' and display back */

#include<iostream.h>

void main( )

float result[50];

cout<<"Enter result of five student ";

for (int i=0; i<5; i++)

cout<<" Enter result "<<i+1<<": ";

cin>>result[i];

cout<<"\n Results you entered are \n";

for (i=0; i<5; i++)

cout<<"Result "<<i+1<<" = "<<result[i];

cout<<" \n Have A Nice Week End! ";

Home work

Write a simple code that accepts results of n students and displays number of
students who scored less than average .

Multi-Definitional array

By Deres G. RVU Page 19


Introduction to programming I 2007
E.C

Arrays can have any number of diminutions. Each dimensions represented by


index in the array.

Example

To declare two dimensional array of type integer data type.

int A[10][20]

initializing array

Example

int A[4]={1,2,3,4};

int A[ ]={1,2,3,4,5,6};

int A[3]={1,2,3,4}; //illegal


float A[2][3]={1,2,3,4,5,6};

float A[2][3]={{1,2,3}{4,5,6}};

Example.

/*A program to accept table of real number and display back */

#include<iostream.h>
void main( )
{
int row, col, i, j ;
folat table[100][100];
cout<<"Enter number of rows : ";
cin>>row;
cout<<" Enter number columns :";
cin>>col;
cout<<" Now enter "<<row<<" X "<<col<<"real numbers\n";

By Deres G. RVU Page 20


Introduction to programming I 2007
E.C

for (i=0; i<row; i++)


for(j=0; j<col; j++)
cin>>table[i][j];
cout<<" Your table of real numbers is :\n\n";
for(i=0; i<row; i++)
{
for(i=0; j<col; j++)
cout<<table[i][j]<<"\t";
cout<<endl;
}
}
Exercise

A. Write a program that accepts n real numbers and displays max and min from
the list.
B. Write a program that returns the sum of two Matrix

Example
/* The following program finds average of numbers that are stored in array*/
#include<iostream.h>
void main( )
float av, sum=0;
int num[10];
cout num[10]; i;
cout<<"Enter the numbers \n ";
for (i=0; i<10; i++)
{
cout<<"Enter number "<<i+1 <<" : ";
By Deres G. RVU Page 21
Introduction to programming I 2007
E.C

cin>>num[i];
}
for (i=0; i<10; i++)
sum+=num[i];
av=sum/10;
cout<<"\n\n The average is "<<av;
}

Example
/* The following program finds the highest temperature out of the three cities of four
seasons*/
void main( )
{
const int rows=3;
const int columns=4;
int seasontemp[rows][columns]={{26,34,22,17},{24,32,19,13},{28,38,25,20}};
int highest=0, i, j;
for (j=0; i<rows, ++i)
for( j=0; j<columns; ++j)
{
if(temp[i][j]>highest
highest=temp[i][j];
}
cout<<" The highest temperature is ="<<highest;
}

By Deres G. RVU Page 22


Introduction to programming I 2007
E.C

By Deres G. RVU Page 23

You might also like