Sunday 1 January 2012

III. BASIC STATEMENT



III. BASIC STATEMENT

Paragraph Answers

1.What are header file? How it can be used in C++?
  • A header file comprises of all standard declarations and definitions for pre-defined functions.
  • One can include the header file in the program by using a preprocessor directive.
  • A preprocessor directive starts with #, which instructs the compiler to do the required job.
  • #include<iostream.h> is a typical preprocessor directive, that instructs the compiler to include the header file iostream.h.
  • In order to use cin / cout objects one has to include iostream.h in the program.
  • The other header files are iomanip.h, stdio.h, ctype.h, math.h, fstream.h etc.
2.What is the purpose of declaration statement?
  • Variables need to be declare and define before they are used in a program
  • Declaration of a variable introduces a variable’s name and its associated data type
  • Declaration statements are used to declare user defined data type indentifiers, function headers, pointer variables and the like.
  • Some variables also get defined when they are declared.
  • Pointer variables get defined only when memory is fetched. For example by using NEW memory operator.
3.What are control statement?Explain .
Ref.short Ans.
4.Explain Selection Statements with examples
Ref.short Ans.
5.What is if else statement? Given an example.
  • if…else…statement which chooses between two alternatives, executes the chosen block based on the condition
  • Here if the condition is TRUE block will be executed , otherwise the statement following else option will be executed
Syntax:
If (condition / expression)
{
action block 1;
}
else
{action block 2;
}
Example:
# include <iostream.h>
# include <conio.h>
void main()
{
int a,b;
clrscr();
cin >> a>>b;
if ( a>b)
cout << “\nA is greater”;
else
cout << “\nB is greater”;
getch();
}
  • In the above program “A is greater” is printed if the expression is evaluated to true,
  • Otherwise statement following else option will be executed

11.Explain Nested if with an example.
  • In an nested if .. else statement, “Each else matches with the nearest unmatched preceding if”
  • The statement sequence of if or else may contain another if statement
Syntax:
If (condition 1)
If (condition 1)
{action 1;}
else
{action2;}
else
{action3}
Example
if (grade = = ‘A’)
if (basic > 5500)
incentive = basic * 10/100;
else
incentive = basic * 5/100;
else
cout << “Try to attain Grade A”;
Working of a program
First & Second condition TRUE then incentive gets the value basic*10/100
First TRUE Second FALSE , then incentive gets the value basic*5/100
First FALSE – the inner if will not be executed , the outer else will be executed and thus prints “Try to attain Grade A.

6.Explain the switch statement and gives suitable program using switch statement.
  • This is a multiple branching statement
  • Based on a condition, the control is transferred to one of the many possible points.
Syntax:
switch (expression/variable)
{
case 1 : action block 1; break;
case 2 : action block 2; break;
default : action block 3;
}






Example:
# include<iostream.h>
# include<conio.h>
void main( )
{
clrscr();
int n;
cin >> n;
switch (n)
{
case 1 : cout << “\nONE”; break;
case 2 : cout << “\n TWO”; break;
default : cout << “\nEnter only 1 & 2”;
}
getch( );
}
  • If n=1
  • case 1 statement will execute directly and terminated by break;
  • If switch expression gets other then the case value default statement will execute
Break
  • Every action block should be terminated with a break statement.
  • Break statement would exit the current loop only.
  • Break statement accomplishes jump from the current loop.
7.Explain the loops with the suitable program.
  • Loops execute a set of instructions repeatedly for a certain number of times.
  • Based on the position of the condition, the loops are classified as
  • Entry-Check loop and Exit Check Loop
  • A looping block therefore consists of two segments 1.The body of the loop 2. The control statement.
  • The control statement checks the condition, based on that the body of the loop to execute the segment repeatedly
There are three kinds of loops in C++, the for loop, the while loop and the do.. while loop
Looping Process
  • The control variable is initialized the first time
  • The body of the loop is executed only if the condition is TRUE.
  • the control variable is incremented for next Iteration
  • The condition will be evaluated before the body of the loop is executed.
  • The loop is terminated when the condition evaluates to FALSE

# include<iostream.h>
# include<conio.h>
void main( )
{
clrscr();
Int n;
n=1;
while(n<=5)
{ cout <<n;
n++;
}
getch(); }

8. Explain do..while loop with suitable program? Explain Exit – check loop.
  • do.. while <(condition)> is called as Exit – check loop,
  • The condition placed at the last statement of the body of the loop.
  • The body of loop will be executed at least one time.
Syntax:
do
{
a
Output
6
ction block;

} while (condition)
Ex.
# include<iostream.h>
# include<conio.h>
void main( )
{
clrscr();
Int n=6;
do
{ cout <<n;
n++;
} while(n <= 5);
getch(); }
9. Explain while<condition>loop with suitable program?
  • while loop is called as the Entry-check loop.
  • The condition placed at the beginning of the body of the loop.
  • The body of the loop will be executed only if the condition results true .
  • The control exits the loop once the condition is evaluated of false.
