Upcoming SlideShare
×

Comp ppt (1)

1,334
-1

Published on

1 Like
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
Your message goes here
• Be the first to comment

Views
Total Views
1,334
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
61
0
Likes
1
Embeds 0
No embeds

No notes for slide

Comp ppt (1)

1. 1. (1.1) INTRODUCTIONGenerally a program executes its statements from beginning tothe end, but all programs dont follow this. This presentation willtell us in detail about the program control statements. (1.2) STATEMENTSStatements are the instructions given to the computer to performany kind of data movements, be it making decisions or repeatingactions. They form the smallest executable unit of the program.They are terminated by a semicolon(;). The different types ofstatements are:-
2. 2. (1.2.1) Compound statements(Block)A block is a group of statements that are enclosed in a pair ofcurly brackets:-{ statement:-1 statement:-2}It may appear anywhere in a program as a single statement of theprogram. A block is many times known as compound statements.
3. 3. (1.3)STATEMENT FLOW CONTROLIn a program, statements may be executed sequentially , selectivelyor iteratively:-Sequence:- This is the default flow of the statements and also known as sequence construct and it also means that the statements are being executed sequentially.Selection:- The selection construct means that the execution of the statements depends on a condition-test, if the condition is true then the statements would be executed else they would be skipped. An else-if statement is a good example of selection statement.
4. 4. Iteration:- The iteration means repetition of a certain set of statements based on a certain condition-test. Looping statements can be used to perform the repetition of a certain set of statements. They are of three types:- for loop, Do-while loop (1.4) SELECTION STATEMENTS and while loop.They allow to choose a certain set of statements for execution only ifa condition is true. There are two types of selection statementsprovided by c++ namely if statement and switch statement. (1.4.1) THE IF STATEMENT OF C++An if statement tests for a particular condition, if the statement istrue then the particular set of statements is executed else they areskipped.
5. 5. The syntax for the if statement is :- If(condition) statement; The statement can either be a single statement or a block. We can also use nested if statements or a ladder of if statements if required in our program. (1.4.2)THE SWITCH STATEMENTC++ provides another type of selection statement i.e. switch statement.The selection statement successively tests the value of an expressionagainst a list of integer or character constants. When a match is found,the statements associated with that constant are executed.
6. 6. The syntax for a switch statement is :-Switch(expression){ case constant 1: statement sequence 1 break; case constant 2: statement sequence 2 break; case constant 3: statement sequence 3 break; case constant n-1: statement sequence n-1; break; default : statement sequence n;}
7. 7. (1.5) ITERATION STATEMENTSIteration statements in c++ allows a certain number instructions tobe performed repeatedly until a certain condition becomes false.Iteration statements are also called loops or looping statements.C++ allows the use of three loops namely:-for loop, while loop, do-while loop.
8. 8. (1.5.1) ELEMNTS THAT CONTROL A LOOPEvery loop has its elements that control and govern the execution.Generally, a loop has 4 elements that have different purposes. Theseelements are as given below:-1. Initialization Expression(s): Before entering the loop the initialization expression(s) initializes the loop control variables with their first values.2. Test Expression:The test expression is that expression whose truth value decideswhether the loop-body will be executed or not. If the value is truethen the body gets executed else it gets terminated.
9. 9. 3. Update expression(s): The update expression(s) change thevalue(s) of loop variable(s). The update expression execute at theend of the loop.4. The Body-of-the-loop: The statements that are to beexecuted form the body-of-the-loop. (1.5.2) THE FOR LOOPThe for loop is the easiest to understand of all because all its loop-control elements are gathered in one place i.e. at the top. Its syntaxis:-For(initialization expression(s);test—expression; update expression) body-of-the-loop;The following slide explains the working of the for loop with an e.g:
10. 10. #include<iostream.h>int main(){ int i; for(i=1;i<=10;i++) cout<<“n”<<i; return 0;} Firstly, initialization expression is executed i.e., i=1 which gives the first value 1 to variable i. Then, the test-expression is executed i.e., i<=10 which results into true i.e., 1. Since, the test expression is true, the body-of-the-loop i.e., cout<<“n”<<i is executed which prints the current value of i on the next line.
11. 11.  After executing the loop-body, the update expression i.e., i++ is executed which increments the value of i. After the update expression is executed, the test-expression is again evaluated. If it is true the sequence is repeated from step no. 3, otherwise the loop terminates. (1.5.3) THE WHILE LOOPThe second loop provided by c++ is the while loop. It is an entrycontrolled loop. The syntax for a while loop is:-while (expression) loop-bodyWhere the loop may contain a single statement, a compoundstatement or an empty statement. The loop iterates while theexpression is true. When the expression becomes false, the programcontrol terminates the loop.
12. 12. In a while loop, a variable should be initialized before the loop asuninitialized variables cannot be used in the loop. The loop variableshould be updated inside the body-of-the-loop. Following exampleexplains the working of a while loop:-int i,fact=1,n;cout<<“Enter a number.”;cin>>n;i=1;while(i<=n){ fact=fact×I; i=i++;}cout<<“The factorial of the num.=“<<fact;
13. 13. The program segment in the previous slide would input a number from the user and print the factorial of the number. (1.5.4) THE DO-WHILE LOOPUnlike the for and while loop do-while loop is an exit controlled loopi.e., it evaluates the test-expression at the bottom of the loop afterthe body-of-the-loop. This means that the do-while loop alwaysexecutes at least once. The syntax for do-while loop is:-do{ statement;}while(test-expression)The brackets { } are not necessary when there is only one statement inthe loop-body.
14. 14. An example for do-while loop is:-int i=1,n=10002;do{ cout<<n; n++; i++;}while(i==1);In the above example the test expression is never true but stillevery time the loop is executed the loop would execute only onceand print “10002”.
15. 15. (1.5.5) NESTED LOOPSC++ allows its users to use a number of loops inside a single loop,hence it means that c++ allows the use of nested loops. A user canuse nested loops according to his own requirement. The followingexample explains the use of nested loops:-for(i=1;i<=4;i++){ cout<<“n”; for(j=1;j<=i;j++) cout<<“*”;}In the above program the inner loop will execute for i number oftimes and i has values 1,2,3,4.
16. 16. The output of the above program segment would be:- * ** *** **** (1.6) JUMP STATEMENTSJump statements unconditionally transfer program control withina function. C++ has four jump statements that perform anunconditional branch: return, goto, break, and continue. Of thesereturn and goto maybe used anywhere in the program but breakand continue can be used only inside brackets { } .
17. 17. (1.6.1) THE GOTO STATEMENTA goto statement can statement can transfer the program controlanywhere in the program. The target destination of a gotostatement is marked by a label. The target label and goto mustappear in the same function. (1.6.2) THE BREAK STATEMENTThe break statement enables the user to skip over a part of theprogram. The break statement is used inside brackets { }. It bringsthe program control directly outside the brackets { }. (1.6.3) THE CONTINUE STATEMENT The continue statement skips a certain part of the code but does not terminate the loop but skips a certain part of the code.
18. 18. (1.6.4) THE EXIT() FUNCTIONThe library function exit() takes u directly outside the program. Itdoes not even stop for the getch() function. It terminates theprogram as soon as it is encountered no matter where it appears.
1. A particular slide catching your eye?

Clipping is a handy way to collect important slides you want to go back to later.