157x Filetype PDF File size 0.99 MB Source: keerthicomputerstudymaterials.files.wordpress.com
Chapter10-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–elsestatement o Nested–ifstatement o switch statement ifstatement: 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) // This Condition is true 1 | P a g e Chapter10-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#include void main( ) { int a, b, c; int largest, smallest, seclargest; clrscr( ); cout<<”Enter the three numbers”< >a>>b>>c; largest = a; //Assume first number as largest if(b>largest) largest = b; if(c>largest) largest = c; smallest = a; //Assume first number as smallest if(b #include void main( ) { float TAmount, discount, FAmount ; clrscr( ); cout<<”Enter the Total Amount”< >TAmount; discount = 0; //Calculate Discount if(TAmount>1000) Discount = (8/100) * TAmount; FAmount = TAmount–Discount //Calculate Final Amount cout<<”Toatal Amount = “< #include void main( ) { int year ; clrscr( ); cout<<”Enter the Year in the form YYYY”< >year; if(year%4 ==0 && year%100!=0 || year%400 ==0) cout< #include void main( ) { char ch ; clrscr( ); cout<<”Enter the Character”< >ch; if(ch>= A && ch <=Z) cout< = a && ch <=z) cout<
no reviews yet
Please Login to review.