Rabu, Juni 25, 2014

Intoduction To Boolean Variable On C Programming Language

Boolean variable has only True or False value. To make program use boolean variable can be used if... else... statement

Example of its program as follows:
  1. Boolean without variable
    • Example we want to make program, if a statement valued true then program will show A letter.

      main()
      {
        if (1<2)
        {
          printf("A");
        }
        getch();
      }


    • On program syntax above, statement valued true, because correct 1<2. But if statement is made be false, example by declaring 1>2, so that program syntax becomes:

      main()
      {
        if (1>2)
        {
          printf("A");
        }
        getch();
      }


      then program syntax will not show A letter when program is run, please try.

    • Now we will make program, if a statement valued true, then program will show A letter, but if not (if statement valued false) then program will show B letter

      main()
      {
        if (1<2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }
      main()
      {
        if (1>2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }

  2. Boolean use integer variable
    • If a statement valued true then program will show A letter

      main()
      {
        int num1,num2;
        num1=1; num2=2;
        if (num1<num2)
        {
          printf("A");
        }
        getch();
      }

    • If a statement valued false then program will not show A letter

      main()
      {
        int num1,num2;
        num1=1; num2=2;
        if (num1>num2)
        {
          printf("A");
        }
        getch();
      }

    • If a statement valued true then program will show A letter, if not program will show B letter

      main()
      {
        int num1,num2;
        num1=1; num2=2;
        if (num1<num2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }
      main()
      {
        int num1,num2;
        num1=1; num2=2;
        if (num1>num2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }

  3. Boolean use character char variable, not integer char
    • If a statement valued true then program will show A letter

      main()
      {
        char character1,character2;
        character1='s'; character2='i';
        if (character1 != character2)
        {
          printf("A");
        }
        getch();
      }

    • If a statement valued false then program will not show A letter

      main()
      {
        char character1,character2;
        character1='s'; character2='i';
        if (character1 == character2)
        {
          printf("A");
        }
        getch();
      }

    • If a statement valued true then program will show A letter, if not program will show B letter

      main()
      {
        char character1,character2;
        character1='s'; character2='i';
        if (character1 != character2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }
      main()
      {
        char character1,character2;
        character1='s'; character2='i';
        if (character1 == character2)
        {
          printf("A");
        }
        else
        {
          printf("B");
        }
        getch();
      }

0 komentar: