850 likes | 1.03k Views
Help session - C++. Arranged by : IEEE – NIIT Student Chapter. Agenda:. Introduction – Hello World Variables. Input / Output Comments. Math operators. If-else Statement Loops (for, while, do-while). Lesson-1 Hello World. Hello World. #include <iostream.h> main() {
E N D
Help session - C++ Arranged by : IEEE – NIIT Student Chapter
Agenda: • Introduction – Hello World • Variables. • Input / Output • Comments. • Math operators. • If-else Statement • Loops (for, while, do-while)
Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }
Main parts of C++ Programs. • Preprocessor Directives • The main() function • C++ Statements
Preprocessor directive • Anything in C++ that starts with # symbol is called a "preprocessor directive". • What is a preprocessor directive…??? • thing the compiler does before reading the code. • e.g. #include <iostream.h> • "#include" tells the compiler to take the file "iostream.h" and make the functions in it ready for use.
#include <iostream.h> • Now, what is <iostream.h> ….??? • It is a header file and it defines all the functions required for input/output. • You see "cout"? That is one of them.
Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }
main() • "main" is a *required* function in every program. • If you don't have it in your programs, the compiler will have an error and it won't compile. • That is because "main" starts the program'soperations. • It can be anywhere in your program, but it must be there.
C++ Statements - { ....... } • The statements of the program written under the main() function between the curly braces { } • “ { “ starts the function, and it starts executing until the end bracket “ } “ is reached. • Anything written in between these curly braces is called the body of the function.
Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }
cout << "Hello, World!"; • " cout << " is how you output something to the screen. • Everything after the << and inside the double quotes is printed to the screen. • So here, the quotes are taken out, as they just hold the text to print, and Hello, World! • Will be printed to the screen exactly like that.
REMEMBER • C++ is a Case Sensitive language • So, main() and Main() are two different function. • And “ x “ and “ X “ are two different variables
Variables • Variables are names of memory slots that store information based on the data type. • A variable name could be anything, but it can'tbe anyreserved words (e.g. cout, cin, for, while, int, main, switch, case, class etc.) • must start with a letter, can't have spaces, and only letters, digits, and an underscore ( _ ) may be used. • Try to avoid unnecessary words in the variable name, like "bigfatlongconfusingnumber" or unprofessional names like "something".
Variables • Now, all variables are used differently based on the type they are. • Here are basic variable types and their abilities of storing what type of information:
Variables • Now, if I wanted the user to type in one letter, I could make a new variable to store the input like this: char input; • In C++, you can have multiple variables on one line. Of course, they all have to be the same type and are separated by commas. Example of multiple variables on one line: int a, b, c=2, d;
Some more examples: • int a; • char niit, nust, name = ‘m’; • long int product; • float division_result; • double sum; • unsigned long int length = 0; • bool condition = true;
“ endl “ Operator • The endl stands for end of line • This operator has the same effect as the escape sequence “ \n “
cout • Cout stands for Console Output • Console represent the computer display screen • It is used as an output statement to display output on the computer screen • It is a part of iostream header file.
<< • Put to Operator or Insertion Operator • It directs the output to the output device.
Example - cout #include <iostream.h> main() { int pi = 3.14; cout << “C++ is a powerful programming language \n”; cout << “UNIX Operating System is written in C++” << endl ; cout << “It is an easy language to learn.”; cout << “Value of Pi = ” << pi << endl; cout << “C:\\hello.cpp”; // C:\helo.cpp cout << “\n I love \”C++\””; // I love “C++” cout << “I am a student of \n BICSE”; cout << “I am a student of “ << endl << “BICSE”; }
Input - cin • cin stands for Console Input • It is used as input statement to get input from the keyboard during the execution of program. • It is a part of iostream header file.
>> • Get From Operator or Extraction Operator • It gets the input from the input device and assigns it to the variable
Input - cin • Now, how to get the input to the variable? • Easy! char input; cout << "Enter 1 letter: "; cin >> input; • Now any character which we will enter from the key board will save in the above variable “input”
Remember: • cout goes with “ << “ and • cin goes with “ >> ”
Examples - cin #include <iostream.h> main() { int num_1, num_2, sum, product; cout << “Enter First number : ”; cin >> num_1; cout << “Enter Second Number”; cin >> num_2; sum = num_1 + num_2; product = num * 2; cout << “Sum = ” << sum; cout << “product = “ << product; }
Example - cin • Now, if you want 3 letters inputed, you can define 3 char's and use them in the cin>> function asking the user for 3 variables. #include <iostream.h> void main() { char in1, in2, in3; cout<<"Enter 3 letters: "; cin>>in1>>in2>>in3; cout<<"\nThe letters you have inputed are” <<in1<<in2<<in3; }
Comments • Comments are in about every programming language. • They are there to add a comment about whats going on in the code. • The compiler ignores them. • In C++, there are 2 different styles of comments: • // This is a single line comment. • /* This is a multi line comment and anything written in between this, will be considered as a comment and it will be ignored during compilation */
Math Operators • In C++, math is a very rich and large section. • C++ can do just about anything in math. • It can find square roots, do scientific calculator commands, find remainders, use decimals, and more. • Most of the symbols are the same, except for 3: • multiplication ( e.g. 8 * 2 = 16 ) • division ( e.g. 8 / 2 = 4 ) • modulus ( e.g. 8 % 3 = 2 ) • Modulus is a method that finds the remainder of numbers.
Math Operators • Most of the more complicated functions, like square roots, are found in “math.h” • i.e. #include <math.h>
If – else Statement if (condition ) { // if condition is true. } else { // if condition is false. }
If – else Statement • In C++, there comes a time in which the program has to make decisions based on what is currently happening. • It is just like in real life. • That is what the if() statement is for. • It pretty much explains itself. • Let's start.
Example - If – else Statement #include <iostream.h> int main() { int batch; cout << "Enter '1' for BICSE or '2' for BIT: "; cin >> batch; if(batch == 1) { cout<<"\nYou are a student of BICSE.“; } else { cout<<"\nYou are a student of BIT."; } }
if – else Statement • Is the last program perfect....??? • Or there is something wrong with it...???
If – else Statement #include <iostream.h> int main() { int batch; cout << "Enter '1' for BICSE or '2' for BIT: "; cin >> batch; if(batch == 1) { cout<<"\nYou are a student of BICSE.“; } else if (batch == 2) { cout<<"\nYou are a student of BIT."; } else { cout<<"\nYou are neither BICSE nor BIT Student"; } }
If – else Statement • And its not necessary that you have to use both if and else together. • You can also use only if, it totally depends on the requirement of the program. • E.g. See this case...
Example - If – else Statement #include <iostream.h> int main() { int batch; cout << "Enter '1' for BICSE or ‘2' for Mechanical or 3 for Software Engr or 4 for BIT: "; cin >> batch; if(batch == 1 || batch == 4) { cout<<"\nYou are from NIIT.“; } if (batch == 2) { cout<<"\nYou are from EME."; } if (batch == 3) { cout<<"\nYou are from MCS."; } else { cout << “\nYou are not a student of NIIT, EME or MCS ”; } }
Example - If – else Statement #include <iostream.h> int main() { intinputUser; cout<<"Enter an integer: "; cin>>inputUser; if ( inputUser > 50 && inputUser < 100 ) { cout<<"\nYou entered an integer greater than 50 but less than 100.“; } else if ( inputUser > 100 && inputUser < 200 ) { cout<<"\nYou entered an integer greater than 100 but less than 200"; } else { cout<<"\nYou entered an integer that is not between 50 and 200."; } }
Nested If-else if (condition-1 ) { if (condition -2) { // if condition-2 is true. } else { // if condition-2 is false } }