SlideShare a Scribd company logo
1 of 28
Download to read offline
 
                    	
  
        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	
  
a.	
  	
  Sequence	
          b.	
  Selec7on	
                       c.	
  Repea7on	
  


  Statement1	
                T	
                      F	
  
                                          Exp	
                                         T	
  
                                                                           Exp	
                statement	
  

  Statement2	
                                                                  F	
  
                         Statement1	
               Statement2	
  




  StatementN	
  
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:	
  
	
  
                                 if	
  (condi7on)	
  statement	
  
	
                               	
  
	
  
Ex:	
  
     	
     	
  if(x	
  ==	
  5)	
  
     	
     	
  	
  	
  	
  	
  	
  	
  cout	
  <<	
  “	
  x	
  is	
  5”	
  ;	
  	
  	
  
	
  
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:	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  
                                                          if	
  (condi7on	
  )	
  statement1	
  	
  
              	
   	
   	
   else	
  	
  s	
  tatement2	
      	
  
	
                                                        	
  
Ex:	
  
	
  	
  if	
  (x==	
  5)	
  
               	
   	
  cout<<“x	
  is	
  5”;	
  
	
  	
  else	
  
                  	
                    	
  cout	
  <<	
  “x	
  is	
  not	
  5”                                                                                           	
  	
  
                                                                                                                                                                                                                                                	
  
if	
  ..	
  Else	
  if	
  …else	
  
Syntax:	
  
if	
  (	
  exp1)	
                       if	
  exp1	
  is	
  sa7sfied,	
  do	
  
         	
  statement	
  1;	
           something.	
  
else	
  if	
  (exp	
  2)	
               Otherwise,	
  check	
  if	
  
                                         exp2	
  is	
  sa7sfied	
  and	
  if	
  so,	
  do	
  
         	
  statement	
  2;	
           something	
  else.	
  
…	
                                      	
  Otherwise	
  (else)	
  ,	
  do	
  
else	
                                   completely	
  	
  
                                         Something	
  else	
  
         	
  statement	
  n;	
  
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	
  
	
   has	
  another	
  if	
  in	
  it’s	
  	
  body	
  or	
  in	
  its	
  else’s	
  body	
  or	
  in	
  
It	
  
	
  
both	
  
Syntax:	
  
	
  
   if	
  (exp1){              	
  	
  
                                                               if	
  (exp1	
  )	
  
             	
  if	
  (exp2)	
  
                                                                          	
  statement	
  1;	
  
             	
  statement	
  1;	
                             else{	
  
             	
  else	
  	
                                               	
  if	
  (exp2)	
  
             	
  statement	
  2;	
                                        	
  statement	
  2;	
  
   }	
                                                                    	
  else	
  
   else	
                                                                 	
  statement	
  3;	
  
             	
  statement	
  3;	
                             }	
  
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	
  case	
  mul7-­‐way	
  	
  
                          	
  
               decision-­‐making	
  
switch	
  (expression){	
  
      	
  case	
  value	
  1:	
  	
  
      	
   	
  statement	
  1;	
  break;	
  
      	
  case	
  value	
  2:	
  
      	
   	
  statement	
  2;	
  break;	
  
      	
  default:	
  
      	
   	
  statements;	
  
}	
  
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)	
  
   	
  {	
                                                  F	
  
                                         condi7on	
  
   	
   	
  statements;	
  
                                          T	
  
   	
  }	
  
                                            statement	
  
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;	
                          statement	
  


}while	
  (expression);	
   T	
                               F	
  
                                              condi7on	
  
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;	
  
incrementdecrement)	
  
{	
  
                 	
  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();	
  
}	
  
for	
  statement	
  –	
  Example3	
  
  Output	
  screen	
  
                                  *
                                 * *
                                * * *
                               * * * *
                              * * * * *
                             * * * * * *
                            * * * * * * *
                           * * * * * * * *
                          * * * * * * * * *
                         * * * * * * * * * *	
  

More Related Content

What's hot

nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++guestfb6ada
 