Syntax:
while <(condition)>
{

action block

}
Ex:
# include<iostream.h>
# include<conio.h>
void main( )
{
clrscr();
Int n;
n=1;
while(n<=5)
{ cout <<n;
n++;
}
getch(); }
Output
12345


10. Explain entry check loops with suitable program ref. 9 ,11
11.Explain for loop with suitable program?
for(; ;)…loop: is an Entry -check loop
  • The condition placed at the beginning of the body of the loop.
  • The body of the loop will be executed only if the condition results true .
  • The control exits the loop once the condition is evaluated of false.
Syntax:
for(initial value ; test-condition ; increment)
{
action block;
}
General working of for loop
  • The control variable is initialized the first time
  • The body of the loop is executed only if the condition is TRUE.
  • the control variable is incremented for next Iteration
  • The condition will be evaluated before the body of the loop is executed.
  • The loop is terminated when the condition evaluates to FALSE
Ex.
# include<iostream.h>
# include<conio.h>
void main( )

{
clrscr();
Int n;
For(n=1;n<=5;n++)
{
cout<<n;
}
getch();
}
Output
12345

12.Explain Nested loop with an example.
  • If the loop construct inside the body of another loop is called nested.
The rules for the formation of nested loops are:
1.An outer loop and inner loop cannot have the same control variable, as it will lead to logical errors.
2.The inner loop must be completely nested inside the body of the outer loop.

Loop 1()
{
Loop 2()
{
action block 2;
}
Action block 1;
}

Example
for(int i = 1; i <= 3; i++)
{
int j = 1;
while (j <= i)
{
cout << “* “;
j++;
}
cout << ‘\n’;
}

The iterations of the nested loops are as follows;
For loop
While loop
I=1
Is executed once(j<=1)
I=2
Is executed twice (j=1..2)
I=3
Is executed thrice(j=1..3)

13.Explain the development of Program
  • Programs are written in high level language using the grammar of a computer language.
  • A Program written in high level language is called as the Source Code.
  • The source code has to be converted to machine-readable form.
  • The machine-readable form of a program is called as Object file.
  • Compiler checks for the grammar of language (syntax) and create object files from source code.
  • An object file is created from an error free source code.
  • The object file is linked with the essential libraries to generate an executable file.
14. Explain Input ,Output and Assignment statement with an example

Short Answers

1.What are the different statements in C++?
The different statements in C++ are
  • Input / output,
  • Declaration,
  • Assignment,
  • Control structures,
  • Function call,
  • Object message and
  • Return.
2.How many methods used for assigning data to the variables and what are they?
There are two methods used for assigning data to the variables.
  • By assignment statement ex. a= 15;
  • During the runtime of a program. ex. cin>>a;
3.What is the use of cin object? (OR)Define cin statement (OR)What is the use of Input stream? Define input statement.
  • cin is a predefined object that corresponds to a standard input stream.
  • Data is read from the key board during runtime by using the object cin.
  • cin can read data from other sources also.
  • The declarations for the object cin are available in a header file called as istream.h
  • The operator >> is called the extraction operator or get from operator.
  • The (>>) extracts data from the input stream object (cin) and
places the value in the variable(a) on its right. ex. cin>>a;
4.What is istream.h and ostrem.h?
  • The declarations for the object cin are available in a header file called as istream.h
  • The declarations for the object cout are available in a header file called as ostream.h
  • iostream.h file comprises the combined properties of istream and ostream.
5.Define header file.
  • A header file comprises of all standard declarations and definitions for predefined functions
  • One can include the header file in the program by using a preprocessor directive
  • A preprocessor directive starts with # , which instructs the compiler to do the required job.
  • Syntax : # include <header file> Ex. iomanip.h, stdio.h, ctype.h, math.h, fstream.h etc
6.What is the use of cout object?
  • cout pronounced as (C out) is a predefined object of standard output stream.
  • The standard output stream normally flows to the screen display – although it can be redirected to several other output devices.
  • The declarations for the object cout are available in a header file called as ostream.h
  • The operator << is called the insertion operator or put to operator.
  • It directs the contents of the variable on its right to the object on its left. Ex. cout<<a;
7.Define cascading the extraction and insertion operator.
  • Multiple values can be read from the input stream and placed in the corresponding variable by cascading the extraction operator
  • Sending of multiple output via single statement by cascading the insertion operator. Ex. cout<<a<<b;
8.Give the various sections in a C++ program?
  • Include files
  • Declaration of variables , data type , user defined functions.
  • main() function
9.What is the use of main() function?
  • When the program is executed the main() function will be automatically executed.
  • It is use to give call statements to the various modules to be executed and the executed statements.
    10.Write the importance of declaration statements.
  • Variables need to be declare and define before they are used in a program
  • Declaration of a variable introduces a variable’s name and its associated data type
  • Declaration statements are used to declare user defined data type indentifiers, function headers, pointer variables and the like.
