1 / 21

Statements

Statements. A simple statement is a computation terminated by a semicolon. Variable definitions and semicolon-terminated . expressions. C++ Statements represent the lowest-level building blocks of a program and it may be like:. Examples: Int i ; // declaration statement

phuong
Download Presentation

Statements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

  3. if (condition) statement • 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. if Statement • Syntax: If (condition) Statement; if (balance > 0) { interest = balance * creditRate; balance += interest; } statement1; else statement2;

  5. if (condition) statement1 else statement2 If (condition)statement1; else statement2; if (x == 100) cout << "x is 100"; else cout << "x is not 100";

  6. If statement complex

  7. Write a program that check an entered score value using If statement. 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”; }

  8. Writ a program to solves the eqautiona*x*x+ b*x + c=0: # include<iostream.h> # include<math.h> 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 :<<a<<“x^2+”<<b<<“x+”<<c<<“=0”; Double d,x1,x2; D= b*b-4*a*c; If (d<0) { Cout << this equation has no real solution :d<0\n; return 0; } X1=(-b+sqrt(d)/(2*a); X2=(-b-sqrt(d)/(2*a); Cout <<“The solution are :” x1<<“, “<<x2<< endl; }

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

  11. Write a code that take 2 numbers and the operator then find the result value according to the operator type. 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; }

  12. The while loop • Syntax: • While (expression) • statement #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

  13. The do-while loop • 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; }

  14. The while loop Syntax: While (expression) statement #include <iostream> using namespace std; int main () { int n; cout << "Enter the starting number > "; cin >> n; while (n>0) { cout << n << ", "; --n; } cout << "FIRE!\n"; return 0; }

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

  16. EX: THE SUM OF THE FRIST N SQUARES Hint { Int n, sum=0; cout<< “Enter a positive integer:”; cin>> n; for ( inti=1 ; i <=n ; i++) sum+ = i * i ; cout <<“The sum of the frist “<<n<<“squares is ” <<sum << endi ; }

  17. Ex :The sum of 1/2+2/3+……..89/99+99/100 Hint { Int sum=0; for ( inti=1 ; i <=100 ; i++) sum+ = i / (i+1) ; //sum+= (i-1)/i cout <<“The sum of 1/2+2/3+……..89/99+99/100 ” <<sum << endi ; }

  18. Ex :The sum of 1-2+3-4+5-…..nHint { Int n, sg= 1,sum=0; cout<< “Enter a positive integer:”; for ( inti=1 ; i <=n ; i++) sum+ = sg*i ; sg= -1*sg ; cout <<“The sum of 1/2+2/3+……..98/99+99/100 ” <<sum << endi ; }

  19. Arrays • 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];

More Related