Page 1 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Chapter-10, CONTROL STATEMENTS, , , Introduction, , , , Control statements are statements that alter the sequence of flow of instructions., , , , Any single input statement, assignment and output statement is simple statement., , , , A group of statement that are separated by semicolon and enclosed within curled braces { and } is, called a block or compound statement., , , , , , , The order in which statements are executed in a program is called flow of control., , Types of control statements:, C++ supports two basic control statements., o Selection statements, o Iteration statements, , , , , Selection Statements:, This statement allows us to select a statement or set of statements for execution based on some, condition., , , , It is also known as conditional statement., , , , This structure helps the programmer to take appropriate decision., , , , The different selection statements, viz., o if statement, o if – else statement, o Nested – if statement, o switch statement, , if statement :, , , This is the simplest form of if statement., , , , This statement is also called as one-way branching., , , , This statement is used to decide whether a statement or set of statements should be executed or, not., , , , The decision is based on a condition which can be evaluated to TRUE or FALSE., , , , The general form of simple – if statement is:, if (Test Condition), , 1|Page, , // This Condition is true
Page 2 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Statement 1;, Statement 2;, , , Here, the test condition is tested which results in either a TRUE or, FALSE value. If the result of the test condition is TRUE then the, Statement 1 is executed. Otherwise, Statement 2 is executed., , Ex:, , if( amount > = 5000 ), discount = amount * (10/100);, net-amount = amount – discount;, , Practical Program 5: Write a C++ program to find the largest, smallest, and second largest of three numbers using simple if statement., #include<iostream.h>, #include<conio.h>, void main( ), {, int a, b, c;, int largest, smallest, seclargest;, clrscr( );, cout<<”Enter the three numbers”<<endl;, cin>>a>>b>>c;, largest = a;, if(b>largest), largest = b;, if(c>largest), largest = c;, smallest = a;, if(b<smallest), smallest = b;, if(c<smallest), smallest = c;, , //Assume first number as largest, , //Assume first number as smallest, , seclargest = (a + b + c) – (largest + smallest);, cout<<”Smallest Number is = “<<smallest<<endl;, cout<<”Second Largest Number is = “<<seclargest<<endl;, cout<<”Largest Number is = “<< largest<<endl;, getch( );, }, Practical Program 6: Write a C++ program to input the total amount in a bill, if the amount is greater, than 1000, a discount of 8% is given. Otherwise, no discount is given. Output the total amount,, discount and the final amount. Use simple if statement., 2|Page
Page 3 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , #include<iostream.h>, #include<conio.h>, void main( ), {, float TAmount, discount, FAmount ;, clrscr( );, cout<<”Enter the Total Amount”<<endl;, cin>>TAmount;, discount = 0;, if(TAmount>1000), Discount = (8/100) * TAmount;, , //Calculate Discount, , FAmount = TAmount – Discount, , //Calculate Final Amount, , cout<<”Toatal Amount = “<<TAmount<<endl;, cout<<”Discount = “<<discount<<endl;, cout<<”Final Amount = “<< FAmount<<endl;, getch( );, }, if – else statement :, , , This structure helps to decide whether a set of statements should be executed or another set of, statements should be executed., , , , This statement is also called as two-way branching., , , , The general form of if – else statement is:, if (Test Condition), Statement 1;, else, Statement 2;, , , , Here, the test condition is tested., , If the test-, , condition is TRUE, statement-1 is executed., Otherwise Statement 2 is executed., Ex:, , if( n % 2 == 0 ), cout<<” Number is Even”;, else, cout<<” Number is Odd”;, , Practical Program 7: Write a C++ program to check whether a given year is a leap year not, Using if, - else statement., 3|Page
Page 4 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , #include<iostream.h>, #include<conio.h>, void main( ), {, int year ;, clrscr( );, cout<<”Enter the Year in the form YYYY”<<endl;, cin>>year;, if(year%4 ==0 && year%100!=0 || year%400 ==0), cout<<year<<” is a leap year”<<endl;, else, cout<<year<<” is not leap year”<<endl;, getch( );, }, Practical Program 8: Write a C++ program to accept a character. Determine whether the character is, a lower-case or upper-case letter., #include<iostream.h>, #include<conio.h>, void main( ), {, char ch ;, clrscr( );, cout<<”Enter the Character”<<endl;, cin>>ch;, if(ch>= ‘A’ && ch <=’Z’), cout<<ch<<” is an Upper-Case Character”<<endl;, else, if(ch>= ‘a’ && ch <=’z’), cout<<ch<<” is an Lower-Case Character”<<endl;, else, cout<<ch<<” is not an alphabet”<<endl;, getch( );, }, Nested if statement :, , , If the statement of an if statement is another if statement then such an if statement is called as, Nested-if Statement., , , , Nested-if statement contains an if statement within another if statement., , , , There are two forms of nested if statements., , if – else - if statement :, , , This structure helps the programmer to decide the execution of a statement from multiple, statements based on a condition., , 4|Page
Page 5 :
Chapter 10- Control Statements, , , , There will be more than one condition to test., , , , This statement is also called as multiple-way branch., , , , The general form of if – else – if statement is:, , I PUC, MDRPUC, Hassan, , if (Test Condition 1), , else, , , , Example:, Statement 1;, if( marks > = 85 ), PRINT “Distinction”, else, if (Test Condition 2), if( marks > = 60 ), Statement 2;, PRINT “First Class”, else, else, if( marks > = 50 ), ……….., PRINT “Second Class”, else, else, if( marks > = 35 ), if( test Condition N), PRINT “Pass”, Statement N;, else, else, PRINT “Fail”, Default Statement;, , Here, Condition 1 is tested. If it is TRUE, Statement 1 is executed control transferred out of the, structure. Otherwise, Condition 2 is tested. If it is TRUE, Statement 2 is executed control is, transferred out of the structure and so on., , , , If none of the condition is satisfied, a statement called default statement is executed., , 5|Page
Page 6 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Practical Program 9: Write a C++ program to input the number of units of electricity consumed in a, house and calculate the final amount using nested-if statement. Use the following data for calculation., Units consumed, < 30, >= 30 and < 50, >= 50 and < 100, >= 100, , Cost, Rs. 3.50 / Unit, Rs. 4.25 / Unit, Rs. 5.25 / Unit, Rs. 5.85 / Unit, , #include<iostream.h>, #include<conio.h>, void main( ), {, int units ;, float Billamount;, clrscr( );, cout<<”Enter the number of units consumed”<<endl;, cin>>units;, if(units < 30), Billamount = units * 3.50 ;, else, if(units < 50), Billamount = 29 * 3.50 + (units – 29) * 4.25 ;, else, if(units < 100), Billamount = 29 * 3.50 + 20 * 4.25 + (units – 49) * 5.25 ;, else, Billamount = 29 * 3.50 + 20 * 4.25 + 50 * 5.25 + (units – 99) * 5.85 ;, cout<<”Total Units Consumed =”<<units<<endl;, cout<<”Toatl Amount = “<<Billamount<<endl;, getch( );, }, , , The general form of if – else -if statement is: Ex: To find the greatest of three numbers a, b and c., if ( a>b ), if (Test Condition 1), if ( a > c ), if (Test Condition 2), OUTPUT a, Statement 1;, else, else, OUTPUT c, Statement 2;, else, else, if ( b > c ), if (Test Condition 3), OUTPUT b, Statement 3;, else, else, OUTPUT c, Statement 4;, , 6|Page
Page 7 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Switch Statement :, , , C++ has built in multiple-branch selection statement i.e. switch., , , , If there are more than two alternatives to be selected, multiple selection construct is used., , , , The general form of Switch statement is:, Switch ( Expression ), {, Case Label-1:, , Statement 1;, Break;, , Case Label-2:, , Statement 1;, Break;, ………….., , Case Label-N:, , Statement N;, Break;, , Default, }, , 7|Page, , :, , Default- Statement;
Page 8 :
Chapter 10- Control Statements, , , , I PUC, MDRPUC, Hassan, , Ex: To find the name of the day given the day number, switch ( dayno ), {, Case 1:, , cout<< “Sunday”<<endl;, break;, , Case 2:, , cout<< “Monday” <<endl;, break;, , Case 3:, , cout<< “Tuesday” <<endl;, break;, , Case 4:, , cout<< “Wednesday” <<endl;, break;, , Case 5:, , cout<< “Thursday” <<endl;, break;, , Case 6:, , cout<< “Friday” <<endl;, break;, , Case 7:, , cout<< “Saturday” <<endl;, break;, , default:, , cout<< “Invalid Day Number” <<endl;, , }, , , The switch statement is a bit peculiar within the C++ language because it uses labels instead of, blocks., , , , This force up to put break statements after the group of statements that we want to execute for a, specific condition., , , , Otherwise the remainder statements including those corresponding to other labels also are, executed until the end of the switch selective block or a break statement is reached., , Practical Program 10: Write a C++ program to input the marks of four subjects. Calculate the total, percentage and output the result as either “First Class” or “Second Class” or “Pass Class” or “Fail”, using switch statement., Class, First Class, Second Class, Pass Class, Fail, #include<iostream.h>, #include<conio.h>, 8|Page, , Range (%), Between 60% to 100%, Between 50% to 59%, Between 40% to 49%, Less than 40%
Page 10 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , o while loop, o do while loop, o for loop, while loop:, , , This is a pre-tested loop structure., , , , This structure checks the condition at the beginning of the structure., , , , The set of statements are executed again and again until the condition is true., , , , When the condition becomes false, control is transferred out of the structure., , , , The general form of while structure is, while ( Test Condition), {, Statement 1, Statement 2, …….., Statement N, }End of While, , , , Example:, n = 10;, While ( n > 0), {, cout<<n<<”\t”;, - - n;, }, cout<<”End of while loop \n”;, Output: 10 9 8 7 6 5 4 3 2 1 End of while loop, , Practical Program 11: Write a C++ program to find sum of all the digits of a number using while, statement., #include<iostream.h>, #include<conio.h>, void main( ), {, int num, sum, rem;, clrscr( );, cout<<”Enter the Number”<<endl;, cin>>num;, sum = 0;, , 10 | P a g e
Page 12 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , cin>>num;, m = num;, flag = 1;, while(num>2), if(num % 2 ==1), {, flag = 0;, break;, }, else, num = num/2;, if(flag), cout<<m<<” is power of 2 “<<endl;, else, cout<<m<<” is not power of 2 “<<endl;, getch( );, }, do while statements:, , , This is a post-tested loop structure., , , , This structure checks the condition at the end of the structure., , , , The set of statements are executed again and again until the condition is true., , , , When the condition becomes false, control is transferred out of the structure., , , , The general form of while structure is, do, {, Statement 1, Statement 2, …….., Statement N, } while ( Test Condition);, , , , Example:, i = 2;, do, {, cout<<i<<”\t”;, i = i + 2;, }, while ( i < = 25);, , 12 | P a g e
Page 13 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Practical Program 14: Write a C++ program to check whether the given number is an Armstrong, Number using do-while statement. (Hint: 153 = 13 + 53 + 33), #include<iostream.h>, #include<conio.h>, void main( ), {, int num, rem, sum, temp;, clrscr( );, cout<<”Enter the three digit number”<<endl;, cin>>num;, temp = num;, sum = 0;, do, {, rem = temp % 10;, sum = sum + rem * rem * rem;, temp = temp / 10;, }while(temp!=0);, if(sum == num), cout<<num<<” is an Armstrong Number “<<endl;, else, cout<<num<<” is not an Armstrong Number “<<endl;, getch( );, }, Difference between while and do while loop:, while, , do while, , This is pre- tested looping structure, , This is post tested looping structure, , It tests the given condition at initial point, , It tests the given condition at the last of, , of looping structure, , looping structure., , Minimum execution of loop is zero, , Minimum execution of loop is once., , Syntax:, while ( Test condition ), , Syntax:, , {, , {, , statement 1;, , statement 1;, , statement 2;, , statement 2;, , …………….;, , statement n;, , statement n;, }, Semi colon is not used., 13 | P a g e, , do, , }, while ( Test condition);, Semi colon is used.
Page 14 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , for statement:, , , This structure is the fixed execution structure., , , , This structure is usually used when we know in advance exactly how many times asset of, statements is to be repeatedly executed again and again., , , , This structure can be used as increment looping or decrement looping structure., , , , The general form of for structure is as follows:, for ( Expression 1; Expression 2; Expression 3), {, Statement 1;, Statement 2;, Statement N;, }, Where, Expression 1 represents Initialization, Expression 2 represents Condition, Expression 3 represents Increment/Decrement, , , , Example:, sum = 0;, for ( i=1; i<=10; i++), sum = sum + i;, , , , This looping structure works as follows:, o Initialization is executed. Generally it is an initial value setting for a counter variable and is, executed only one time., o Condition is checked. if it is TRUE the loop continues, otherwise the loop ends and control, exists from structure., o Statement is executed as usual, is can either a single statement or a block enclosed in curled, braces { and }., o At last, whatever is specified in the increase field is executed and the loop gets back to, executed step 2., o The initialization and increase fields are optional. They can remain empty, but in all cases the, semicolon sign between them must be written compulsorily., o Optionally, using the comma operator we can specify more than one expression in any of the, fields included in a for loop., , 14 | P a g e
Page 15 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Practical Program 15: Write a C++ program to find the factorial of a number using for statement., (Hint: 5! = 5 * 4 * 3 * 2 * 1 = 120), #include<iostream.h>, #include<conio.h>, void main( ), {, int num, fact, i;, clrscr( );, cout<<”Enter the number”<<endl;, cin>>num;, fact = 1;, for( i = 1; i<= num; i++), fact = fact * i;, cout<<” The factorial of a ”<<num<<”! is: “<<fact<<endl;, getch( );, }, Practical Program 16: Write a C++ program to generate the Fibonacci sequence up to a limit using, for statement. (Hint: 5 = 0, , 1, , 1, , 2, , 3), , #include<iostream.h>, #include<conio.h>, void main( ), {, int num, first, second, count, third;, clrscr( );, cout<<”Enter the number limit for Fibonacci Series”<<endl;, cin>>num;, first = 0;, second = 1;, cout<<first<<”\t”<<second;, third = first + second;, for( count = 2; third<=num; count++), {, cout<<”\t”<<third, first = second;, second = third;, third = first + second;, }, cout<<”\n Total terms = “<<count<<endl;, getch( );, }, , 15 | P a g e
Page 16 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , Jump statements or Transfer of control from within loop, , , Transfer of control within looping are used to, o Terminate the execution of loop., o Exiting a loop., o Half way through to skip the loop., , , , All these can be done by using break, exit and continue statements., , break statement, , , The break statement has two uses, o You can use it to terminate a case in the switch statement., o You can also use it to force immediate termination of a loop like while, do-while and for, by, passing the normal loop conditional test., , , , When the break statement is encountered inside a loop, the loop is immediately terminated and, program control resumes at the next statement., , , , The general form of break statement is:, break;, , , , Example:, , for (n=0; n<100; n++), {, cout<<n;, if(n==10) break;, }, Program: To test whether a given number is prime or not using break statement., #include<iostream.h>, #include,conio.h>, void main( ), {, int n, i, status;, clrscr( );, cout<<”Enter the number”;, cin>>n;, status=1;, for(i=2; i<=n/2; i++), {, if(n % i ==0), {, status=0, cout<<”It is not a prime”<<endl;, break;, 16 | P a g e
Page 17 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , }, }, if(status), cout<<”It is a prime number”<<endl;, getch( );, }, exit( ) function:, , , This function causes immediate termination of the entire program, forcing a return to the operating, system., , , , In effect, exit( ) function acts as if it were breaking out of the entire program., , , , The general form of the exit( ) function is:, exit( );, , or, , void exit( int return_code);, , , , The return code is used by the operating system and may be used by calling programs., , , , An exit code of 0 means that the program finished normally and any other value means that some, error or unexpected results happened., , Program: To test whether a given number is prime or not using exit( ) statement., #include<iostream.h>, #include,conio.h>, void main( ), {, int n, i;, clrscr( );, cout<<”Enter the number”;, cin>>n;, for(i=2; i<=n/2; i++), {, if(n % i ==0), {, cout<<”It is not a prime”<<endl;, exit(0);, }, }, cout<<”It is a prime number”<<endl;, getch( );, }, continue statement:, , , The continue statement causes the program to skip the rest of the loop in the current iteration as if, end of the statement block has been reached, causing it to jump to start of the following iteration., , , , The continue statement works somewhat like break statement., , 17 | P a g e
Page 18 :
Chapter 10- Control Statements, , , , I PUC, MDRPUC, Hassan, , Instead of forcing termination, however continue forces the next iteration of the loop to take place,, skipping any code in between., , , , The general form of the continue statement is:, continue;, , , , Example:, for (n=10; n<0; n--), {, if(n==10) continue;, cout<<n;, }, goto statement:, , , The goto allows to makes an absolute jump to another point in the program., , , , This statement execution causes an unconditional jump or transfer of control from one statement to, the other statement with in a program ignoring any type of nesting limitations., , , , The destination is identified by a label, which is then used as an argument for the goto statement., , , , A label is made of a valid identifier followed by a colon (:)., , , , The general form of goto statement is:, statement1;, statement2;, goto label_name;, statement 3;, statement4;, label_name: statement5;, statement6;, , Program: To print from 10 to 1 using goto statements., #include<iostream.h>, #include<conio.h>, #include<iomanip.h>, void main( ), {, int n=10;, loop: cout<<”\t”<<n;, n--;, if(n>0) goto loop;, cout<<”End of loop”;, getch( );, }, , 18 | P a g e
Page 19 :
Chapter 10- Control Statements, , I PUC, MDRPUC, Hassan, , CHAPTER 10 – CONTROL STATEMENTS BLUE PRINT, VSA (1 marks), , SA (2 marks), , LA (3 Marks), , Essay (5 Marks), , Total, , 01 Question, , -, , -, , 02 Question, , 11 Marks, , **************, , 19 | P a g e