SlideShare una empresa de Scribd logo
1 de 19
Descargar para leer sin conexión
IF Statement
Conditional Structure (1)
 Type 1                            Type 2

                      if( Cond )                        if( Cond )
   Cond     T                                   T       {
                      {                Cond
                         A;                                A;
        F        A                          F
                         B;                                B;
                      }                 C           A   }
                      C;                                else
                 B
                      D;                                {
                                        D           B       C;
                                                            D;
    C                                                   }
                                        E
                                                        E;
    D




                                                                     2
Conditional Structure (2)
 if statement
   if( Cond )                                        T
                                            Cond
   {                  If cond is true,
       A;                                        F
                  Statements are executed    C           A
       B;
   }
                                             D           B
   else
   {                  If cond is false,
       C;         Statements are executed
                                             E
       D;
   }

   E;            Statements are executed
                    after if statement


                                                             3
Conditional Structure (3)
 Example                      if(Cond1 )
                               {
                                  A;
              Cond1    T
                               }
                   F           else
                           A
                               {
              Cond2               if( Cond2 )
      F            T              {
                                      C;
  B            C                  }
                                  else
                                  {
                                      B;
          D                        }
                                   D;
                               }
                       E
                               E;


                                                4
Example 2
 Read a number and print “Yes” if it is 2 or “No”
                            #include <stdio.h>

          Start             main() {

                                int x ;
            x                   scanf( “%d”, &x ) ;

                                if( x == 2 )
            x                   {
                                    printf( “Yesn” ) ;
                                }
          x == 2                else
 T                    F         {
                                    printf( “Non” ) ;
  “Yes”            “No”          }

                                return ;
                            }

          Stop



                                                          5
Example 3
 Find the maximum among 3 numbers
                                                  #include <stdio.h>
                      Start
                                                  main() {

                                                      int a, b, c ;
                    Var a, b, c
                                                      scanf( %d%d%d”, &a, &b, &c ) ;

                   Input a, b, c                      if(a > b )
                                                      {
                                                          if(a > c ) {
                                                              printf( “%dn”,   a);
                      a >b                                } else {
    T                                     F                   printf( “%dn”,   c);
                                                          }
        a >c                           b >c           }
                                                      else
                                                      {
a              c                   b          c           if( b > c ) {
                                                              printf( “%dn”,   b);
                                                          } else {
                                                              printf( “%dn”,   c);
                        Stop                              }
                                                      }

                                                      return ;
                                                  }                                    6
#include <stdio.h>

                                                                                           main() {
                                                             Example 4                         int a, b, c ;

                                                                                               scanf( %d%d%d”, &a, &b, &c ) ;
           Sort 3 numbers
                                                                                               if( a > b )
                                         Start                                                 {
                                                                                                   if( b > c ) {
                                                                                                       printf( “%d %d %dn”, a, b, c ) ;
                                         a, b, c                                                   } else {
                                                                                                       if( a > c ) {
                                                                                                           printf( “%d %d %dn”, a, c, b   );
                                         a, b, c                                                       } else {
                                                                                                           printf( “%d %d %dn”, c, a, b   );
                                                                                                       }
                                                                                                   }
                                          a >b
          T                                                     F                              }
                                                                                               else
          b >c                                               a >c                              {
T                         F                   T                             F
                                                                                                   if( a > c ) {
                                                                                                       printf( “%d %d %dn”, b, a, c ) ;
                        a >c                                              b >c                     } else {
              T                    F                            T                     F                if( b > c ) {
                                                                                                           printf( “%d %d %dn”, b, c, a   );
a, b, c       a, c, b          c, a, b             b, a, c      b, c, a          c, b, a               } else {
                                                                                                           printf( “%d %d %dn”, c, b, a   );
                                                                                                       }
                                                                                                   }
                                                                                               }
                                            Stop
                                                                                               return ;
                                                                                           }                                               7
if and if-else Statements
 Nested if statement          if ( Condition1 ) {
                                   A;
                T              }
  Condition1          A        else {
        F                          if( Condition2 ) {
                T     B                B;
  Condition2
                                   }
        F
                T
                                   else {
  Condition3          C
                                       if( Condition3 ) {
        F                                  C;
       D                               }
                                       else {
                                            D;
       E
                                       }
                                   }
                               }
                               E;
                                                            8
if and if-else Statements
 Nested if statement
                                if ( Condition1 ) {
                  T
                                    A;
   Condition1           A       }
         F                      else if( Condition2 ) {
                  T     B           B;
   Condition2
                                }
         F
                  T
                                else if( Condition3 ) {
   Condition3           C
                                    C;
         F                      }
        D                       else {
                                    D;
                                }
        E
                                E;




                                                          9
if and if-else Statements
 Nested if statement
  – Identify whether a character is number, capital letter or
    small letter

              scanf( “%c”, &ch ) ;

              if (‘0’ <= c && c <= ‘9’)
                   printf( “Numbern” ) ;
              else if ( ‘A’ <= c && c <= ‘Z’)
                   printf( “Upper casen” ) ;
              else if (‘a’ <= c && c <= ‘z’)
                   printf( “Low casen” ) ;
              else
                   printf( “Outside of thatn” ) ;




                                                                10
switch Statement
 Special case of nested if statement

                T              switch( variable ) {
  var==value1          A
                               case value1 : A ;
         F
                T                         break ;
  var==value2          B
                               case value2 : B ;
         F
                                          break ;
                T
  var==value3          C
                               case value3 : C ;
         F                                break ;
        D
                               default :
                                           D ;
        E                      }




                                                      11
switch statement
 Change swtich to nested if statements

    switch( variable ) {      if( var==value1 ) {
    case value1 : A ;             A ;
               break ;        }
    case value2 : B ;         else if(var==value2 ) {
               break ;            B ;
    case value3 : C ;         }
               break ;        else if(var==value3 ) {
    default : D ;                 C ;
    }                         }
                              else {
                                  D ;
                              }


                                                        12
switch statement
   Role of break in switch statement
      – When grade == 4
switch (grade)                       switch (grade)
{                                    {
case 4 : printf(“A”) ;               case 4 : printf(“A”) ;
         break;                      case 3 : printf(“B”) ;
case 3 : printf(“B”) ;               case 2 : printf(“C”) ;
         break;                      case 1 : printf(“D”) ;
case 2 : printf(“C”) ;               default : printf(“Illegal grade”);
         break;                      }
case 1 : printf(“D”) ;
         break;
default : printf(“Illegal grade”);
}



                                                                          13
switch statement
 Role of break in switch statement
  – If grade is ‘A’, ‘B’, ‘C’ or ‘D’ then “pass”
  – If grade is ‘F’ then “fail”, or “error”
  switch (grade) {
      case ‘A’ :
      case ‘B’ :
      case ‘C’ :
      case ‘D’ : printf(“passn”); break;
      case ‘F’ : pritnf(“failn”); break;
      default : printf(“errorn”); break;
  }




                                                   14
switch statement
 Role of break in switch statement
  – Change previous example to if-else statements

if ( grade == ‘A’ || grade == ‘B’ || grade == ‘C’ || grade == ‘D’ )
    printf( “passn” ) ;
else if( grade == ‘F’ )
    printf( “failn” ) ;
else
    printf( “errorn” ) ;




                                                                      15
Short-Circuit
 Short-Circuit Evaluation
  – Boolean operators(&&, ||, etc.) in which the second
    argument is only executed or evaluated if the first argument
    does not suffice to determine the value of the expression.


 expr1 && expr2
  – If expr1 is False, then expr2 is not executed.


 expr1 || expr2
  – If expr1 if True, then expr2 is not executed.




                                                                   16
