Code About Us Tutorial


    An Introduction to C++
    Statements & Expression
    Basic Types
    Arrays
    Functions
    Pointers and References
    Basic Classes
    Inheritance
    Templates


   
 

Home > Statements & Expression

November 27, 2009 12:19 am

   

Statements & Expression

See Also:
The while Statement
The do ... while Statement
The for Statement
The break Statement
The continue Statement
The goto Statement
The switch Statement
Keywords
Scope Resolution (::)
Arithmatic Operators
Today we are going to discuss how to use many useful statements and keywords in C++. First let us find out what we mean when we refer to a statement and an expression in the programming world. In C++ a statement does one of three things:
  1. controls the sequence of execution,
  2. evaluates an expression,
  3. or does nothing (the null statement).

Basics

All C++ statements end with a semicolon, if you forget to use the semicolon you will probably get an error saying, "error C2146: syntax error: missing ';'" Here is an example of a statement:
 
 
int y;            // <--
y = a + b;     // <-- notice the semicolon after both statements

You will see many different statements in this section. Lets move onto what an expression is. All expressions are statements, because an expression is said to return a value. A statement, such as y = a + b; assigns the value of the sum of a and b to y.

Let us look at an example of a small application that expresses the idea of expressions.

 
 
#include <iostream.h>
void main()
{
	int b, c;      //you can declare 3 int variables all on 1 line, or on 3 separate lines.
	int output;
	
	cout << "Enter 2 numbers: ";
	cin >> b >> c; 

cout <<"The Value of b: " << b << "n"; cout <<"The Value of c: " << c << "n";

output = b + c;

cout << "output: " << output << "n";

}

Let me explain / describe to you exactly what is happening in the above application. We started off by declaring three int variables, and below that, we assigned them all their own value. You assign values to variables by using the assignment operator (=). We then introduced two new keywords, they are cout and cin. The keyword cout stands for Console output, this refers to what you see on your screen. The keyword cout is an output object of the <iostream> library, we will we will cover libraries in depth at a later stage. For now all you need to know is that you must include the <iostream> library if you want to use the object cout or cin. You use cout whenever you want to display a message on the screen. The << is usually followed by the keyword cout. The keyword cin means Console input. You use cin whenever you want an input from the user. After you use the keyword cin you use the >> symbol. The >> is the extraction operator, also called the input operator. When the statement cin >> b >> c; executes, the system pauses, waiting for input. As soon as an integer is input, it is assigned to the variables b and c and the program will continue. I hope that sums up the input and output objects. The next lines tell the user what numbers they have just entered. The two numbers the user entered are then added to each other and assigned to the output variable. Once the output variable has a value, it is displayed, in this instance, on the console. As a final note on the above code, what we used is an example of what is called Multiple Input in the same stream. The line that reads: cin >> b >> c;

is where we used it. I could have wrote:

 
 
cin >> b;
cin >> c;

but it basically does the exact same thing, the only difference is, you would enter one variable and press enter then enter the next, but the outcome is the same.

The if statement

Now lets move onto the if Statement. The if statement allows conditional execution. Its syntax is:

if (condition) statement;

Where condition is an integer expression and statement is an executable statement. The statement will be executed only if the condition has a nonzero value (anything but a zero). (Whenever an integer expression is being evaluated as a condition, a nonzero value is understood to mean "true" and a zero value to mean "false.")

Here is a simple program that uses the if else statement.
 
 
#include <iostream.h>
void main()
{
	int ScoreP, ScoreM;
	cout <<"Enter the Pirates Final Score: ";
	cin >> ScoreP;
	cout <<"Enter the Mets Final Score: ";
	cin >> ScoreM;

if(ScoreP < ScoreM) cout <<"The Pirates Lost by: " << ScoreM - ScoreP << "Points" << endl; else cout <<"The Pirates Won by: " << ScoreP - ScoreM << " Points" << endl; }

The above code prompts the user to enter two scores, and when the code encounters the if else statement it checks to see which score is higher, if the Pirates Score is less than the Mets Score then it will display the Pirates lost by, followed the amount of points they lost by. If the conditional expression evaluates to false, i.e., the Pirates Score was higher it will Display the Pirates won. The program calculates the number of points the pirates won/lost by in the following lines of the code:

 
 
cout <<"The Pirates Lost by: "  << ScoreM - ScoreP << "Points" << endl;
	else
cout <<"The Pirates Won by: "  << ScoreP - ScoreM << " Points" <<  endl;
where it says
 
 
ScoreP - ScoreM
And
 
 
ScoreM - ScoreP

This does a simple subtraction of the two values. Here is a table of the Relational Operators, in case you are wondering, you can use all of these in a conditional expression.

Fig. 2a: Fig. 2a (figs/fig2a.htm)

Here is a program that will find the highest value of three Integers. (Referred to in C++ terms as the Maximum):

 
 
#include <iostream.h>

void main() { int n1,n2,n3;

cout << "Enter 3 integers: "; cin >> n1 >> n2 >> n3; int max = n1; if (n2 > max) max = n2; if (n3 > max) max = n3; cout << "The maximum is " << max << endl; }

What actually happens when this program is run, you entered 34, 46, and 78, n1 = 34, n2 = 46, and n3 = 78. First max is assigned 34. Then, since 46 is greater than 34, max is assigned 46. Finally, since 78 is greater than 46, max is assigned 78, and that value is displayed. The above code makes use of two if statements. You can make use of as many if statements as you need in a program.Please Rate this Code:


    Comments for: Statements & Expression
MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
By far, statements and expressions hapens to be the basics of most programming languages, I mean, this section is very important in the learning of ANY programming language, but its always good to go learn some logic and math first, right?

-MoMad

MoMad [big_mo_mine@yahoo.com] Posted: 31 times. says:
By far, statements and expressions hapens to be the basics of most programming languages, I mean, this section is very important in the learning of ANY programming language, but its always good to go learn some logic and math first, right?

-MoMad says:
Very good site! Very easy explantions. except page describing cout and cin. there was no reason for the "n" in brackets.





Add Comment:
Name:
Email:


 «12» THINKQUEST TEAM C0111571 © 2001. All Rights Reserved.