Results 1 to 6 of 6

Thread: Problem with if statement in C++

  1. #1
    Join Date
    Dec 2009
    Posts
    50

    Problem with if statement in C++

    I am quiet new to the C++ programming language and i want to clear a doubt that i am having regarding the if statement in c program. Can you tell me whether it is better to use the if statement with it else part or with out the else part consider the following

    if (x=a)
    {}
    else
    if (x=b)
    {}

    or

    if (x=a)
    {}
    if (x=b)
    {}
    which one of the following is better
    Last edited by Slender; 29-01-2010 at 09:46 PM.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: Problem with if statement in C++

    When it comes to C++ programming language you cannot predict which one is better because it all depends over the condition of your program. If you want the to check only one condition then you can use the if else statement and if you want to check with the condition then you will have to make use of the nested if statement and hence its hard t predict which one of them is better.

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: Problem with if statement in C++

    If you are using the if else statement then you are comparing the value of the same variable with two values and the else part is also used to compare but most probably it is used whenever there is a need to compare the same variable with two different values. So if in your program you just have to compare the variable once using the if statement will be better.

  4. #4
    Join Date
    Dec 2009
    Posts
    50

    Re: Problem with if statement in C++

    Thanks for replying me friend i understood your point but confused a little that is if i am using two if statements then also i am comparing the values two times. I can compare the values of a single variable two as many number of times as i use the if statement. can you please provide me a suitable example for this and lastly thanks for the replies.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: Problem with if statement in C++

    As you have asked for an example let's take the example what you gave with your query go to the first statement if (x=a) at this point if x=a then the then the else loop would not be executed the program will point to the statement immediately comes after the else part. Now when you come to the second statement if x=3 then again then irrespective of x value the second if loop will be executed once. But it all depends on your requirement.

  6. #6
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Problem with if statement in C++

    As far as the C++ language is concerned if you are using the else statement after the if statement then it wastes some of the compiler time so it is advisable to use nested if statement consider the program below and concentrate on its output

    int x = 5;
    cout << "With else statement:";
    if ( cout << "\n\x == 5", x == 5 );
    else if ( cout << "\n\x == 4", x == 4 );
    cout << "Without else statement:";
    if ( cout << "\n\x == 3", x == 3 );

Similar Threads

  1. Problem of WHERE clause in SQL statement
    By Macario in forum Software Development
    Replies: 4
    Last Post: 15-10-2010, 08:02 AM
  2. SQL statement problem in C#
    By AMISH in forum Software Development
    Replies: 4
    Last Post: 01-06-2010, 01:17 PM
  3. Problem with Prepared statement's 'setDate' method
    By Luis-Fernando in forum Software Development
    Replies: 5
    Last Post: 05-03-2010, 08:19 PM
  4. Problem related to null value in sql statement
    By kamina23 in forum Software Development
    Replies: 4
    Last Post: 28-01-2010, 07:36 PM
  5. Replies: 4
    Last Post: 25-02-2009, 08:52 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,110,897.18716 seconds with 16 queries