Showing posts with label else. Show all posts
Showing posts with label else. 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>>

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