11.What is called as definition statement?
  • When a declaration sets aside memory for the variable it is called as definition.
  • For example consider the declaration statement – int num;
  • This statement is called as definition statement because memory is set aside to store data.
  • Ex. int num; num = 5;
12.What is an assignment operator? Which operator is used for assignment operator?
  • An assignment statement used to assign the value on the right hand side of an expression to the variable on the left hand side of the assignment operator.
  • =’ is the assignment operator.
  • Ex. a=5; tot= a=b; s+=5;
13.What are control statements or control structures?
  • Program statements that cause jump of control from one part of a program to another called as Control Structures.
  • The two major category of control structures are Decision making (selection)statements (if,switch) and Looping Statements(for,while,do..while).
14.What is selection statement and its types.
In a program a decision(condition) causes a one time jump to a different part of a program.
They are 1) if statement 2) switch statement
15.What is the use of if and if else statement in C++? (or) Difference between if and if… else
if statement is the simplest of all the decision statements.
It is implemented in two forms.
1.simple if statement
  • Here if the condition is TRUE block will be executed , otherwise the control jumps out of the block
If (condition / expression)
{
action block;
}
2. if…else statement
  • if…else…statement which chooses between two alternatives, executes the chosen block based on the condition.
  • Here if the condition is TRUE block will be executed , otherwise the statement following else option will be executed
If (condition / expression)
{action block 1;
}
else
{action block 2;
}
16. Define Break statement (or) What is the use of break statement
  • Every action block should be terminated with a break statement.
  • Break statement would exit the current loop only.
  • Break statement accomplishes jump from the current loop.

17.How loops are classified based on the position of the condition?
Based on the position of the condition, the loops are classified as
Entry-Check loop and Exit Check Loop
18.What is continue statement?
  • The continue statement forces the next iteration of the loop to take place,
  • Skipping any code following the continue statement in the loop body.
Syntax : continue;
19.What is Break statement?
  • break statement would exit the current loop only.
  • break statement accomplishes jump from the current loop
20.What is called as exit check loop? Give example. (or) Define do…while ref. detail
21.What is called as entry check loop? Give example ref. detail
22.Differentiate between do..while and while.
  • ref. detail
23.Explain for loop with an example ref. detail
24.What are the general working of for loop? ref. detail
25.What is the working of continue statement in various loops? ref book pg.No.84
26.Differentiate between continue and break statements.
Ref. book Pg.No. 84
27.What is nested loops and write the rules to construct nested loops



Book Exercises:

1. Categorise the following declarations as valid/invalid. If invalid, specify the reasons
Declarations
Valid/ Invalid
Reason
int A; a;
Invalid
; semicolon can’t be used as separator .
, comma is used to separate variables of same data type
char name (10);
Invalid
( ) not to be used correct one is [ ].
float f, int;
Invalid
int is a data type and variable is not declared.
double d, float f;
Invalid


Two data types can’t be defined in a single statement it must be in two different statements.
int 1choice, _2choice
Invalid
The variable name must starts with alphabetic character.



2. Debug the following program. Rewrite the corrected program.

# include<iostream.h>
# include<conio.h>
void main( )
{
int n, n1, result;
cout<< “\n Enter two number ..” ;
cin >> n;
cin>> n1;
result = n * n1;
cout << “\n” <<result;
}


Output

Enter a number: 4 8
3.Write appropriate declaration statements for the following:
a)float a;
a = 8/3;
b) char Emp_Name [6] ;
Emp_Name = “Kalam” ;
c) char ch=’y’;
cout << “Enter your choice <y/n>: ”;
cin >> ch

4.Point out the errors in the following snippets:
a)
int a = 10, b = 5;
if (a > b)
cout << a;
b)
if ((a<b) && (a<0))
cout << “a is negative and ..”;
c)
char option = ‘Y’ ;
do
{
cout << ‘*’;
……
} while (option == ‘Y’);

d) no errors.

e)
char ch;
while(ch = = ‘y’) ;
{
cout << “\n continue <y/n> ….”;
cin >> ch;
}
5.What will be the output of the following snippets / programs?
5a)
output
Enter feet
7
converted to inches …
84
5b)
output
2
3
4
5
6
value of the variable I after executing the while loop.
7
sum…20
5c)
Out Put
2
3
4
5
6 14
5d)
Output
1
2
3
4
5
6 15
5e)
Out Put
#
##
###
####
#####
5f)
Output
The sum of digits of 1784 is : 20
5g)
Output
The sum is ….36

5h) -5


5j)
Output
do loop….
5k)
Output
Returning to Edit Window…
5l)
Output
June….
5m)
Output
Wednesday
Thursday
Friday
5n)
Output
2 5
1 5

No comments:

Post a Comment