Date post: | 12-Jul-2015 |
Category: |
Documents |
Upload: | sorn-chanratha |
View: | 250 times |
Download: | 0 times |
Chapter II
Lecturer: Hong Mom
Objec7ve
• A:er completely this chapter, the students will be able to
• Use control structure • Examine rela7onal and logical operators • Discover how to use the selec7on control structures if ,if ..else and switch
Control Structures
• What is Control Structures? – Used to change the flow of programs a:er a decision is taken.
• A computer can proceed: -‐ In sequence -‐ Selec7vely – making a choice -‐ Repe77vely -‐ looping
Control Structures
Statement1
Statement2
StatementN
a. Sequence b. Selec7on
Exp
Statement1
F T
Statement2
c. Repea7on
Exp statement T
F
Rela7onal Operators A condi7on is represented by a logical (Boolean) expression that can be true (1) or false (0)
Rela7onal operators:
-‐ Allow comparisons
-‐ Require two operands (binary)
-‐ Evaluate to true or false
Rela+onal Operators in C++ :
== , != , < , <= , >, >=
Logical Operators
Logical Operators:
! : NOT
&& : AND
|| : OR
Example:
Expression Value
19< 8 false
45 > 41 True
Control Structures
Compound statement or block : is a group of statements which are separated by semicolons (;), but grouped together in a block enclosed in braces: { }: { statement1; statement2; …
}
If statement
The if statement : is used to execute a statement or block only if a condi7on is sa7sfied. Form: Ex:
if(x == 5) cout << “ x is 5” ;
if (condi7on) statement
if –else statement
if-‐else statement allows us to specify two alterna7ve statements: one which is executed if a condi7on is sa7sfied and one which is executed if the condi7on is not sa7sfied. Form:
Ex: if (x== 5)
cout<<“x is 5”; else
cout << “x is not 5”
if (condi7on ) statement1 else statement2
if .. Else if …else
Syntax: if ( exp1)
statement 1; else if (exp 2)
statement 2; … else
statement n;
if exp1 is sa7sfied, do something. Otherwise, check if exp2 is sa7sfied and if so, do something else. Otherwise (else) , do completely Something else
if .. Else if …else #include <iostream.h> void main(){
int x; cout << “Enter a number”; cin >> x; if (x>10){ cout <<“You have enter number more than 10”; } else{ cout <<“You have entered a very small number”; }
Nested if
if (exp1 ) statement 1;
else{ if (exp2) statement 2; else statement 3;
}
if (exp1){ if (exp2) statement 1; else statement 2;
} else
statement 3;
It has another if in it’s body or in its else’s body or in both Syntax:
Switch case mul7-‐way decision-‐making
• Switch – Useful when variable or expression is tested for mul7ple values
– Test whether an expression number of constant values
– According consists of a series of case labels and an op7onal default case
– break is necessary
switch (expression){ case value 1: statement 1; break; case value 2: statement 2; break; default: statements;
}
switch case mul7-‐way decision-‐making
Switch – Example1
switch(op){ case ‘+’: cout<<“The sum is “ << a+b; break; case ‘-‐’: cout<<“The substract is”<<a-‐b; break; case’*’: cout<<“The product is “ << a*b; break; default: cout <<“\n Wrong operator”;
}
Switch – Example1 switch (grade) {
case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”;
}
Itera7on
• Itera7on control statements allow you to execute one or more program statements repeatedly.
• Itera7on control statements: – while – do-‐while – for
The while statement
• The while statement causes one or more statements to repeat as long as a specified expression remains true. while(expression) { statements; }
condi7on
statement
T
F
While-‐ Example
#include<iostream.h> void main(){ int i = 1; while(i<=5){ cout<<”Welcome CIEDC” << endl; i++;
} }
Do-‐while statement
• This loop is executed at least once. • It is executed 7ll the condi7on remains true.
do{ statements;
}while (expression); condi7on
statement
T F
Do-‐While-‐ Example
#include<iostream.h> void main(){ int i = 1; do{ cout<<”Welcome CIEDC” << endl; i++; }while(i<=5);
}
for statement
• for statement -‐ is used to execute one or more statements a specified number of 7mes. for (ini7al expression; con7nues condi7on; increment\decrement) { statements;
}
for statement
Wrong statement
for (i = 0, i < n, i = i +2 ) // semicolons needed
for (i = 0; i < n) // three parts needed
True statement
for (i=0; i<n; i =i+2)
for statement
• Using a comma-‐separated list of expressions
for ( int num = 2;num <= 20;total += num, num += 2 ) ;
This is equal to :
for ( int num = 2;num <= 20;num += 2 ){
total += num
}
for statement-‐ Example1
//Sum of 1+2+3+….+100 #include<iostream.h> void main(){ int s=0; for(int i=1;i<=100;i++) s = s+i; cout<<”s = ”<<s;
}
for statement-‐ Example2
//Program: S = 2+4+6+….+2*n #include<iostream.h> void main(){
int s=0,n; cout<<”Input n:”; cin>>n; for(int i=1;i<=n;i++) s = s+2*i; cout<<”s = ”<<s;
}
for statement – Example3 //to print * paxern #include<iostream.h> #include<conio.h> void main(){
clrscr(); int i,j; for(i=1; i<10; i++){ for(j=1; j<10-‐i; j++) cout<<‘ ‘; for(j = 1; j<=i; j++) cout<<“*”; } cout<<endl; getch();
}
Output screen * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
for statement – Example3