Short-Circuit
 Example of Short-Circuit Evaluation
 int i=2, j=3;                   int i=2, j=3;
 if( (i == 2) && ( j++ == 3) )   if( (i == 2) || (j++ == 3) )
    printf( “Truen” ) ;            printf( “Truen” ) ;
 else                            else
    printf( “Falsen” ) ;           printf( “Falsen” ) ;
 printf(“%d %dn”, i, j);        printf(“%d %dn”, i, j);

 int i=2, j=3;                   int i=2, j=3;
 if( (i == 3) && ( j++ == 3) )   if( (i == 3) || (j++ == 3) )
    printf( “Truen” ) ;            printf( “Truen” ) ;
 else                            else
    printf( “Falsen” ) ;           printf( “Falsen” ) ;
 printf(“%d %dn”, i, j);        printf(“%d %dn”, i, j);

                                                                17
Conditional Operator
 Conditional Operator Syntax
  expr1 ? expr2 : expr3

  – Conditional operator ? : is ternary operator
  – Evaluate expr1 first,
    If it is true then expr2 is executed.
    Or if it is false then expr3 is executed.


 if-else statement          Conditional Operator
  if ( y < z)               x=(y<z)?y:z
    x = y;
  else
    x=z

                                                    18
Conditional Operator
 Example
 int a = 5, b = 6 ;

 int max = (a < b) ? b : a ;
 int min = (a > b) ? b : a ;




                                     19

Más contenido relacionado

La actualidad más candente

Module 11 Tansformation
Module 11  TansformationModule 11  Tansformation
Module 11 Tansformationguestcc333c
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, iohtaitk
 
Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)Irfan Anjum
 
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...Förderverein Technische Fakultät
 
C++ language basic
C++ language basicC++ language basic
C++ language basicWaqar Younis
 
Promises. The basics, from Promises/A+
Promises. The basics, from Promises/A+Promises. The basics, from Promises/A+
Promises. The basics, from Promises/A+Luke Smith
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy BookAbir Hossain
 
Lesson 20: The Mean Value Theorem
Lesson 20: The Mean Value TheoremLesson 20: The Mean Value Theorem
Lesson 20: The Mean Value TheoremMatthew Leingang
 
principles of programming languages
principles of programming languages principles of programming languages
principles of programming languages Lakshmi Prasad
 

La actualidad más candente (16)

Module 11 Tansformation
Module 11  TansformationModule 11  Tansformation
Module 11 Tansformation
 
2. data, operators, io
2. data, operators, io2. data, operators, io
2. data, operators, io
 
Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)Logical instructions (and, or, xor, not, test)
Logical instructions (and, or, xor, not, test)
 
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
6th Semester (Dec-2015; Jan-2016) Computer Science and Information Science En...
 
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...
Theorie, Praxis und Perspektiven der operationsbasierten formalen Schaltungsv...
 
6th Semester (June; July-2014) Computer Science and Information Science Engin...
6th Semester (June; July-2014) Computer Science and Information Science Engin...6th Semester (June; July-2014) Computer Science and Information Science Engin...
6th Semester (June; July-2014) Computer Science and Information Science Engin...
 
6th semester Computer Science and Information Science Engg (2013 December) Qu...
6th semester Computer Science and Information Science Engg (2013 December) Qu...6th semester Computer Science and Information Science Engg (2013 December) Qu...
6th semester Computer Science and Information Science Engg (2013 December) Qu...
 
C++ language basic
C++ language basicC++ language basic
C++ language basic
 
Promises. The basics, from Promises/A+
Promises. The basics, from Promises/A+Promises. The basics, from Promises/A+
Promises. The basics, from Promises/A+
 
7 habits
7 habits7 habits
7 habits
 
Important C program of Balagurusamy Book
Important C program of Balagurusamy BookImportant C program of Balagurusamy Book
Important C program of Balagurusamy Book
 
8th Semester (June; July-2014) Computer Science and Information Science Engin...
8th Semester (June; July-2014) Computer Science and Information Science Engin...8th Semester (June; July-2014) Computer Science and Information Science Engin...
8th Semester (June; July-2014) Computer Science and Information Science Engin...
 
6th Semester (June; July-2015) Computer Science and Information Science Engin...
6th Semester (June; July-2015) Computer Science and Information Science Engin...6th Semester (June; July-2015) Computer Science and Information Science Engin...
6th Semester (June; July-2015) Computer Science and Information Science Engin...
 
Lesson 20: The Mean Value Theorem
Lesson 20: The Mean Value TheoremLesson 20: The Mean Value Theorem
Lesson 20: The Mean Value Theorem
 
principles of programming languages
principles of programming languages principles of programming languages
principles of programming languages
 
C introduction by thooyavan
C introduction by  thooyavanC introduction by  thooyavan
C introduction by thooyavan
 

Destacado

13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation웅식 전
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide웅식 전
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료웅식 전
 
9 object class
9 object class9 object class
9 object class웅식 전
 
15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function웅식 전
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing웅식 전
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자웅식 전
 
03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스웅식 전
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io웅식 전
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11 웅식 전
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef웅식 전
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng웅식 전
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)웅식 전
 

Destacado (19)

13. dynamic allocation
13. dynamic allocation13. dynamic allocation
13. dynamic allocation
 
13th chapter12 slide
13th chapter12 slide13th chapter12 slide
13th chapter12 slide
 
구름 기본 소개자료
구름 기본 소개자료구름 기본 소개자료
구름 기본 소개자료
 
13. structure
13. structure13. structure
13. structure
 
9 object class
9 object class9 object class
9 object class
 
15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
9. pointer, pointer & function
9. pointer, pointer & function9. pointer, pointer & function
9. pointer, pointer & function
 
5 1. character processing
5 1. character processing5 1. character processing
5 1. character processing
 
02장 자료형과 연산자
02장 자료형과 연산자02장 자료형과 연산자
02장 자료형과 연산자
 
03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스03장 조건문반복문네임스페이스
03장 조건문반복문네임스페이스
 
4. loop
4. loop4. loop
4. loop
 
12 2. dynamic allocation
12 2. dynamic allocation12 2. dynamic allocation
12 2. dynamic allocation
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
10th
10th10th
10th
 
2 3. standard io
2 3. standard io2 3. standard io
2 3. standard io
 
Week12 chapter11
Week12 chapter11 Week12 chapter11
Week12 chapter11
 
15 1. enumeration, typedef
15 1. enumeration, typedef15 1. enumeration, typedef
15 1. enumeration, typedef
 
11 1. multi-dimensional array eng
11 1. multi-dimensional array eng11 1. multi-dimensional array eng
11 1. multi-dimensional array eng
 
Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)Goorm ide 소개 슬라이드(교육용 버전)
Goorm ide 소개 슬라이드(교육용 버전)
 

Similar a 3 2. if statement

(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2Pamidimukkala Sivani
 
Supplement to local voatility
Supplement to local voatilitySupplement to local voatility
Supplement to local voatilityIlya Gikhman
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1Little Tukta Lita
 

Similar a 3 2. if statement (6)

C++11
C++11C++11
C++11
 
4. loop
4. loop4. loop
4. loop
 
Revision1 C programming
Revision1 C programmingRevision1 C programming
Revision1 C programming
 
(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2(Www.entrance exam.net)-tcs placement sample paper 2
(Www.entrance exam.net)-tcs placement sample paper 2
 
Supplement to local voatility
Supplement to local voatilitySupplement to local voatility
Supplement to local voatility
 
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6  1
ฟังก์ชั่นย่อยและโปรแกรมมาตรฐาน ม. 6 1
 

Más de 웅식 전

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization웅식 전
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main웅식 전
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array웅식 전
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer웅식 전
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function웅식 전
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class웅식 전
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing웅식 전
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement웅식 전
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib웅식 전
 
2 2. operators
2 2. operators2 2. operators
2 2. operators웅식 전
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types웅식 전
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)웅식 전
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스웅식 전
 
Goorm ide open beta
Goorm ide open betaGoorm ide open beta
Goorm ide open beta웅식 전
 

Más de 웅식 전 (18)

15 3. modulization
15 3. modulization15 3. modulization
15 3. modulization
 
15 2. arguement passing to main
15 2. arguement passing to main15 2. arguement passing to main
15 2. arguement passing to main
 
14. fiile io
14. fiile io14. fiile io
14. fiile io
 
12 1. multi-dimensional array
12 1. multi-dimensional array12 1. multi-dimensional array
12 1. multi-dimensional array
 
11. array & pointer
11. array & pointer11. array & pointer
11. array & pointer
 
10. pointer & function
10. pointer & function10. pointer & function
10. pointer & function
 
9. pointer
9. pointer9. pointer
9. pointer
 
7. variable scope rule,-storage_class
7. variable scope rule,-storage_class7. variable scope rule,-storage_class
7. variable scope rule,-storage_class
 
6. function
6. function6. function
6. function
 
5 2. string processing
5 2. string processing5 2. string processing
5 2. string processing
 
3 2. if statement
3 2. if statement3 2. if statement
3 2. if statement
 
3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib3 1. preprocessor, math, stdlib
3 1. preprocessor, math, stdlib
 
2 2. operators
2 2. operators2 2. operators
2 2. operators
 
2 1. variables & data types
2 1. variables & data types2 1. variables & data types
2 1. variables & data types
 
Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)Goorm ide 교육용버전 for skku(학생)
Goorm ide 교육용버전 for skku(학생)
 
