1. Computing & Technology

Discuss in my forum

C++ Tutorial - About Expressions and Statements

By , About.com Guide

1 of 8

Expressions and Statements
In Learn about Numbers we saw how to assign values to ints and floats. These assignment statements have a very simple structure.

 Variable = Expression
 
We know what a variable is, i.e. somewhere to store the value of the expression, but just what is an expression? It's a statement that resolves to a value: either numeric or character.

What Exactly is a Statement?

A statement is a building block of a program. For example the assignment statement
 int a=10; 
This is also an expression and has the value 10. If we write this code
 int b= (a=10) ; 
Both a and b will be set to the value 10.

In a later lesson we'll meet other statements such as if or while. Both of these require a conditional expression.

Conditional Expression

A conditional expression is one which evaluates as true (a non zero value) or false (0). True is almost always 1 but any non zero value is also true! A zero value is false, anything else is true.
 #include <iostream>
 
 using namespace std;
 int main() 
 {
    int a=10;
    int b=( a = 10) ;
    int c=( a==10) ;
    cout << "Value of b is " << b << endl ;
    cout << "Value of c is " << c << endl ;
    return 0; 
 
 }
 
Compile and run the example above. It should print out the following.
 Value of b is 10
 Value of c is 1
 
On the next page : Learn about the preprocessor.

©2012 About.com. All rights reserved.

A part of The New York Times Company.