1 / 84

Help session - C++

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() {

pilis
Télécharger la présentation

Help session - C++

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Help session - C++ Arranged by : IEEE – NIIT Student Chapter

  2. Agenda: • Introduction – Hello World • Variables. • Input / Output • Comments. • Math operators. • If-else Statement • Loops (for, while, do-while)

  3. Lesson-1Hello World

  4. Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }

  5. Main parts of C++ Programs. • Preprocessor Directives • The main() function • C++ Statements

  6. 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.

  7. #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.

  8. Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }

  9. 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.

  10. 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.

  11. Hello World #include <iostream.h> main() { cout<<"Hello, World!"; }

  12. 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.

  13. REMEMBER • C++ is a Case Sensitive language • So, main() and Main() are two different function. • And “ x “ and “ X “ are two different variables

  14. You have completed the introduction to the world of C++!

  15. Lesson-2Variables

  16. 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".

  17. 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:

  18. Table of Data types:

  19. 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;

  20. 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;

  21. Lesson-3Input / Output

  22. Commonly used Escape Sequences:

  23. “ endl “ Operator • The endl stands for end of line • This operator has the same effect as the escape sequence “ \n “

  24. cout

  25. 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.

  26. << • Put to Operator or Insertion Operator • It directs the output to the output device.

  27. 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”; }

  28. cin

  29. 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.

  30. >> • Get From Operator or Extraction Operator • It gets the input from the input device and assigns it to the variable

  31. 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”

  32. Remember: • cout goes with “ << “ and • cin goes with “ >> ”

  33. 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; }

  34. 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; }

  35. Lesson-4Comments

  36. 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 */

  37. Lesson-5Maths Operators

  38. 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.

  39. Math Operators • Most of the more complicated functions, like square roots, are found in “math.h” • i.e. #include <math.h>

  40. Math Operators

  41. Lesson-6if – else statement

  42. If – else Statement if (condition ) { // if condition is true. } else { // if condition is false. }

  43. 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.

  44. 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."; } }

  45. if – else Statement • Is the last program perfect....??? • Or there is something wrong with it...???

  46. 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"; } }

  47. 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...

  48. 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 ”; } }

  49. 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."; } }

  50. Nested If-else if (condition-1 ) { if (condition -2) { // if condition-2 is true. } else { // if condition-2 is false } }

More Related