BrainDump
  Home arrow BrainDump arrow Page 2 - C: Logical Operators
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
BRAINDUMP

C: Logical Operators
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    2008-03-10

    Table of Contents:
  • C: Logical Operators
  • Logical AND
  • You Are Cool....Not(!)
  • Logical OR(||)

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    C: Logical Operators - Logical AND


    (Page 2 of 4 )

    Logical AND is used to test whether two conditions are true. For example, if I wanted to make a joke about your mother, I could check to see if she is both fat and ugly. If so, I could make my program say something extra insulting. If not, it would say something only slightly insulting. Let's look at a program that asks you to try to guess what two numbers the computer is thinking of:


    #include <stdio.h>

    int main()

    {

    char c,d;

    char numone='1';

    char numtwo='3';

    printf("I am thinking of a number between 1-10.n");

    printf("Please enter a number: ");

    c=getchar();

    fflush(stdin);

    printf("nnI am thinking of another number between 1-10.n");

    printf("Please enter a second number: ");

    d=getchar();

    if(c==numone && d==numtwo)

    {

    printf("nnYou guessed both numbers in the proper sequence!n");

    printf("nYou're a genius! An ugly genius, but a genius nonetheless!");

    }

    else

    {

    printf("nnYou have failed to guess the numbers in the proper

    sequencenn");

    printf("Dumb and ugly...what a shame!n");

    }

    return(0);

    }

    The program begins by creating four variables: "c" and "d", which will hold the users two guesses, and numone and numtwo. both of which hold the numbers the computer is thinking of. Next the computer asks the user to enter a number between 1-10 and stores the value in "c". Then it asks for a second number, and stores that value in "b".

    We then enter an If statement, that says if the value in "c" is equal to numone AND the value in "d" is equal to numtwo then print some text. Note that, in this example, the numbers much match up exactly. For instance, if the user enters in '1' and then '3', they have guessed the right numbers. However, if they enter in '3' and then '1', they have not. If the user enters the wrong number and sequence, then some other text is printed. Here are the results:

    If the user enters the correct data:

      I am thinking of a number between 1-10.

      Please enter a number: 1

      I am thinking of another number between 1-10.

      Please enter a second number: 3

      You guessed both numbers in the proper sequence!

      You're a genius! An ugly genius, but a genius nonetheless!

    If the user enters the wrong data or enters it out of sequence:

      I am thinking of a number between 1-10.

      Please enter a number: 3

      I am thinking of another number between 1-10.

      Please enter a second number: 1

      You have failed to guess the numbers in the proper sequence

      Dumb and ugly...what a shame!

    More BrainDump Articles
    More By James Payne


       · Thanks for dropping by to read my article on C Logical Operators. In this issue we...
     

    BRAINDUMP ARTICLES

    - Introduction to Office Live Workspace
    - Using MS Excel for One-way Analysis of Varia...
    - Comparing Data Sets Using Statistical Analys...
    - Import Blogger Posts into WordPress Using Wi...
    - Download WordPress from an FTP Server and Ru...
    - Install and Run WordPress in XAMPP Local Host
    - What Windows 7 Brings to the Table
    - Virtualization and Sandbox Detection
    - Advanced Firebug Techniques in Windows XP Ho...
    - Editing CSS with Firebug in Windows XP Home
    - Using Firebug in Windows XP Home
    - Migrating to Exchange Server 2007
    - Using System Restore on a Non-Bootable PC
    - Finding Logged on Users and More Scripting S...
    - Developing Macro Commands in MS Excel





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 3 Hosted by Hostway
    Stay green...Green IT