BrainDump
  Home arrow BrainDump arrow C Statements
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 Statements
By: James Payne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 29
    2008-02-25

    Table of Contents:
  • C Statements
  • ELSE!
  • Else...If
  • The Case Statement

  • 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 Statements


    (Page 1 of 4 )

    In our last article we talked about Arrays and Operators in C. In this article we will cover conditional statements like the If statement, the Else clause, and the Case statement. If there is time, we will also begin discussing how to work with loops.

    Conditional Statements

    When writing programs, we often find ourselves having to respond to something the user has done. Conditionals work, at their very basic level, by stating: if the user does this, do that. It of course gets more complicated than that, but that is where we are going to begin.

    Say you set your alarm at night. If it doesn't go off, you have to call your boss and explain why you are such a slacker. That's the basis of an If statement. If this, then that. Here it is in code:

    #include <stdio.h>

    #include <stdlib.h>

    void main()

    {

    int yourMommasWeight=2000;

    if(yourMommasWeight >120)

    {

    printf("nnnnWow your momma is FAAAAT!nnnnn");

    }

    }

    The above code insults your fat momma. It does so by creating an integer variable named yourMommasWeight and assigning it the value 2000. We then say that if the value of yourMommasWeight is greater than 120, print some text. If the value is less than 120, nothing happens. However, we did make the value of the variable greater than 120, and this is the result:

      Wow your momma is FAAAAT!

    We could also write a program that compared your mom to my mom to see if your mom was fatter:


    #include <stdio.h>

    #include <stdlib.h>

    void main()

    {

    int yourMommasWeight=2000;

    int mymom=119;

    if(yourMommasWeight >mymom)

    {

    printf("nnnnYep...yer mom is a big fat pig.nnnnn");

    }

    }

    In the above example, we compare two variables: yourMommasWeight and mymom. Again, we set the value of the variables, and since your mother is fatter than mine, it prints out some text. If my mother had been fatter, nothing would have happened. We'll fix that shortly. However, first let's be a little fair and get some input from the user. I mean after all, I may be just a little biased.


    #include <stdio.h>

    #include <stdlib.h>

    void main()

    {

    int yourmom;

    int mymom;

    printf("Enter your mother's weight: ");

    scanf_s("%i",&yourmom);

    printf("Enter my mother's weight: ");

    scanf_s("%i",&mymom);

    if(yourmom>mymom)

    {

    printf("nnnnYep...yer mom is a big fat pig.nnnnn");

    }

    }

    In this program we create two variables, yourmom and mymom. We then ask the user to enter a number, and store that value in the variable yourmom. Next, we ask the user to give us another number, and store that in the variable mymom. It then compares the values in both variables, and if the value of yourmom is greater than mymom, it prints some text. If the value of mymom is greater than yourmom, nothing occurs.

    Okay, let's make the program do something if by some crazy chance my mom is fatter than yours. For this we will need the Else Clause...

    More BrainDump Articles
    More By James Payne


       · Hey, thanks for dropping by for the continuing discussion on C. In this article we...
     

    BRAINDUMP ARTICLES

    - Basic Operations and Registers in Assembly
    - Assembly Coding within Visual C/C++ IDE
    - New Microsoft Office Coming with a Twist
    - Microsoft`s FUSE Labs Unveils Spindex Social...
    - HP Slate with Windows 7: Dead or Alive?
    - Windows Phone 7 Mobile OS to Rival Android a...
    - Windows 7 Climbing the Charts, Fights for Ma...
    - Windows 7 Upgrades to Come at Cheaper Prices
    - Microsoft Being Inventive in New Ways to Off...
    - Microsoft Prepares 64 GB Zune HD for Upcomin...
    - Windows 7 Trial for Businesses Gets Extension
    - Microsoft Refuses to Follow Others` Lead on ...
    - Microsoft Promises Plenty of Surprises for E...
    - Are Microsoft Certifications Worth the Cost ...
    - Microsoft, NSF Open Cloud Computing to Scien...





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek