Sie sind auf Seite 1von 12

I have created this project on Object oriented programming with C++ .

I would like to express my special thanks of graduate to my teacher Mrs. Vandana Agarwal. Who give me the golden opportunities to do this wonderful project. Which also helped me in doing a lat of research and I come to know about so many new things. I am really thankful to them.

Secondly I would also to thank my parents and friends who helped me a lat in finishing this project within the limited time. I am making this project not only for marks but to also increase my knowledge.

Q.1:-What is meant by looping? Describe three different forms of looping, with a general syntax. Ans:LOOPING
The iteration statements allow a set of instructions to be performed separately until a certain condition is fulfilled. The iteration statements are also called loops or looping statements. C++ provides three condition of loops: For loop. While loop. Do while loop. The for loop:The for loop is the easiest to understand of C++ loops. All its loop control elements are gathered in one place on the top of the loop construction of C++, they are scattered about the program. The general form(syntax) of the for loop statement is : The following example program illustrate the use of for statements.

for( initialization(s),text expression; update expression(s)) body of the loop

Program using for loop to print numbers from 1 to 10. #include<iostream.h> #include<conio.h> void main( ) { int i; clrscr( ) for(i=1;i<=10;i++) cout<<\n<<i; return 0; } The three elements: initialization, text expression, and update expression generally given the working of a loop and thus are known as loop control elements.

The while loop:-

The second available in C++ is the while loop. The while loop is entry control loop. The syntax is a while loop:

Initialization while expression loop body

in a while loop, a loop control variable should be initialized before the loop beings as an initialized variable can be used an expression. The loop variable should be update inside the body of the while. Following example program illustrate the working of while loop. Program to calculate the factorial of an integer. #include<iostream.h> #include<conio.h> void main( ) { int i,num,fact=1; clrscr(); cout<<\n enter a no; cin>>num; i=num; while(num) { fact*=num; ----num; cout<<the factorial of<<i; cout<<is<<fact<<\n; return 0; } getch(); }

The do while loop:-

Unlike the for and while loops, the do while is An exit-control loop. It evaluates its text-expression at the bottom of the loop after executing its loop body statements. This means that a do while loop always executes at least once. In the other two loops for and while, the text expression is evaluated at the being of the loop before executing the loop body. If the text expression evaluates the false for the first time itself the loop is never executed. But in some situations, it is wanted that the loop body is executed at least once, no matter what the initial state of the text expression. In such cases, the do while loop is the following syntax:

Program to display a hello massage. #include<iostream.h> #include<conio.h> void main() { int c=i; do { cout<<hello<<endl; c=c+1; } while(c<=5) getch( ); }

Do { Statement; } While (text-expression);

Q.2:- What is the purpose of if-else statement? Ans:IF-ELSE STATEMENT

An if statement tests a particular condition if the condition evaluates to true a course of action is followed I.e. statements or set of statements is executed. If the (Boolean expression) {// statements (s) will execute if the Boolean expression is true} else {// statements (s) will execute if the Boolean expression is false}

If condition is false
else code

If_code

Decision making structure require that the programmer specify one or more conditions to be evaluated or tested by the program, along with a statement or statement to be executed if the condition is determined to be true and optionally other statement to be executed if the condition is determined to be false. Following is the general from of typical decision making structure found in most of the programming languages. If statement an if statement consists of a Boolean expression followed by one or more statements.

If else statements an if statement can be followed by an optional else statement, which execute when the Boolean expression is false.

Q.3:-Compare the use of the if-else statement with the use of the conditional operator . In particular, in what way can the conditional operator be used in place of an if-else statement?Ans:An if statements a particular condition; if the condition evaluates to true, a course- of -action is followed i.e. a statement or set of statements is executed. Otherwise (if the condition evaluates to false), the course-of-action is ignored. The syntax (general form) of the if statement is as shown below: if

(expression)

Statement;

What if there is another course of action to be followed if the expression evaluates of false. There is another form of if that allows for this kind of either-or condition by providing an else clause. The syntax of the if-else statement is the following: if

(expression)

Statement1; Statement2;

else

If the expression evaluates to true i.e. nonzero value, the statement-1 is executed, otherwise, statement-2 is executed. The statement-1 and statement-2 can be a single statement, or a compound statement, or a null statement. C++ =offers a conditional operator(?:) that stores a value depending upon a condition. This operator is ternary operator i.e., it requires three operands. The general form of conditional operator?: Is as follows:

expression1 ? expression2: expression3

if expression1 evaluates to true i.e. 1, then the value of the whole expression is the value of expression2, otherwise, the value of the whole expression is the value of expression3. For instance result=marks >=50 ? p : f ; The identifier result will have value p if the text expression marks>=50 evaluates to true (1), otherwise result will have value f.

Q.3:-What is the purpose of do-while statement? How dose it differ from while statement. Ans:DO WHILE LOOP
Unlike the for and while loops, the do while loop is an exit control loop. It evaluates its test expression of the bottom of loop after executing its loop body statements. This means that a do while loop always execute of least once. But in such cases, the do while loop is the oblivious choice. Int c=1; Do { Cout<<hello<<endl; C=c+1; } While (c<=5) It differ from while statement:In a while loop the text expression beginning of the loop before executing the loop body. It is a entry control loop. A loop control variable should be initialized before the loop beings as an uninitialized variable can be used an expression. The loop variable should be updated inside the body of the while loop. Int c=1; While (c<5) { Cout<<hello<<endl;

C=c+1; }

Q.5:- What is the purpose of for statement? How does it differ from while statement and do while statement? Ans:FOR STATEMENT
The for loop is the easiest to understand of the C++ loops. All its loop-control elements are gathered in one place (on the top of the loop).While in the other loop constructions of C++, they are scattered about the program. The general form (syntax) of the for loop statement is:-

For(initialization(s); text expression; update; body of the loop)

The following example program illustrate the use of for statement. Program using for loop to print numbers form 1 to 10. #include<iostream.h> #include<conio.h> Void main( ) { Int I; Clrscr( ); For(i=1;i<=10;i++) Cout<<\n<<I; Return 0; }

It is differ from while and do while loop:-

In a while loop, where the lop body may be contain a single statement or an empty statement. The loop iterates while the expression evaluates to true. When the expression becomes false, the program control passes to the line after the loop-body code. Int c=1; While (c<5) { Cout<<hello<<endl; C=c+1; } In a do while loop, if the text-expression evaluates to false for the first time itself, the loop is never executed. But in some situations, it is wanted that the loop body is executed at least once, no matter what the initial state of the textexpression is. Int c=1; Do { Cout<<hello<<endl; C=c+1; } While (c<=5)

Das könnte Ihnen auch gefallen