W14 chap13
W14 chap13W14 chap13
W14 chap13
 
10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스10장 문자열클래스와파일클래스
10장 문자열클래스와파일클래스
 
Goorm ide open beta
Goorm ide open betaGoorm ide open beta
Goorm ide open beta
 

3 2. if statement

  • 2. Conditional Structure (1)  Type 1  Type 2 if( Cond ) if( Cond ) Cond T T { { Cond A; A; F A F B; B; } C A } C; else B D; { D B C; D; C } E E; D 2
  • 3. Conditional Structure (2)  if statement if( Cond ) T Cond { If cond is true, A; F Statements are executed C A B; } D B else { If cond is false, C; Statements are executed E D; } E; Statements are executed after if statement 3
  • 4. Conditional Structure (3)  Example if(Cond1 ) { A; Cond1 T } F else A { Cond2 if( Cond2 ) F T { C; B C } else { B; D } D; } E E; 4
  • 5. Example 2  Read a number and print “Yes” if it is 2 or “No” #include <stdio.h> Start main() { int x ; x scanf( “%d”, &x ) ; if( x == 2 ) x { printf( “Yesn” ) ; } x == 2 else T F { printf( “Non” ) ; “Yes” “No” } return ; } Stop 5
  • 6. Example 3  Find the maximum among 3 numbers #include <stdio.h> Start main() { int a, b, c ; Var a, b, c scanf( %d%d%d”, &a, &b, &c ) ; Input a, b, c if(a > b ) { if(a > c ) { printf( “%dn”, a); a >b } else { T F printf( “%dn”, c); } a >c b >c } else { a c b c if( b > c ) { printf( “%dn”, b); } else { printf( “%dn”, c); Stop } } return ; } 6
  • 7. #include <stdio.h> main() { Example 4 int a, b, c ; scanf( %d%d%d”, &a, &b, &c ) ;  Sort 3 numbers if( a > b ) Start { if( b > c ) { printf( “%d %d %dn”, a, b, c ) ; a, b, c } else { if( a > c ) { printf( “%d %d %dn”, a, c, b ); a, b, c } else { printf( “%d %d %dn”, c, a, b ); } } a >b T F } else b >c a >c { T F T F if( a > c ) { printf( “%d %d %dn”, b, a, c ) ; a >c b >c } else { T F T F if( b > c ) { printf( “%d %d %dn”, b, c, a ); a, b, c a, c, b c, a, b b, a, c b, c, a c, b, a } else { printf( “%d %d %dn”, c, b, a ); } } } Stop return ; } 7
  • 8. if and if-else Statements  Nested if statement if ( Condition1 ) { A; T } Condition1 A else { F if( Condition2 ) { T B B; Condition2 } F T else { Condition3 C if( Condition3 ) { F C; D } else { D; E } } } E; 8
  • 9. if and if-else Statements  Nested if statement if ( Condition1 ) { T A; Condition1 A } F else if( Condition2 ) { T B B; Condition2 } F T else if( Condition3 ) { Condition3 C C; F } D else { D; } E E; 9
  • 10. if and if-else Statements  Nested if statement – Identify whether a character is number, capital letter or small letter scanf( “%c”, &ch ) ; if (‘0’ <= c && c <= ‘9’) printf( “Numbern” ) ; else if ( ‘A’ <= c && c <= ‘Z’) printf( “Upper casen” ) ; else if (‘a’ <= c && c <= ‘z’) printf( “Low casen” ) ; else printf( “Outside of thatn” ) ; 10
  • 11. switch Statement  Special case of nested if statement T switch( variable ) { var==value1 A case value1 : A ; F T break ; var==value2 B case value2 : B ; F break ; T var==value3 C case value3 : C ; F break ; D default : D ; E } 11
  • 12. switch statement  Change swtich to nested if statements switch( variable ) { if( var==value1 ) { case value1 : A ; A ; break ; } case value2 : B ; else if(var==value2 ) { break ; B ; case value3 : C ; } break ; else if(var==value3 ) { default : D ; C ; } } else { D ; } 12
  • 13. switch statement  Role of break in switch statement – When grade == 4 switch (grade) switch (grade) { { case 4 : printf(“A”) ; case 4 : printf(“A”) ; break; case 3 : printf(“B”) ; case 3 : printf(“B”) ; case 2 : printf(“C”) ; break; case 1 : printf(“D”) ; case 2 : printf(“C”) ; default : printf(“Illegal grade”); break; } case 1 : printf(“D”) ; break; default : printf(“Illegal grade”); } 13
  • 14. switch statement  Role of break in switch statement – If grade is ‘A’, ‘B’, ‘C’ or ‘D’ then “pass” – If grade is ‘F’ then “fail”, or “error” switch (grade) { case ‘A’ : case ‘B’ : case ‘C’ : case ‘D’ : printf(“passn”); break; case ‘F’ : pritnf(“failn”); break; default : printf(“errorn”); break; } 14
  • 15. switch statement  Role of break in switch statement – Change previous example to if-else statements if ( grade == ‘A’ || grade == ‘B’ || grade == ‘C’ || grade == ‘D’ ) printf( “passn” ) ; else if( grade == ‘F’ ) printf( “failn” ) ; else printf( “errorn” ) ; 15
  • 16. Short-Circuit  Short-Circuit Evaluation – Boolean operators(&&, ||, etc.) in which the second argument is only executed or evaluated if the first argument does not suffice to determine the value of the expression.  expr1 && expr2 – If expr1 is False, then expr2 is not executed.  expr1 || expr2 – If expr1 if True, then expr2 is not executed. 16
  • 17. Short-Circuit  Example of Short-Circuit Evaluation int i=2, j=3; int i=2, j=3; if( (i == 2) && ( j++ == 3) ) if( (i == 2) || (j++ == 3) ) printf( “Truen” ) ; printf( “Truen” ) ; else else printf( “Falsen” ) ; printf( “Falsen” ) ; printf(“%d %dn”, i, j); printf(“%d %dn”, i, j); int i=2, j=3; int i=2, j=3; if( (i == 3) && ( j++ == 3) ) if( (i == 3) || (j++ == 3) ) printf( “Truen” ) ; printf( “Truen” ) ; else else printf( “Falsen” ) ; printf( “Falsen” ) ; printf(“%d %dn”, i, j); printf(“%d %dn”, i, j); 17
  • 18. Conditional Operator  Conditional Operator Syntax expr1 ? expr2 : expr3 – Conditional operator ? : is ternary operator – Evaluate expr1 first, If it is true then expr2 is executed. Or if it is false then expr3 is executed.  if-else statement Conditional Operator if ( y < z) x=(y<z)?y:z x = y; else x=z 18
  • 19. Conditional Operator  Example int a = 5, b = 6 ; int max = (a < b) ? b : a ; int min = (a > b) ? b : a ; 19