1. Home
  2. Computing & Technology
  3. C / C++ / C#

C++ Tutorial - About Expressions and Statements

By David Bolton, About.com

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.
Explore C / C++ / C#
About.com Special Features

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

Easy ways to connect two computers for networking purposes. More >

  1. Home
  2. Computing & Technology
  3. C / C++ / C#
  4. C++
  5. Learn C++ Programming
  6. C++ Tutorial - About Expressions and Statements>

©2009 About.com, a part of The New York Times Company.

All rights reserved.