210 likes | 304 Views
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
E N D
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!
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'; }
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
if Statement • Syntax: If (condition) Statement; if (balance > 0) { interest = balance * creditRate; balance += interest; } statement1; else statement2;
if (condition) statement1 else statement2 If (condition)statement1; else statement2; if (x == 100) cout << "x is 100"; else cout << "x is not 100";
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”; }
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; }
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; }
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.
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; }
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; }
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; }
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; }
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; }
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 ; }
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 ; }
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 ; }
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];