Top Banner
•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!
21

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.

Apr 01, 2015

Download

Documents

Sky Mowell
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 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.

•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!

Page 2: 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.

•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';

}

Page 3: 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.

• 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

Page 4: 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.

•Syntax:Syntax:If If (condition) (condition) Statement;Statement;

if Statement

statement1;else

statement2;

if (balance > 0) {interest = balance * creditRate;balance += interest;

}

Page 5: 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.

If (condition)statement1;elsestatement2;

if (x == 100) cout << "x is 100";else cout << "x is not 100";

Page 6: 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.

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";

Page 7: 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.

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.

Page 8: 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.

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;}

Page 9: 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.

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;}

Page 10: 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.

• 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.

Page 11: 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.

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"; }

Page 12: 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.

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;}

Page 13: 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.

•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;}

Page 14: 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.

• 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;}

Page 15: 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.

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;

}

Page 16: 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.

• Syntax:

for (initializationinitialization; condition; increase increase) statement statement;

int main (){

for (int n=10; n>0; n--) {

cout << n << ", "; }

cout << "FIRE!\n"; return 0;

}

Page 17: 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.

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 ;}

Page 18: 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.

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 ;}

Page 19: 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.

{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 ;}

Page 20: 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.

• 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];

Page 21: 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.

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;

}