Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Help session - C++ Arranged by : IEEE – NIIT Student Chapter."— 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-1 Hello 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's operations. 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-2 Variables

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't be any reserved 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 0 to 9, or punctuation symbol
Table of Data types: TYPE Size (Bytes) Range char 1 0 to 9, or punctuation symbol int 2 to unsigned int 0 to 65535 long int 4 to unsigned long int 0 to float 3.4 x 10^-38 to 3.4 x 10^+38 long float 8 1.7 x 10^-308 to 1.7 x 10^+308 Double long double 10 3.4 x 10^ to 3.4 x 10^+4932 bool 0 or 1 (true or false )

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-3 Input / Output

22 Commonly used Escape Sequences:
Explanation \a For alert or alarm \t ‘ t ’ stands for Tab \n ‘ n’ stands for New Line \\ Used to print Single Backslash ‘ \ ‘ \\” Used to print “ \’ Used to print ‘

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-4 Comments

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-5 Maths 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> Function How it looks in C++ tan(double x) tan(3.14) // number must be in radians. sin(double x) sin(3.14) // number must be in radians. cos(double x) cos(3.14) // number must be in radians. sqrt(double x) sqrt(56) // it will return the square root of 56 pow(double x, double y) pow(10,2) // it will return 10^2 = 100 abs(int x) abs(-90) // returns the +ve value i.e. +90

40 Math Operators <= => == != || && < > Symbol What it means
Less than or equal to. Usually comparing a variable to either another variable or a real number. => Greater than or equal to. Again, usually comparing a variable to either another variable or a real number. == Is equal to. This is different than the single "=" sign because it takes a variable and sees if it is the same as the following *without* changing the variable's value != Is NOT equal to. Same as the 2 equal signs except negatory || “Logical OR”. Used in if() statements (covered later) or any test statement. && “Logical AND". Used, again, in if() statements and any testing statements < Less than. Used mostly in math statements > Greater than. Used mostly in math statements

41 Lesson-6 if – 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() { int inputUser; 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

51 Lesson-7 LOOPS

52 Loops: A statement or a set of statements that is executed repeatedly, is called a loop. And these statements are executed until the given condition is true. There are 3 Standard loops used in C++ For Loop While Loop Do-While Loop

53 For Loop

54 For Loop for ( initialization; condition; inc/dec ) { ----- }

55 For Loop "for" loops are loops that use variables to declare the starting point and ending point, or when the loop will stop looping and happening. To use a "for" loop, you need to make an integer. So, define your integer variable in the function you will use it. It can be called anything (remember good variable naming etiquette). Now, I will create a model of a "for" loop.

56 For Loop int count = 0; for (count=0; count<=10; count++) { }

57 For Loop - for (count=0; count<=10; count++)
We first start off with the word "for", obviously saying it's a for loop. We then add the parentheses as with any function and include all the contents and "schematics" of the for loop. count is the variable that I told you to define. It is best to always have a variable equal to zero when you start, but it really depends on what you want to accomplish with the loop. For now, it‘s equal to zero. We then add a semicolon (";") to end that part of the function. Next, we tell it what the variable needs to reach to stop.

58 For Loop - for (count=0; count<=10; count++)
This part shows the variable name (count) and 2 plus signs. The 2 plus signs mean to add 1 to the variable every time the loop completes one time. That's how the variable makes it to 10, or whatever the ending number is.

59 For Loop - for (count=0; count<=10; count++)
Its the condition, and this condition is checked every time in a loop. If this condition is satisfied then the loop will execute one more time. But if the condition become false, then loop will be quit. And control will be transferred to the next statement written right after the closing bracket of the for loop.

60 Simple for loop... #include <iostream.h> main() {
// to print the numbers from 0 to 15 for(int i = 0; i <= 15; i++) cout << i << endl; }

61 output Press any key to continue...

62 For loop If we have to print the same 15 numbers, but in reverse order, i.e. From 15 to 0 Then...

63 Simple for loop... #include <iostream.h> main() {
// to print the numbers from 15 to 0 for(int i = 15; i >= 0; i--) cout << i << endl; }

64 output Press any key to continue...

65 For Loop Now, what if we have to write a table of 2 using this FOR loop...???

66 Example – For Loop #include <iostream.h> main() { cout << “Table of 2 using For Loop: \n”; for(int i = 1; i <= 10; i++) cout << “2 x “ << i << “ = “ << (i * 2)<< endl; }

67 Output: Table of 2 using For Loop: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10= 20 Press any key to continue...

68 For Loop Infinite for loop. for( ; ; ) { }
cout << “ $$$ God is ONE $$$ ” << endl; }

69 While Loop

70 While Loop while( condition ) { -------
// incrementing or decrementing the condition }

71 While Loop While loops are extremely simple.
To use while loops, you again have to have a variable to test. For example, we will use an integer called “value” We will first initialize it to some value, say zero or one, depending on the requirement. Then condition will be in the while statement (as described in for loop) And increment/decrement of the declared variable will be done somewhere in the body of the loop.

72 While Loop Now, what if we want to write the same table of 2 using this While loop...???

73 Example - While Loop #include <iostream.h> main() {
int value = 1; cout << “Table of 2 using While Loop: \n”; while( value <= 10 ) cout << “2 x “ << value << “ = “ << (value * 2)<< endl; value++; }

74 Output Table of 2 using While Loop: 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6
Press any key to continue...

75 Example – while loop If we have to write a program to find the sum of the following series: 1 + ½ + 1/3 + ¼ + 1/ /45 = ??????

76 Example – while loop #include <iostream.h> main() { float sum = 0.0, c = 1.0; while( c <= 45) sum = sum + 1/c ; c ++; } cout << “Sum of Series = ” << sum;

77 Do-while loop

78 Do-While Loop do { -------
// incrementing or decrementing the condition } while( condition );

79 Do-While Loop Do loops are loops that will happen at least once, and then test a variable to see if it should continue looping or not. Do loops are somewhat like for loops, but not too much. First, we open up and start the loop by putting "do" followed by an opening brace. Now, the loop is going to happen at least once until we get to the "while()" statement.

80 Do-While Loop Next, we put in the loop contents, followed by a closing brace. Next, we put the while() statement, which will make the loop keep happening as long as the condition in the while statement is satisfied. Then after that, we put a semicolon to end the loop. Remember, you must remember that in a do loop, the loop completes once and then goes to the while() statement and then sees if it should continue or not. But "for" loop doesn't happen unless the variable test is true.

81 Example - Do-While Loop
#include <iostream.h> int main() { int check = 1; cout << “do-while loop starts now: \n”; do cout << “\n press 0 (zero) to Exit or Press any other key to continue...”; cin >> check; }while( check != 0 ); cout << “\n do-while loop Ends now :-) \n”; }

82 output do-while loop starts now: press 0 (zero) to Exit or Press any other key to continue... f press 0 (zero) to Exit or Press any other key to continue... T press 0 (zero) to Exit or Press any other key to continue... 5 press 0 (zero) to Exit or Press any other key to continue... 7 press 0 (zero) to Exit or Press any other key to continue... ? press 0 (zero) to Exit or Press any other key to continue... 1 press 0 (zero) to Exit or Press any other key to continue... 0 do-while loop Ends now :-) Press any key to continue...

83 Any Question...???

84 IEEE-NIIT Student Chapter
That completes our today’s help session...  For any further Help, feel free to contact:


Download ppt "Help session - C++ Arranged by : IEEE – NIIT Student Chapter."

Similar presentations


Ads by Google