Professional Documents
Culture Documents
Expression Result
6 == 4 0 (false)
6 == (2* 3) 1 (true)
The != operator produces a nonzero value (true) if the left opennd is not equal
to the right operand. Otherwise it produces a zero (false).
Logic operators
Logic operators include &&, II, and !.
The && operator is the C++ logical AND operator. If two
operands are nonzero (true), then the entire expression is
nonzero (true). Otherwise the expression is zero (false).
The II operator is the C++ logical OR operator. If two
operands are zero (false), then the entire expression is zero
(false). Otherwise the expression is nonzero (true).
The ! operator is C++ logical NOT operator. If the operand
is nonzero (true), the expression is Zero (false). If the
operand is zero (false), the expression is nonzero (true).
Condition operator
The condition operator looks like:
expression1 ? expression2 : expression3
If expression1 is nonzero (true), the value of the entire expression is the value
of expression2. If expression1 is zero (false), the value of the entire expression
is the value of expression3.
Examine the following program:
void main()
{
int a, b, c;
a = 5;
b = 4;
c = (a > b ? 3 : 10); /* c = 3 */
c = (a == b ? 14 : 0); /* c = 0 */
c = (a <= b ? 25 : 12); /* c = 12 */
}
ARITHMETIC OPERATO RS
DESCRIPTION
Turbo C++ provides the following arithmetic operators:
unary operators
- unary minus (negation)
+ unary plus
-- decrement
++ increment
binary operators
+ addition
- subtraction
. multiplication
/ division
% modulo
The unary minus (-) makes a positive number into a negative number and a negative
number into a positive number.
The unary plus (+) makes a negative number into a positive number.
The decrement and increment operators can be prefix (++a) or postfix (a++).
Prefix means that Turbo C++ first increments or decrements the variable by 1.
Postfix means Turbo C++ uses the value of the variable and then increments or
decrements the variable by 1. For example:
n += 2 n = n + 2; n -= 2; n = n - 2;
n *= 2; n = n * 2; n /= 2; n = n / 2;
n %= 2; n = n % 2; n «= 2; n = n « 2;
n »= 2; n = n » 2; n &= 2; n = n & 2;
n 1;
IF-ELSE
Like all programming languages, C++ gives you a way of having your program make
decisions. In C++, decisions are made with the if-else statement. There are three main
variants:
•if (condition) /* variant #1 */
if_statements;
•if (condition) /* variant #2 */
if_statements;
else
else_statements;
•if (condition 1) /* variant #3 */
if 1 statements;
else if(condition2)
if 2 statements;
else --
else_statements;
Variant #1 uses a single if condition, followed by one or more if_statements. If
the condition is true, the program executes the if_statements. Otherwise, it
ignores the if_statements.
Variant #2 uses an if condition followed by an else condition. If the condition is
true, the program executes the if_statements, then execution continues with the
first statement after the if-else structure.
If the condition is false, the program\executes the else_statements, then
execution continues with the first statement after the if-else structure.
Variant #3 uses two or more if conditions and an else condition. If the first
condition is true, the program executes the if_statements followed by the first
statement after the if-else structure.
If the first condition is false, the program tests if condition2 is true. If it is true,
the program executes the if_2_statements. Otherwise, any following conditions
are tested until an else is encountered. If none of the conditions have been met,
the program executes the else statements.
Conditions must evaluate to a single numeric value. If it evaluates to 0, it is
considered FALSE; any other value is TRUE.
Conditions can consist of relationships, variables, or routines.
When you compare two values together, they can be related to each other in
the following ways:
Binary Boolean Relations
a<b a is less than b
a <= b a is less than or equal to b
a == b a is equal to b
a != b a is not equal to b
Beginners often make the mistake in using a single = sign to compare two
values. For example, the following two statements perform completely
different functions:
a = b This sets the value of 'a' equal to the value of 'b'
a == b This compares the value of 'a' to the value of 'b'
You can join multiple relations together using the following operators:
!a, && , II
DO- WHILE
The do control structure provides the do-while loop, one of the three ways of
looping in structured programming (the other two are while and the for loop).
A do-while loop always executes at least once. (This differs from a while
loop which may never execute at all.)
The syntax of a do-while loop is:
do
{
statement1;
}
while(condition);
statement2;
.
The do-while loop first executes any statements enclosed within the do-while
loop. In the above example, statement1 represents one or more statements.
After executing the statements once, the program tests the condition by the
while statement. If condition is true, the statements execute again. When
condition is false, control transfers to any statements directly following the
do-while loop. In the above example, statement2 represents one or more
statements outside the do-while loop.
To avoid an endless loop (when the condition always remains true), make
sure that the statements within the do-while loop change the condition.
Otherwise your program will go into an endless loop and not work correctly
FOR
The for loop repeats one or more statements a specific number of times. To do
this the for statement uses an initial statement, a condition, and a modifying
statemenl (which changes something in the condition unless the condition is
self-modifying) The for loop can be particularly useful for processing arrays.The
syntax of the for loop is:
for (initial, condition, modify)
statement(s);
The following example prints the message "Hello, world!" five times
void main()
{
int i;
for(I=0; i<5;i++)
{
printf ("Hello, worldl\n");
}
Use the for loop to repeat a command a specified number of times. The
limit can be specified using either constants (if you know the limit
when you write the program) or variables (if you can determine a limit
when the program is running).
For situations where you can't determine a limit (reading a file, for
example), use a while loop instead
While - Do
• While – do control structure provides the while loop for C. A while loop first
checks for the condition and enters the loop only if the condition is true. ( May
not execute the body – even once)
•While(condition)
{ while_statements; }
SWITCH
• Switch is a more efficient way of nested if-else statements.
• When used control passes to the case label which matches the variables
value. Control continues down the switch statement until a break is reached or
end of switch is reached.
switch(variable)
{
case value1: statement1;
break;
case value2: statement2;
break;
case value3: statement3; break;
default: statement4;
}
BREAK
• break statement transfers control out of a do-while, for , switch
or while statement.
• break statement acts like a limited GOTO found in other
languages.
• break transfers control to the statement immediately following
the control structure, thus prematurely exiting a loop.
While (i > 0)
Count =1
{ printf(“ Count = %d\n”,i++);
/* breaks out of the statement when i >5 */ Count = 2
} Count =4
printf(“ End of program \n”); Count = 5
End of program
Continue
• Continue statement works with a loop control statement ( do-while, for,
while)
• Continue skips the remaining statements in a loop and begins the next
iteration of the loop.
• Use continue whenever you need to execute the next iteration of the loop,
but need to skip certain statements of the loop.
• If you need to exit a loop – use the break statement instead.
do
{ printf(“ Type a number \n”);
Scanf(“%d”,&keyread);
if (keyread == 0) continue;
total = total+ keyread;
printf( “ Sum total = %d\n”,total);
count++
}
while (count < 5);