ComputerProgramming3.The Nuts and Bolts of C++Learning the C++ language3.TheNuts andBolts of C++(4)10September2008
3. The Nuts and Bolts of C++ Computer Programming Learning the C++ language 3. The Nuts and Bolts of C++ (4) 10 September 2008
ComputerProgramming3.The Nuts and Bolts of C++3.3Expressions,Statements,andBlocks
3. The Nuts and Bolts of C++ Computer Programming 3.3 Expressions, Statements, and Blocks
ComputerProgramming3.The Nuts and Bolts of C++ATypical Program#include<iostream>int abc (...)1functionstatementsreturn1int cde(.:.)(functionreturn1int main()(functionreturn O;力
3. The Nuts and Bolts of C++ Computer Programming #include <iostream> int abc (.) { . ; return . ; } int cde (.) { . ; return . ; } int main() { .; return 0; } A Typical Program function function function statements
ComputerProgramming3.The Nuts and Boltsof C++StatementsAC++program comprisesanumberoffunctions.Afunction comprises a number of statements.Astatement canbeas simpleas?Can beas complicatedasif (choiceSundayllchoiceSaturday)cout << "\nYou're already off on weekends!\n";A statement must end with a semicolon;In a statement, space carries no information.b+x=a+b;ax=;=
3. The Nuts and Bolts of C++ Computer Programming • A C++ program comprises a number of functions. • A function comprises a number of statements. • A statement can be as simple as • Can be as complicated as • A statement must end with a semicolon ; • In a statement, space carries no information. Statements x = a + b; if (choice == Sunday || choice == Saturday) cout << "\nYou're already off on weekends!\n"; x = a + b; x = a + b ;
ComputerProgramming3.The Nuts and Bolts of C++ExpressionsA statementcomprises one ormanyexpressionsAnything that returns a value is an expression.Examples of expressions3.2I// Returns the value 3.2// Returns a constant, say 87studentPerclass// Here, two expressionsx=a+b;lReturn a + b and return the value ofa variable xSince x = a + b is an expression (i.e.return a value),itcan certainly be assigned to anothervariable.Forexample,// assign y to the value of x which isy=x=a+b;// equal tothesum ofa andb
3. The Nuts and Bolts of C++ Computer Programming Expressions • A statement comprises one or many expressions. • Anything that returns a value is an expression. • Examples of expressions 3.2 // Returns the value 3.2 studentPerClass // Returns a constant, say 87 x = a + b; // Here, two expressions // Return a + b and return the value of a variable x • Since x = a + b is an expression (i.e. return a value) , it can certainly be assigned to another variable. • For example, y = x = a + b; // assign y to the value of x which is // equal to the sum of a and b