BrainDump
  Home arrow BrainDump arrow Page 3 - 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 - You Are Cool....Not(!)


    (Page 3 of 4 )

    Let's say we wanted to let the user know that they entered the right numbers, but did not enter them in the proper sequence. We could do so by using the NOT(!) in our code, like so:


    #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 if(c==numtwo && d==numone)

    {

    printf("nnYou guessed the right numbers but in the wrong sequence!");

    printf("nWhat a boob.");

    }

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

    {

    printf("nnYou guessed the first number right.nn");

    }

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

    {

    printf("nnYou guessed the second number right.nn");

    }

    else

    {

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

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

    }

    return(0);

    }

    This program starts in the same manner as our previous program, creating the same four variables, which serve the same function. In fact, most of this program functions in the same way. Where it differs is in the If statement.

    Our first part of the If statement says if "c" is equal to numone AND "d" is equal to numtwo, print out:

      You guessed both numbers in the proper sequence!

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

    If that criteria is not met, it moves on to an Else If, which states: if "c" is equal to numtwo AND "d" is equal to numone, (i.e.; if the numbers are right but out of sequence) print this out:

      You guessed the right numbers but in the wrong sequence!

     

      What a boob.

    Again, if that criteria is not met it moves on to the next Else If, which says: If "c" is equal to numone, AND "d" is NOT equal to numtwo, print this:

      You guessed the first number right.

    If the criteria is not met, it moves onto the next Else If, which states: If "c" is NOT equal to numone, AND "d" is equal to numbtwo, print this text:

      You guessed the second number right.

    And lastly, if none of the criteria is met, it prints out:


      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 4 Hosted by Hostway
    Stay green...Green IT