•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-effectdouble d = 10.5; // declaration statementd + 5; // useless statement!
•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';
}
• 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
•Syntax:Syntax:If If (condition) (condition) Statement;Statement;
if Statement
statement1;else
statement2;
if (balance > 0) {interest = balance * creditRate;balance += interest;
}
If (condition)statement1;elsestatement2;
if (x == 100) cout << "x is 100";else cout << "x is not 100";
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";
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.
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;}
• Syntax:switch (expression) {
case constant1:statements;
...case constantn:
statements;default:
statements;}
Its objective is to check several possible Its objective is to check several possible constant values for an expressionconstant values for an expression.
switch example if-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"; }
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;}
•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;}
• 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;}
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;
}
• Syntax:
for (initializationinitialization; condition; increase increase) statement 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
HintHint{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<<“squares is ” <<sum << endi ;}
HintHint{Int sum=0;for ( int i=1 ; i <=100 ; i++)sum+ = i / (i+1) ; //sum+= (i-1)/icout <<“The sum of 1/2+2/3+
……..89/99+99/100 ” <<sum << endi ;}
{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 ” <<sum << endi ;}
• 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];
1
2
3
4
5
6
7
8
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;
}
Top Related