+ All Categories
Home > Documents > C++ Chapter II

C++ Chapter II

Date post: 12-Jul-2015
Category:
Upload: sorn-chanratha
View: 250 times
Download: 0 times
Share this document with a friend
28
Chapter II Lecturer: Hong Mom
Transcript
Page 1: C++ Chapter II

 Chapter  II  

     Lecturer:  Hong  Mom  

Page 2: C++ Chapter II

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    

Page 3: C++ Chapter II

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  

Page 4: C++ Chapter II

Control  Structures  

Statement1  

Statement2  

StatementN  

a.    Sequence   b.  Selec7on  

Exp  

Statement1  

F  T  

Statement2  

c.  Repea7on  

Exp   statement  T  

F  

Page 5: C++ Chapter II

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++  :  

==    ,    !=  ,  <  ,  <=  ,  >,  >=    

Page 6: C++ Chapter II

Logical  Operators  

Logical  Operators:  

 !    :  NOT  

 &&  :  AND  

 ||      :  OR  

Example:    

 Expression                                  Value  

 19<  8                                                                                                                false  

 45  >  41                                                                                                          True  

Page 7: C++ Chapter II

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

}    

Page 8: C++ Chapter II

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    

Page 9: C++ Chapter II

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    

Page 10: C++ Chapter II

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  

Page 11: C++ Chapter II

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

Page 12: C++ Chapter II

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:  

Page 13: C++ Chapter II

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  

Page 14: C++ Chapter II

   

switch  (expression){    case  value  1:        statement  1;  break;    case  value  2:      statement  2;  break;    default:      statements;  

}  

switch  case  mul7-­‐way    decision-­‐making  

Page 15: C++ Chapter II

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

}  

Page 16: C++ Chapter II

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.”;  

}    

Page 17: C++ Chapter II

Itera7on  

•  Itera7on  control  statements  allow  you  to  execute  one  or  more  program  statements  repeatedly.  

•  Itera7on  control  statements:  – while    – do-­‐while  –  for  

Page 18: C++ Chapter II

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  

Page 19: C++ Chapter II

While-­‐  Example  

#include<iostream.h>  void  main(){    int  i  =  1;    while(i<=5){      cout<<”Welcome  CIEDC”  <<  endl;      i++;  

                             }    }  

Page 20: C++ Chapter II

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  

Page 21: C++ Chapter II

Do-­‐While-­‐  Example  

#include<iostream.h>  void  main(){    int  i  =  1;    do{      cout<<”Welcome  CIEDC”  <<  endl;      i++;    }while(i<=5);  

}  

Page 22: C++ Chapter II

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;  

}      

Page 23: C++ Chapter II

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)    

Page 24: C++ Chapter II

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

}

Page 25: C++ Chapter II

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;  

}  

Page 26: C++ Chapter II

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;  

}      

Page 27: C++ Chapter II

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();  

}  

Page 28: C++ Chapter II

Output  screen   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *  

for  statement  –  Example3  


Recommended