08 advanced techniques
08 advanced techniques08 advanced techniques
08 advanced techniquesgoodhackers
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inTIB Academy
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systemsMitul Tank
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and VariablesSyed Afaq Shah MACS CP
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionKent Huang
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)Chris Huang
 
Csharp In Detail Part1
Csharp In Detail Part1Csharp In Detail Part1
Csharp In Detail Part1Mohamed Krar
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable codeGeshan Manandhar
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARDTia Ricci
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 

What's hot (18)

nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++
 
08 advanced techniques
08 advanced techniques08 advanced techniques
08 advanced techniques
 
Lecture04
Lecture04Lecture04
Lecture04
 
Tdd.eng.ver
Tdd.eng.verTdd.eng.ver
Tdd.eng.ver
 
C C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.inC C++ tutorial for beginners- tibacademy.in
C C++ tutorial for beginners- tibacademy.in
 
Session 3
Session 3Session 3
Session 3
 
Embedded systems
Embedded systemsEmbedded systems
Embedded systems
 
Lecture 3 Conditionals, expressions and Variables
Lecture 3   Conditionals, expressions and VariablesLecture 3   Conditionals, expressions and Variables
Lecture 3 Conditionals, expressions and Variables
 
Clean Code: Chapter 3 Function
Clean Code: Chapter 3 FunctionClean Code: Chapter 3 Function
Clean Code: Chapter 3 Function
 
重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)重構—改善既有程式的設計(chapter 9)
重構—改善既有程式的設計(chapter 9)
 
Csharp In Detail Part1
Csharp In Detail Part1Csharp In Detail Part1
Csharp In Detail Part1
 
C reference card
C reference cardC reference card
C reference card
 
Final Exam in FNDPRG
Final Exam in FNDPRGFinal Exam in FNDPRG
Final Exam in FNDPRG
 
7 rules of simple and maintainable code
7 rules of simple and maintainable code7 rules of simple and maintainable code
7 rules of simple and maintainable code
 
C Reference Card (Ansi) 2
C Reference Card (Ansi) 2C Reference Card (Ansi) 2
C Reference Card (Ansi) 2
 
ANSI C REFERENCE CARD
ANSI C REFERENCE CARDANSI C REFERENCE CARD
ANSI C REFERENCE CARD
 
Pl sql programme
Pl sql programmePl sql programme
Pl sql programme
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 

Similar to C++ Chapter II

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional StatementDeepak Singh
 
C statements
C statementsC statements
C statementsAhsann111
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJTANUJ ⠀
 
Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perlsana mateen
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__elseeShikshak
 
Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuressana mateen
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c languageshhanks
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language samt7
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statementsmaznabili
 
CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23Bilal Ahmed
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming LanguageAhmad Idrees
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Neeru Mittal
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection StatementsSzeChingChen
 

Similar to C++ Chapter II (20)

Chapter 8 - Conditional Statement
Chapter 8 - Conditional StatementChapter 8 - Conditional Statement
Chapter 8 - Conditional Statement
 
C statements
C statementsC statements
C statements
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
Scalar expressions and control structures in perl
Scalar expressions and control structures in perlScalar expressions and control structures in perl
Scalar expressions and control structures in perl
 
Mesics lecture 6 control statement = if -else if__else
Mesics lecture 6   control statement = if -else if__elseMesics lecture 6   control statement = if -else if__else
Mesics lecture 6 control statement = if -else if__else
 
Unit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structuresUnit 1-scalar expressions and control structures
Unit 1-scalar expressions and control structures
 
operators and control statements in c language
operators and control statements in c languageoperators and control statements in c language
operators and control statements in c language
 
Overview of c++ language
Overview of c++ language   Overview of c++ language
Overview of c++ language
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23CS101- Introduction to Computing- Lecture 23
CS101- Introduction to Computing- Lecture 23
 
Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Javascript
JavascriptJavascript
Javascript
 
Ch05.pdf
Ch05.pdfCh05.pdf
Ch05.pdf
 
C++ chapter 4
C++ chapter 4C++ chapter 4
C++ chapter 4
 
Introduction to Selection control structures in C++
Introduction to Selection control structures in C++ Introduction to Selection control structures in C++
Introduction to Selection control structures in C++
 
Ch5 Selection Statements
Ch5 Selection StatementsCh5 Selection Statements
Ch5 Selection Statements
 
What is c
What is cWhat is c
What is c
 
C fundamental
C fundamentalC fundamental
C fundamental
 
Python unit 3 and Unit 4
Python unit 3 and Unit 4Python unit 3 and Unit 4
Python unit 3 and Unit 4
 
Iteration
IterationIteration
Iteration
 

More from Sorn Chanratha

Training of trainer on Web2.0
Training of trainer on Web2.0Training of trainer on Web2.0
Training of trainer on Web2.0Sorn Chanratha
 
Learn to write Khmer Poem
Learn to write Khmer PoemLearn to write Khmer Poem
Learn to write Khmer PoemSorn Chanratha
 
រឿងប្រលោមលោក ស្នាមញញឹម
រឿងប្រលោមលោក ស្នាមញញឹមរឿងប្រលោមលោក ស្នាមញញឹម
រឿងប្រលោមលោក ស្នាមញញឹមSorn Chanratha
 
របាយការណ៍សិទ្ធិមនុស្ស របស់ Un
របាយការណ៍សិទ្ធិមនុស្ស របស់ Unរបាយការណ៍សិទ្ធិមនុស្ស របស់ Un
របាយការណ៍សិទ្ធិមនុស្ស របស់ UnSorn Chanratha
 
Facebook security updated
Facebook security updatedFacebook security updated
Facebook security updatedSorn Chanratha
 
introduction to internet
introduction to internetintroduction to internet
introduction to internetSorn Chanratha
 
introduction to facebook
introduction to facebookintroduction to facebook
introduction to facebookSorn Chanratha
 
Create and using twitter​
Create and using twitter​Create and using twitter​
Create and using twitter​Sorn Chanratha
 
Create and using Gmail
Create and using Gmail Create and using Gmail
Create and using Gmail Sorn Chanratha
 
Create and using Facebook
Create and using Facebook Create and using Facebook
Create and using Facebook Sorn Chanratha
 
useful website address
useful website addressuseful website address
useful website addressSorn Chanratha
 

More from Sorn Chanratha (18)

Buddhist dictionary
Buddhist dictionaryBuddhist dictionary
Buddhist dictionary
 
Training of trainer on Web2.0
Training of trainer on Web2.0Training of trainer on Web2.0
Training of trainer on Web2.0
 
Learn to write Khmer Poem
Learn to write Khmer PoemLearn to write Khmer Poem
Learn to write Khmer Poem
 
រឿងប្រលោមលោក ស្នាមញញឹម
រឿងប្រលោមលោក ស្នាមញញឹមរឿងប្រលោមលោក ស្នាមញញឹម
រឿងប្រលោមលោក ស្នាមញញឹម
 
របាយការណ៍សិទ្ធិមនុស្ស របស់ Un
របាយការណ៍សិទ្ធិមនុស្ស របស់ Unរបាយការណ៍សិទ្ធិមនុស្ស របស់ Un
របាយការណ៍សិទ្ធិមនុស្ស របស់ Un
 
Facebook security updated
Facebook security updatedFacebook security updated
Facebook security updated
 
introduction to internet
introduction to internetintroduction to internet
introduction to internet
 
introduction to facebook
introduction to facebookintroduction to facebook
introduction to facebook
 
Google Search
Google SearchGoogle Search
Google Search
 
Facebook Security
Facebook SecurityFacebook Security
Facebook Security
 
CYN Address
CYN Address CYN Address
CYN Address
 
Create and using twitter​
Create and using twitter​Create and using twitter​
Create and using twitter​
 
Create and using Gmail
Create and using Gmail Create and using Gmail
Create and using Gmail
 
Create and using Facebook
Create and using Facebook Create and using Facebook
Create and using Facebook
 
useful website address
useful website addressuseful website address
useful website address
 
