Saturday, 8 June 2013

Tokens, Expressions and Control Structures


Tokens

We know that the smallest individual unit in a program is known as token. C++ has the following tokens :-

  • Keywords
  • Identifiers
  • Constants
  • Strings
  • Operators

Keywords

The keywords implement specific C++ language features. They are explicitly reserved identifiers which cannot be used as names for the program varibales or other user-defined program elements.

Given below is the list of the keywords used in C++ , many of which are common to the keywords of C.

  • asm                                                      
  • auto
  • break
  • case
  • catch
  • char
  • class
  • const
  • continue
  • default
  • delete
  • do
  • double
  • else
  • enum
  • extern
  • float
  • for
  • friend
  • goto
  • if
  • inline
  • int
  • long
  • new
  • operator
  • private
  • protected
  • public
  • register
  • return
  • short
  • signed
  • sizeof
  • static
  • struct
  • switch
  • template
  • this
  • threw
  • try
  • typedef
  • union
  • unsigned
  • virtual
  • void
  • volatile
  • while
  • bool
  • const_cast
  • dynamic_cast
  • explicit
  • export
  • false
  • mutable
  • namespace
  • reinterpret_Cast
  • static_Cast
  • true
  • typeid
  • typename
  • using
A major difference between C and C++ is the limit on the length of a variable length. While ANSI C recognises only the first 32 characters of a name, ANSI C++ places no limit on the length of the name.

Constants refer to fixed values that do not change during the course of execution of the program.

Data Types

Data types in C++ can be broadly categorised into three types :-
  •  User Defined
  • Built In
  • Derived Data type
 Thats all the concept for today and now we put the answers to yesterday's problems.

Answers to problems in post 7.

The Questions we had were :-
Q1. Write a program to display the following output using a single cout statement.

•Maths = 90
...•Physics = 77
•Chemistry = 80.
Q2. Write a program to read two numbers from keyboard and display the larger value on the screen.

Q3. Write a program to read the values of a,b and c and display the value of x, where

x = a/b-c
Test your program for the following set of values :-

i) a=250, b=85, c=25;
ii) a=300, b=70, c=70.

Q4. Write a C++ Program that will ask for a temperature in Fahrenheit and diplay its corresponding temperature in Celsius.

And we have the solutions

Question 1 ( Input Cascading concept)

#include<iostream>

using namespace std;

int main()
{
cout<<"Maths= 90"<<"\n"<<"Physics= 77"<<"\n"<<"Chemistry= 80"<<"\n";

return 0;
}.


Question 2 (If else statement)

#include<iostream>

using namespace std;

int main()
{
int number1, number2;

cout<<"\nEnter the first number : ";
cin>>number1;

cout<<"\nEnter the second number : ";
cin>>number2;

if(number1>number2)
cout<<"\nThe greater number = "<<number1<<"\n";

else if (number2>number1)
cout<<"\nThe greater number = "<<number2<<"\n";

else
cout<<"\nBoth the numbers are equal."<<"\n";

return 0;
}


Question 3 (Simple Input/Output with variable)

#include<iostream>
using namespace std;

int main()
{

int a,b,c,x;

cout<<"\nEnter the values of a, b, c : ";
cin>>a>>b>>c;

x=a/b-c;

cout<<"\nThe value of x is = "<<x<<"\n";

return 0;

}


Question 4 ( Implementing the formula)

#include<iostream>

using namespace std;


int main()
{
float tempf, tempc=0 ;

cout<<"Enter the temperate in Fahrenheit: ";
cin>>tempf;

tempc = (tempf-32)*5/9;

cout<<"The Celsius equivalent temperature is : "<<tempc;

return 0;

}.


Assignment for today :-
Try to find out the number of keywords which you find are common between C and C++.

No comments:

Post a Comment

Thank you for commenting.