BrainDump
  Home arrow BrainDump arrow Page 4 - 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 OR(||)


    (Page 4 of 4 )

    Lastly we come to our dear buddy the Logical OR(||). This bad boy checks to see if any one of two expressions is true. Here we will use the OR to shorten our code from the previous example:


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

    {

    printf("nnYou guessed one 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);

    }

    In this example you will notice that our OR(||) did not save us all that much space. In bigger programs it can save you quite a bit of space and time. This program works in the same manner as the prior one, with a slight change in some of the text we print out. It uses the || to say if "c" is equal to numone AND "d" is not equal to numtwo OR if "c" is not equal to numone AND "d" is equal to numtwo, then print out:

      You guessed one number right.

    You will note that we are seeking the right number in the right sequence. So if the user typed in say 6 and then 1, they would wind up with the following:

      You have failed to guess the numbers in the proper sequence

     

      Dumb and ugly...what a shame!

    This is because 1 matches the first number and not the second and they typed it out of sequence. If we weren't concerned with the order in which they typed the numbers, we could simply add more Ors and be fine.

    Well that's it for this article. In our next action-packed episode we will finally and at long last discuss loops in C. So stick around. You might learn something.

    Till then...


    DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

       · 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek