Showing posts with label if. Show all posts
Showing posts with label if. Show all posts

PROGRAMMING GUM



This is a page for the very beginners of C++ Programming Language.Here you can find :-


If You Have Any Suggestion, Query, Or Thoughts To Share With Me, You Can Easily Contact Me.My Contact Details Are Here.


Contact Me 





 

Multiple Statements Under if-else


Multiple Statements Under if-else


Its not always important that we need to execute only one statement at a time whenever 'if' statements are called. It may happen that we need to execute multiple statements when some 'if' sorts out to TRUE. In that case what we do is add just a pair of curly brackets i.e. "{}" immediately after 'if' and write all our instructions that are to be executed within these brackets. Same can be done with else as well. Lets take an example and grab up this concept. I've provide a picture of a sample run at the end of the program to clear the concept.

#include<iostream>
#include<cstdlib>
#include<cstdio>
using namespace std;
int main()
{
    char i;
    cout<<"Give a Grade to C++ : ";
    i=getchar();
    if(i=='A' || i=='a')
    {
        cout<<"\nI like C++.";
        cout<<"\nI love C++.";
        cout<<"\nC++ is just awesome.";
    }
    else
    {
        cout<<"\nNop! C++ isn't my type.";
        cout<<"\nIts annoying.";
        cout<<"\nC++ doesn't suits me.";
    }
    cout<<endl;
    system("PAUSE");
}

Its not that we only can execute cout statements in it. Any statement that do not flashes error can be executed including arithematic operations as well. Here's the output of the program.






<<if else Statement                                           Home                                      Loops In C++>>

if-else Statements

if-else Statements

    Till now,we just threw light on uses of 'if'. Now its about time we should head for the counter part of 'if' i.e. 'else'. Its very easy to take down this topic in one shot.  So we'll discuss just a single program regarding this topic.
    What basically 'else' refers to is the left over of 'if'. In simple language, consider we write our 'if' condition as if(i<8), then this 'if' will include all the values of i which are smaller than 8(not including 8). Now the left over of this 'if' are the values of i including 8,9,10 ......and all above values.These values are covered in else.The syntax of writing 'if' with 'else' is as follows:

if(condition)
    statement 1;
else
    statement 2;

Remember, unlike 'if' statements we do not need to write any condition in 'else' statements which are covered by a pair of brackets. All the conditions which are not a part of 'if' are automatically covered in 'else'. In the last example we read in 'if' topic which had a logical NOT operator (!), the second 'if' can be replaced by else.Why is it so you ask? It is because all the conditions not satisfied by 'if' were FALSE. And NOT operator changes all these FALSE conditions to TRUE. So indirectly it is including all the conditions left by 'if'. Lets consider an example.

#include<iostream>
#include<cstdlib>

using namespace std;

int main()
{
    int a;
    cout<<"\nEnter value of a : ";
    cin>>a;
    if(a==2609)
        cout<<"\n Holla!!!! Thats my birthday";
    else
        cout<<"\nNop! Thats not my BD.";
    system("PAUSE");
    return 0;
}

For creating a link between this program with the one we studied earlier(one having NOT operator), I'll only say,we could have written that program in the following way:

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                             //main function
{
    system("CLS");                     //clears any printed, if any
    int condition;
    cout<<"\nEnter value of condition : ";
    cin>>condition;
    if(condition)        // 1st if
        cout<<"\nYour condition evaluates to TRUE.";
    else                    // Replacement of 2nd if
        cout<<"\nYour condition evaluates to FALSE.";
    system("PAUSE");                  // Freeze the screen to show the output
    return 0;
}

The concept is easy and self explanatory.The next topic we'll be covering is "multiple statement under if and else".

Written By T-Rex
<<'if' Statements                                  Home                              Multiple statement under if-else>>

' if ' Statements

 ' if ' Statements

    'if' is a keyword used in C++ language to implement decision making instructions. The syntax or the general form in which if statements are written as : -

if(our condition)
    command to be executed;

     Here the condition can be given using various methods but the most common of all are relational operators. These operators allow us to compare values and decide whether situation is true or not. A fact that we should know is "Computer understands only binary i.e. 1 or 0".And that is the reason these relational operators always evaluates to either 1 or 0.1 represents TRUE and 0 represents FALSE.
Confused ?? Where these 1 and 0,TRUE and FALSE came from? Are we diverted from our topic? No. Because 'if' keyword operates only on 1 and 0. We will consider this fact very soon by making a program but first take a look at the following table. It shows us the evaluation of relational operators under different conditions.

    Let us now take a look at a program and see how these concepts work. If you don't understand the program then just don't worry about it because I've given an explanation
right after finishing it. We'll be using wx-Dev C++ 7.4.2 to run our programs. You can download this IDE from here.

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                                      //main function
{
    system("CLS");                        //clears any printed, if any
    int m,n,p;
    cout<<"\nEnter value of n : ";
    cin>>n;
    cout<<"\nEnter value of m : ";
    cin>>m;
    if(n<m)
        cout<<"\nn is small.";
    if(n>=m)
        cout<<"\nn is equal to or greater than m.";
    system("PAUSE");                   // Freeze the screen to show the output
    return 0;
}
    Since you have read the program(I assume so...), its time for us to grab the concept of it. You remember those 1 and 0. You also read that 'if' only operates on 0 and 1. Here's the explanation for this concept. Consider the input provided to n is 8 and that to m is 20. By looking in the table we can easily figure out that the expression 'n<m' will evaluate to 1. That means after evaluation computer will read 'if(8<20)' as 'if(1)' because 8<20 evaluated to 1. I also told you that 1 represent TRUE. So the condition after 'if(n<m)' will be executed. As a result of which 'n is small' will be printed on console(screen).  
    Now consider 2nd 'if' statement i.e. 'if(n>=m)'. Our table tells us that 'if(8>=20)' will evaluate to 'if(0)'. Since 0 represents FALSE, this 'if' will not be executed. Also remember the fact that any number other than 0 is always considered as 1. Let us take a program to understand it.

#include <cstdlib>                     
#include <iostream>                     //Header Files
using namespace std;
int main()                             //main function
{
    system("CLS");                     //clears any printed, if any
    int condition;
    cout<<"\nEnter value of condition : ";
    cin>>condition;
    if(condition)        // 1st if
        cout<<"\nYour condition evaluates to TRUE.";
    if(!condition)        // 2nd if
        cout<<"\nYour condition evaluates to FALSE.";
    system("PAUSE");                  // Freeze the screen to show the output
    return 0;
}
    Oops!! Trouble?? So here's its solution. Consider that we input 1 to condition. So clearly our 1st 'if' i.e. if(condition) evaluates to 'if(1)' and hence 'Your condition evaluates to TRUE' is printed on screen. In 2nd 'if', '!' operator is used. This operator is called logical NOT operator. What it does is to convert TRUE to FALSE and vise versa. So 'if(!condition)' i.e. 'if(!1)' evaluates to 'if(0)' and cout is not executed. Exactly opposite happens in the case when 0 is given as input.
    But, what happens when we give inputs other than 1 and 0? In that case, since its a non zero number, computer always assumes it to be TRUE. Suppose we provide
input, say 2609, then 1st 'if' i.e. 'if(condition)' or 'if(2609)' evaluates to 'if(TRUE)' and cout statement is executed. Same way as explained above, our 2nd 'if'
evaluates to 'if(!TRUE)' or 'if(FALSE)'.

    So these were some of the basic knowledge we need to know to understand how this 'if' stuff works. Now its about time that we should proceed to our next topic i.e. 'if-else pair'.
Written by T-REX

<<Decision Making                                 Home                                  if-else Statements>>

Decision Making

Decision Making


    Its obvious tht we all face conditions in our life when we aren't sure about performing some task until we don't have sufficient data to make our decision. These situations are also encountered by programmers when their client isn't sure about the data and asks the programmer to make a program that reacts immediately when input is provided. This is when decision making plays its role. Under this section we are going to cover the following topics:
  •  if-else statements
  •  switch case statements

    Knowledge of these topics will enable you to make programs that can take different actions depending upon the data provided by user, no matter whatsoever the data may be. So lets deal with our first topic i.e. if-else statements.
Written by T-REX


<<Basic Terms In C++                                HOME                                'if' Statements>>