370 likes | 791 Views
Flow Of Control. Statements. (I) Statements – Any type of instruction given to computer to perform any kind of action is called Statements.
E N D
Statements • (I) Statements – Any type of instruction given to computer to perform any kind of action is called Statements. • (II) Null statement – It is a useful statement in those where the syntax of the language requires the presence of a statement but where the logic of the program does not. • ; // it is a null statement • (III) Compound Statnement – It is equivalent to the block. It is treated as a single unit and may appear anywhere in the program where a single statement may appear. • (IV) Every statements ends with the ; (semi-colon).
Statement Flow Control • In a program, statements may be executed sequentially, selectively or iteratively. • Sequence – The sequence construct means the statements are being executed sequentially. • Every C++ program begins with the first statement of main ( ). • Statement 1 Statement 2 Statement 3 • In c++, the program execution starts with the first statement of main () and ends with the last statement of main (). Therefore, the main () is also called driver function as it drives the program.
Iteration • A false value is 0 in c++ and non-zero is considered as true in c++. • It means the repetition of a set of statements depending upon a condition test. Till the time a condition is true , a set of construction are repeated again and again as soon as the condition become false the repetition stops. • False (exit) • True • LOOP BODY Condition? Statement 1 Statement 2
Selection Statement • The selection construct means the execution of statements depending upon a condition-test. If a condition is true, a course of action is followed otherwise another course of statement is followed. • True Course of • action • False • Another course-of-action Condition? Statement 1 Statement 2 Statement 1 Statement 2
It is also called conditional statements because they allow choosing the set-of-instruction for execution depending upon an expression’s truth value. • There are two types of selection statements: • Syntax of if statement • If (expression) • Statement; • If statement - An If statement tests a particular condition; if the condition evaluates to true, a course-of-action is followed otherwise another course-of-action is followed. • An integer variable’s truth value when it stores a non-zero value and it is false when it stores a zero.
IF ELSE CONDITION • There is another form of if statement that is if-else statement. • Syntax • If (expression) • Statement 1; • else • statement 2 • Note : if an if else statement , only the code associated with if or code associated with else executes, never both.
Continue • The braces { } are not required if only ONE statement follows if or else. • If you put a semicolon after the test condition, the if statement ends there. The block or statements following are no more part of it in such cases. • i.e. • k=0 ; k=0 ; • if( i==0 ); if( i==0 ) • { { • Cout<<k; cout<<k; • } } • Here semicolon terminates Here if the value i=0 then • the if statement and thus it only it will print value of k doesn’t affect & will print k otherwise not
Nested If If (expression1) { : If (expresson 2) Statement 1; Else Statement 2; } Body if else; If (expression1) { : If (expresson 2) Statement 1; else Statement 2; } else { : If (expresson 2) Statement 1; Else Statement 2; } If (expression1) body of if; else { : If (expresson 2) Statement 1; Else Statement 2; } • A if statement that contain another if statement • In a nested if statement, a dangling else statement goes with the preceding unmatched if statement.
The if-else-ladder • A common programming construct in c++ is the if-else-of ladder, which is often called the if-else-is staircase because of its structure. • General form; • if (exp1)st1; • else • if(exp2)st2; • else • if(exp3)st3; • : : • else st4; • The exp are evaluated from top to bottom. As soon as an exp evaluates to true, the statement associated with it is executed and the rest of the ladder is bypassed. It none exp are true, the final else gets executed. If the final else is missing, no action takes place if all other conditions
Alternate of If • (4) The alternate to if • if(exp1) • exp2 • else (exp3) • can be written as • exp1? exp2: exp3; • it work same as the if statement. • For e.g. • int c; ALTERNATIVE • if (a>b) • c=a; • else int c=a>b?a:b; • c=b;
Comparing if and? • Compare to if-else sequence,?: offer more concise, clean and compact code, but it is less obvious to it. • When ?: operator is used in its nested form, it becomes complex and difficult to understand. • As compare to ?: if is more flexible as it can have multiple statements, multiple assignments and expressions in its body. One equal (=) sign is used to assign a value, but TWO equal (=) sign (I.e., ==) are used to check to see if values are equal to one another.
SWITCH Statement a multi branch selection statement known as switch. Syntax of switch case: Switch(exp) { Case constant 1: st1 ; break; Case constant 2: st2 ; break; Case constant 3: st3 ; break; Case constant 4: st4 ; break; default : st n; } The expression is evaluated and its values are matched against the values of the constant specified in the case statement . when a match is found, the statement sequence associated with that case is executed until the break statement.
A case statement cannot exist by itself, outside of a switch. The break statement used under switch. When a break statement is encounters in a switch statement, program execution jumps to the line of code following the switch statement i.e., outside the body of switch statement. • In example after this slide In 1st block program will execute according to user choice but in 2nd block program the program will print all statements because of missing break statement.
#include<iostream.h> #include<conio.h> Void main() { clrscr(); int dow; cout<<” Enter the no. of week day’s”; cin>>dow; switch(dow) { case 1 : cout<<”\n”<<Sunday”; break; case 2 : cout<<”\n”<<Monday”; break; case 3 : cout<<”Tuesday”; break; case 4 : cout<<”Wednesday”; break; default: cout<<”wrong no. of day”; } getch(); } #include<iostream.h> #include<conio.h> Void main() { clrscr(); int dow; cout<<” Enter the no. of week day’s”; cin>>dow; switch(dow) { case 1 : cout<<”\n”<<Sunday”; case 2 : cout<<”\n”<<Monday”; case 3 : cout<<”Tuesday”; case 4 : cout<<”Wednesday”; default: cout<<”wrong no. of day”; } getch(); }
Switch vs. If-else Switch vs. if else • The switch can only test for equality whereas if can evaluate a relational or logical expression. • Switch statement selects its branch by testing the value of same variable whereas the if-else construction lets you a series of expression that may involve unrelated variables and complex expressions. • The if-else is more versatile for the two statements . • If-else can handle floating-point tests and characters and integer whereas the switch can handle only integer. • Switch case label is more constant than if-else statement. • A switch is more efficient than nested if-else statement.
Iteration statements • It allow a set of instructions to repeatedly until the condition is fulfilled. • Three types of loops: for loop, while loop, do-while loop. • Elemets that control a loop (parts of a loop) • Every loop has its elements that control and govern its execution.
Initialization Expression(s) – before entering a loop, its control variable must be initialized. • The initialization expression(s) is executed only once, in the beginning of the loop. • Test Expression – It is an expression whose truth values decide whether the loop-body will be executed or not. If the test expression evaluates to true , the loop-body gets executed, otherwise the loop is terminated. • Update Expression(s) – The update expression(s) change the value of the loop variable. It is executed at the end of the loop after the loop-body is executed. • The Body-Of-The-Loop – The statements that are executed repeatedly as long as the test-expression is nonzero) form the body of the loop.
The For Loop • The for loop is the easiest to understand of the c++ loops. • Syntax For (initialization expression(s); test-expression; update expression(s)) • {Body of the loop; • } • Explains with help of example • Initialization Test Update • expression expressionexpression • for( i=0 ; i<=10 ; ++i ) • { cout<<”\n”<<i; • }
Initialization exp is executed i.e., i=0 which gives first value 1 to variable i Then, test expression is evaluated i.e., i<=10 which results into true value 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. 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 evaluates till the sequence of step no. 3 executed, otherwise the loop terminates.
for ( a=0; a<=5; a++) cout<<a; In this the loop repeat till the condition is false. And its output is 0 1 2 3 4 5 for (a=0; a<=5; a++); cout<<a; This loop does not repeat because of the termination of the loop by the semi-colon so it will display 5. The for loop variation 1. Multiple initialization and update Expressions – A for loop may contain multiple initialization and/or multiple update expressions. These multiple expressions must be separated by the comma.Example for (i=1, sum=0; i<=n; sum+=i, ++i)cout<<”\n”<<i;☼ The comma operator can serve in a for loop when you need more than one index
2. Prefer prefix Increment/decrement over postfix to be used alone. Use ++i or --i instead of i++ or i -- because when these used alone, prefix are faster than postfix.1 for ( a=0; a<=5; ++a) rather than, prefer 1 than 2.2 for ( a=0; a<=5; a++) 3. Optional Expressions – The loop-control expressions in a for loop statement are optional.For example;a=0; for (; a<=5; a++) in this initialization-exp skiped because the early initialization of an i.e., a=0.4. Infinite loop – An infinite for loop can be created by omitting the test expression.for (a=35; ; --a) 5. Empty loop – if a for loop doesn’t contain any loop body than it is an empty loop.6. Declaraction of variables in the loop – variable cane declare anywhere in the program.
The While loop & The Do-While LOOp it is an entry-controlled loop.the syntax of while loop isWhile (expression) Loop-bodyIn a while loop, a loop control variable should be initialized before the loop begins as an uninitialized variable can be used in an expression. The variable should be updated inside the body-of-the-while.For example: WAP to calculate the factorial of an integer.variation in a while loop A while loop can be an empty loop or an infinite loop.The do-while loop- it is an exit-controlled i.e., it evaluates its test-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.syntaxdo{statements;} while (test-expression);
it unconditionally transfer program control within a function. C++ has four statements that perform an unconditional branch: return,goto,break and continue. The goto statement – it can transfer the program control anywhere in the program. The target destination of a goto statement is marked by a label. syntax goto label; : label: for example a=0; start: cout<<”\n”<<++a; if (a<50) goto start; it will print no. from 1 to 50. If a label appears just before a closing brace, a null statement must follow the label. THE JUMP STATEMENTS
while (test exp) { statement1; if(val>2000) break : statement2; } statement3; for (int ;expression; update) { statement1; if(val>2000) break; statement2; } statement3; do { statement 1; if(val>2000) break; statement2; } while(test expression); statement3; The break statement- it enables a program to skip over part of the code. A break statement terminates the smallest enclosing while, do-while, for or switch statements.A break statement skips the rest of the loop and jumps over to the statement following the loop.A break used in a switch statement will affect only that switch i.e., It will terminate only the very switch it appears in. It does not affect any loop the switch happens to be in.
while (test exp) { statement1; if(val>2000) continue; : statement2; } statement3; for (int ;expression; update) { statement1; if(val>2000) continue; statement2; } statement3; do { statement 1; if(val>2000) continue; statement2; } while(test expression); statement3; The continue statement – instead of forcing termination, it forces the next iteration of the loop to take place, skipping any code in between. The continue statement skips the rest of the loop statements and causes the next iteration of the loop.
The exit function – it causes the program to terminate as soon as it is encountered, no matter where it appears in the program listing. the exit function has been defined under a header file process.h which be included in a program that uses exit() function.