Thread: if statement

  1. #1
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13

    Question if statement

    Hi guys, it's me the newbie again =) I've a new question. Here's a piece of code I copied from a book:

    Code:
    //This programme shows an example of the AND && expression
    #include <iostream>
    
    
    int main()
    {
        using namespace std;
        
        short number1 = 10;
        char word = 't';
        
        if (number1 == 10 && word == 't')
        cout<< "Both sides of the AND expression are True";
        cin.get();
        return 0;
    }
    It seems that when I didn't end this line of command:
    Code:
    if (number1 == 10 && word == 't')
    with a semi-colon, the whole script can still run and the main function is working. Why is that so ??? Thanks in advance guys

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I suggest reading the book more for the explanation. If you still do not understand read the tutorial on if statements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    That is correct.

    If you indent your code correctly, it becomes more clear:
    Code:
        if (number1 == 10 && word == 't')
           cout<< "Both sides of the AND expression are True";
    If you do this:
    Code:
        if (number1 == 10 && word == 't');
        cout<< "Both sides of the AND expression are True";
    The compiler will happily produce code, but you will ALWAYS get the output from the cout line.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Location
    Malaysia
    Posts
    13
    Oh I see...thanks matsp =) You're a real good programmer. Wonder how many years have you been learning C++?

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by junkeat90 View Post
    Oh I see...thanks matsp =) You're a real good programmer. Wonder how many years have you been learning C++?
    I've been working with C++ for about half a year, but first time I used C was more than 15 years ago, and the above "problem" would be identical in C, only that you wouldn't use cout, but printf to print the text.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    char word = 't';
    Is that really what's in the book? If so, beware, because it's misleading: a char stores a single character, not a word in the traditional sense (you need a string for that), nor a machine word (the CPU's "most comfortable" integer type), nor an x86 word (a 16-bit integer).
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Usefulness of the "else if" statement
    By gn17 in forum C Programming
    Replies: 7
    Last Post: 08-12-2007, 05:19 AM
  2. Meaning of this statement?
    By @nthony in forum C Programming
    Replies: 7
    Last Post: 07-16-2006, 02:57 AM
  3. if/break statement
    By Apropos in forum C++ Programming
    Replies: 7
    Last Post: 02-22-2005, 02:33 PM
  4. string & if statement
    By Curacao in forum C++ Programming
    Replies: 4
    Last Post: 05-02-2003, 09:56 PM
  5. Uh-oh! I am having a major switch problem!
    By goodn in forum C Programming
    Replies: 4
    Last Post: 11-01-2001, 04:49 PM