www.pickatutorial.com Computer Tutorials
Top eBooks: C/C++ | C# | Android | Mathematics | Database | Cloud | Graphics | Networking | Oracle | Hardware | AI
Top Tutorials: C/C++ | C#.NET | PHP MySQL | Java | Java Script | jQuery | HTML | xHTML | HTML5 | VB Script| CSS
Lessons Lesson 5: Conditionals and Logical Operators Bookmark and Share
Home
Lesson 1
Lesson 2
Lesson 3
Lesson 4
Lesson 5
Lesson 6
Lesson 7
Lesson 8
Lesson 9
Lesson 10
Lesson 11
Lesson 12
Lesson 13
Lesson 14
Lesson 15
Lesson 16
Lesson 17
Lesson 18
Lesson 19
Lesson 20
Lesson 21
Lesson 22
Lesson 23
Lesson 24
Lesson 25
Lesson 26
Lesson 27
Lesson 28
Lesson 29
Lesson 30
Lesson 31
Lesson 32
Lesson 33
Lesson 34
Lesson 35
Lesson 36
Lesson 37
Lesson 38
Lesson 39
Lesson 40
In every day life, we are often making decisions. In the previous lessons, we have written simple elementary programs. For writing interesting and useful programs, we have to introduce the decision making power in them. Now we will see what kind of decisions are there in C/C++ programming language and how these can be used.

The statement used for decisions in 'C' language is known as the 'if statement'. The if statement has a simple structure. That is:

if (condition)
{
statement;
statement;
.
.
statement;
}

The above statements mean, If condition is true, then execute the statement or a group of statements. Here the condition is a statement which explains the condition on which a decision will be made. We can understand it from the example that A can become the member of the basket ball team if he has a height more than six feet .In this case, the condition will be if (A’s height is greater than six feet) A can be a member of team We have written the condition in English language. Now let's see how we can implement this in terms of variables, operators and C statements. In the program, we will write the condition in parentheses, followed by a statement or group of statements to be executed. Now here is the concept of block of statements. We use braces { } to make a group (block) of a number of statements. We put ‘{’ before first statement and ‘}’ after the last statement.

Let's consider a simple example to explain the if statement. Suppose, we have ages of two students (say for the time being we have got these ages in variables). These variables are age1 and age2. Now we say that if the age1 is greater than age2, then display the statement ‘Student 1 is older than student 2’.

#include <iostream.h>
main()
{
int age1, age2;
age1 = 12;
age2 = 10;
if (age1 > age2)
cout << “Student 1 is older than student 2”;
}
Here, in our code we see a new operator i.e. ‘ > ‘ (greater than) in the if statement. We need such operators while making decisions. These operators are called 'relational operators'.

Greater than >
Equal to = =
Less than <
Greater than or equal to >=
Less than or equal to <=
Not equal to !=
Note that there is no space between = =, >=, <= and !=. These are considered as single operators.
The operator == (equal to) is different from the operator =.

If/else Structure

We have seen that the if structure executes its block of statement(s) only when the condition is true, otherwise the statements are skipped. The if/else structure allows the programmer to specify that a different block of statement(s) is to be executed when the condition is false. The structure of if/else selection is as follows.
if ( condition)
{
statement(s);
}
else
{
statement(s);
}

Thus using this structure we can write the construct of our program as
if (a >= b)
{
cout << " a is greater than or equal to b";
}
else
{
cout << " b is greater than a";
}
In this construct, the program checks the condition in if statement .If the condition is true, then the line "a is greater than b" is printed. Otherwise (if condition is not true), the statement related to else is executed and the message "b is greater than a" is printed.

Logical Operators

There are many occasions when we face complex conditions to make a decision. This means that a decision depends upon more than one condition in different ways. Here we combine the conditions with AND or OR. For example, a boy can be selected in basket ball team only if he is more than 18 years old and has a height of 6 feet. In this statement a boy who wants to be selected in the basket ball team must have both the conditions fulfilled. This means that AND forces both the conditions to be true. Similarly we say that a person can be admitted to the university if he has a BCS degree OR BSC degree. In this statement, it is clear that a person will be admitted to the university if he has any one of the two degrees.

In programming we use logical operators ( && and || ) for AND and OR respectively with relational operators. These are binary operators and take two operands. These operators use logical expressions as operands, which return TRUE or FALSE. The && operator has a higher precedence than the || operator. An expressions containing && or || is evaluated only until truth or
falsehood is known. Thus evaluation of the expression (age > 18) && (height > 6) will stop immediately if age > 18 is false (i.e. the entire expression is false) and continue if age > 18 is true (i.e. the entire expression could still be true if the condition height > 6 is true ).

There is another logical operator that is called logical negation. The sign ! is used for this operator. This operand enables a programmer to ‘reverse’ the meaning of a condition. This is a unary operator that has only a single condition as an operand. The operator ! is placed before a condition. If the original condition (without the ! operator) is false then the ! operator before it converts it to true and the statements attached to this are executed. Look at the following expression

if ( ! (age > 18 ))
cout << “The age is less than 18”;

Here the cout statement will be executed if the original condition (age > 18) is false because the ! operator before it reverses this false to true.




Home - Advertise - Contact - Disclaimer - About Us
© Since 2006 pickatutorial.com -- All Rights Reserved.