Page 64 - MODUL ALGORTIMA DAN PEMROGRAMAN
P. 64

hasil = a && b;
                      printf("a && b = %d\n", hasil);

                      // Logical OR
                      hasil = a || b;
                      printf("a || b = %d\n", hasil);

                      // Logical NOT
                      printf("!a = %d\n", !a);

                      return 0;
                  }

                                               KODE PROGRAM BAHASA C++
                  #include <iostream>
                  using namespace std;
                  int main(){
                      int a = 1; // true
                      int b = 0; // false
                      bool hasil;
                      cout << "a = " << a << endl;
                      cout << "b = "
                  << b << endl;

                      // logika AND
                      hasil = a && b;
                      cout << "a && b
                  = " << hasil <<
                  endl;

                      // logika OR
                      hasil = a || b;
                      cout << "a || b
                  = " << hasil <<
                  endl;

                      // logika NOT
                      cout << "!a = "
                  << !a << endl;

                      return 0;
                  }

                  Output:
                  a = 1
                  b = 0
                  a && b = 0
                  a || b = 1
                  !a = 0

                  7.  Contoh 7 – Program penggunaan operator penugasan

                                                KODE PROGRAM BAHASA C
                  #include <stdio.h>

                  int main() {
                      int a = 5;
                      int b = 10;

                      // Assignment with addition
                      b += a; // Equivalent to b = b + a                                                  41
                      printf("Result of b += a is %d\n", b);


                      // Assignment with subtraction
                      b -= a; // Equivalent to b = b - a
                      printf("Result of b -= a is %d\n", b);
   59   60   61   62   63   64   65   66   67   68   69