Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon.

Similar presentations


Presentation on theme: "C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon."— Presentation transcript:

1 C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon. Variable definitions and semicolon-terminated. expressions. Examples: Int i; // declaration statement ++i; // this has a side-effect double d = 10.5;// declaration statement d + 5; // useless statement!

2 Multiple statements can be combined into a compound statement by enclosing them within braces. ;// null statement For example: { int min, i = 10, j = 20; min = (i < j ? i : j); cout << min << '\n'; }

3 Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues

4 Syntax: Syntax: If (condition) Statement; if Statement statement 1 ; else statement 2 ; if (balance > 0) { interest = balance * creditRate; balance += interest; }

5 If (condition) statement 1 ; else statement 2 ; if (x == 100) cout << "x is 100"; else cout << "x is not 100";

6 if (x == 100) { cout << "x is "; cout << x; } if (x == 100) cout << "x is 100"; Else cout << "x is not 100"; if (x > 0) cout << "x is positive"; else if (x < 0) cout << "x is negative"; else cout << "x is 0";

7 Int main( ) { int score; cout<< “Enter the test score :”; cin>> score ; if (score >100) cout<< “Error :score is out of range”; else if(score >= 90) cout <<‘A’ ; else if(score >= 80) cout <<‘B’ ; else if(score >= 70) cout <<‘C’ ; else if(score >= 60) cout <<‘D’ ; else if(score >= 0) cout <<‘F’ ; else cout<<“Error: Score is out of range”; } Write a program that check an entered score value using If statement.

8 Writ a program to solves the eqautiona*x*x+ b*x + c=0: # include main( ) { Float a,b,c; Cout<< “Enter coefficients of quadratic eqaation:”; Cin>> a>>b>>c; If (a== 0){ Cout<< “This is not quadratic equation:a==0’’; return 0; }

9 Cout<< the equation is :< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/2713253/10/slides/slide_8.jpg", "name": "Cout<< the equation is :<

10 Syntax: switch (expression) { case constant1: statements;... case constantn: statements; default: statements; } Its objective is to check several possible constant values for an expression Its objective is to check several possible constant values for an expression.

11 switch exampleif-else equivalent switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; break; default: cout << "value of x unknown"; } if (x == 1) { cout << "x is 1"; } else if (x == 2) { cout << "x is 2"; } else { cout << "value of x unknown"; }

12 switch (operator) { case '+':result = operand1 + operand2; break; case '-':result = operand1 - operand2; break; case 'x': case '*':result = operand1 * operand2; break; case '/': result = operand1 / operand2; break; default:cout << "unknown operator: " << ch << '\n'; break; }

13 Syntax: While (expression) statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

14 Syntax: do statement while (condition); int main () { unsigned long n; do { cout << "Enter number (0 to end): "; cin >> n; cout << "You entered: " << n << "\n"; } while (n != 0); return 0; }

15 Syntax: While (expression) statement #include using namespace std; int main () { int n; cout "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

16 Syntax: initialization increase statement for (initialization; condition; increase) statement ; int main () { for (int n=10; n>0; n--) { cout << n << ", "; } cout << "FIRE!\n"; return 0; }

17 EX: THE SUM OF THE FRIST N SQUARESHint { Int n, sum=0; cout<< “Enter a positive integer:”; cin>> n; for ( int i=1 ; i <=n ; i++) sum+ = i * i ; cout <<“The sum of the frist “< { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/2713253/10/slides/slide_16.jpg", "name": "EX: THE SUM OF THE FRIST N SQUARESHint { Int n, sum=0; cout<< Enter a positive integer: ; cin>> n; for ( int i=1 ; i <=n ; i++) sum+ = i * i ; cout << The sum of the frist <> n; for ( int i=1 ; i <=n ; i++) sum+ = i * i ; cout << The sum of the frist <

18 Hint { Int sum=0; for ( int i=1 ; i <=100 ; i++) sum+ = i / (i+1) ;//sum+= (i-1)/i cout <<“The sum of 1/2+2/3+……..89/99+99/100 ” < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/2713253/10/slides/slide_17.jpg", "name": "Hint { Int sum=0; for ( int i=1 ; i <=100 ; i++) sum+ = i / (i+1) ;//sum+= (i-1)/i cout << The sum of 1/2+2/3+……..89/99+99/100 <

19 { Int n, sg= 1,sum=0; cout<< “Enter a positive integer:”; for ( int i=1 ; i <=n ; i++) sum+ = sg*i ; sg= -1*sg ; cout <<“The sum of 1/2+2/3+……..98/99+99/100 ” < { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/2713253/10/slides/slide_18.jpg", "name": "{ Int n, sg= 1,sum=0; cout<< Enter a positive integer: ; for ( int i=1 ; i <=n ; i++) sum+ = sg*i ; sg= -1*sg ; cout << The sum of 1/2+2/3+……..98/99+99/100 <

20 An array is a series of elements of the same type placed in contiguous memory locations. An array variable is defined by specifying its dimension and the type of its elements. Int heights [10];

21 const int size = 3; double Average (int nums[size]) { double average = 0; for (register i = 0; i < size; ++i) average += nums[i]; return average/size; }


Download ppt "C++ Statements represent the lowest-level building blocks of a program and it may be like:. A simple statement is a computation terminated by a semicolon."

Similar presentations


Ads by Google