C++ Chapter IV
C++ Chapter IVC++ Chapter IV
C++ Chapter IV
 
C++ Chapter III
C++ Chapter IIIC++ Chapter III
C++ Chapter III
 
C++ Chapter I
C++ Chapter IC++ Chapter I
C++ Chapter I
 

C++ Chapter II

  • 1.     Chapter  II     Lecturer:  Hong  Mom    
  • 2. 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    
  • 3. 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  
  • 4. Control  Structures   a.    Sequence   b.  Selec7on   c.  Repea7on   Statement1   T   F   Exp   T   Exp   statement   Statement2   F   Statement1   Statement2   StatementN  
  • 5. 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++  :   ==    ,    !=  ,  <  ,  <=  ,  >,  >=    
  • 6. Logical  Operators   Logical  Operators:    !    :  NOT    &&  :  AND    ||      :  OR   Example:      Expression                                  Value    19<  8                                                                                                                false    45  >  41                                                                                                          True  
  • 7. 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;    …   }    
  • 8. If  statement   The  if  statement  :  is  used  to  execute  a  statement  or   block  only  if  a  condi7on  is  sa7sfied.   Form:     if  (condi7on)  statement         Ex:      if(x  ==  5)                  cout  <<  “  x  is  5”  ;        
  • 9. 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:                                                                                                                     if  (condi7on  )  statement1           else    s  tatement2         Ex:      if  (x==  5)      cout<<“x  is  5”;      else      cout  <<  “x  is  not  5”      
  • 10. if  ..  Else  if  …else   Syntax:   if  (  exp1)   if  exp1  is  sa7sfied,  do    statement  1;   something.   else  if  (exp  2)   Otherwise,  check  if   exp2  is  sa7sfied  and  if  so,  do    statement  2;   something  else.   …    Otherwise  (else)  ,  do   else   completely     Something  else    statement  n;  
  • 11. 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”;    }  
  • 12. Nested  if     has  another  if  in  it’s    body  or  in  its  else’s  body  or  in   It     both   Syntax:     if  (exp1){     if  (exp1  )    if  (exp2)    statement  1;    statement  1;   else{    else      if  (exp2)    statement  2;    statement  2;   }    else   else    statement  3;    statement  3;   }  
  • 13. 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  
  • 14. switch  case  mul7-­‐way       decision-­‐making   switch  (expression){    case  value  1:        statement  1;  break;    case  value  2:      statement  2;  break;    default:      statements;   }  
  • 15. 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”;   }  
  • 16. 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.”;   }    
  • 17. Itera7on   •  Itera7on  control  statements  allow  you  to   execute  one  or  more  program  statements   repeatedly.   •  Itera7on  control  statements:   –  while     –  do-­‐while   –  for  
  • 18. The  while  statement   •  The  while  statement  causes  one  or  more   statements  to  repeat  as  long  as  a  specified   expression  remains  true.    while(expression)    {   F   condi7on      statements;   T    }   statement  
  • 19. While-­‐  Example   #include<iostream.h>   void  main(){    int  i  =  1;    while(i<=5){      cout<<”Welcome  CIEDC”  <<  endl;      i++;                                }     }  
  • 20. Do-­‐while  statement   •  This  loop  is  executed  at  least  once.   •  It  is  executed  7ll  the  condi7on  remains   true.   do{    statements;   statement   }while  (expression);   T   F   condi7on  
  • 21. Do-­‐While-­‐  Example   #include<iostream.h>   void  main(){    int  i  =  1;    do{      cout<<”Welcome  CIEDC”  <<  endl;      i++;    }while(i<=5);   }  
  • 22. for  statement   •  for  statement            -­‐  is  used  to  execute  one  or  more  statements  a   specified  number  of  7mes.   for  (ini7al  expression;  con7nues  condi7on;   incrementdecrement)   {    statements;   }      
  • 23. 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)    
  • 24. 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 }
  • 25. 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;   }  
  • 26. 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;   }      
  • 27. 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();   }  
  • 28. for  statement  –  Example3   Output  screen   * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *