100 likes | 260 Views
Tutorial 2. By : Shruti Rathee. Identifiers. The names of the things that appear in program is known as identifier. It must start with a letter or an underscore. It cannot start with a digit. An identifier cant be the same name of reserved keywords such as else, enum,do,double . Example :.
E N D
Tutorial 2 By : ShrutiRathee
Identifiers • The names of the things that appear in program is known as identifier. • It must start with a letter or an underscore. It cannot start with a digit. • An identifier cant be the same name of reserved keywords such as else, enum,do,double.
Example : Here ``i`` and ``sum`` are the identifiers. • #include <iostream> using namespace std; int main() { inti,sum; cout << “enter value for i" ; cin >>i; cout << “enter value for sum" ; cin >>sum; cout << “The value for i is" <<i<<endl; cout << “The value for sum is" <<sum<<endl; return 0; }
Variables • These are used to store the values that can be used later in the program. • They are called variables as their value can be changed.
Example : • #include <iostream> using namespace std; int main() { inti; i=10; cout << “i = " <<i<<endl; i=5; cout << “i = " <<i<<endl; return 0; } Here the output will be something like : i=10 i=5
Example • int a = 2; // initializing a. • byte b = 45; // initializes b. • double pi = 3.14159; // declares an approximation of pi. • char x = 'x'; // the variable x has the value 'x'. • For example refer program from Dr. Talla Website
Loops in C++ • There are some kind of loops we have in c++ like : • The while loop. • The do-while loop. • The for loop. • In this tutorial we will cover for loop.
For loop • The syntax of for loop is : for (initial-action;loop-continuation-condition;action-for-each-iteartion) { statement (a); } • Simply we can use it as : for (i=initial value; i<endvalue;i++) { // whatever you want to do in your loop }
Example • inti; for (i=0;i<10;i++) { cout<<“Hi good evening\n”; } this will print Hi good evening 10 times.