Upload
pilis
View
29
Download
1
Embed Size (px)
DESCRIPTION
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 main() { - PowerPoint PPT Presentation
Citation preview
Help session - C++ Arranged by :
IEEE – NIIT Student Chapter
© IEEE – NIIT Student Chapter
Agenda:
1. Introduction – Hello World2. Variables.3. Input / Output4. Comments.5. Math operators.6. If-else Statement7. Loops (for, while, do-while)
2
Lesson-1
Hello World
© IEEE – NIIT Student Chapter
Hello World
#include <iostream.h>
main(){
cout<<"Hello, World!";}
4
© IEEE – NIIT Student Chapter
Main parts of C++ Programs.
•Preprocessor Directives•The main() function•C++ Statements
5
© IEEE – NIIT Student Chapter
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.
6
© IEEE – NIIT Student Chapter
#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.
7
© IEEE – NIIT Student Chapter
Hello World
#include <iostream.h>
main(){
cout<<"Hello, World!";}
8
© IEEE – NIIT Student Chapter
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.
9
© IEEE – NIIT Student Chapter
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.
10
© IEEE – NIIT Student Chapter
Hello World
#include <iostream.h>
main(){
cout<<"Hello, World!";}
11
© IEEE – NIIT Student Chapter
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.
12
© IEEE – NIIT Student Chapter
REMEMBER
•C++ is a Case Sensitive language
•So, main() and Main() are two different function.
•And “ x “ and “ X “ are two different variables
13
© IEEE – NIIT Student Chapter
You have completed the introduction to the world of
C++!
14
Lesson-2
Variables
© IEEE – NIIT Student Chapter
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".
16
© IEEE – NIIT Student Chapter
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:
17
© IEEE – NIIT Student Chapter
Table of Data types:TYPE Size (Bytes) Range
char 1 0 to 9, or punctuation symbol
int 2 -32768 to 32767
unsigned int 2 0 to 65535
long int 4 -2147483647 to 2147483646
unsigned long int 4 0 to 4294967295
float 4 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 8 1.7 x 10^-308 to 1.7 x 10^+308
long double 10 3.4 x 10^-4932 to 3.4 x 10^+4932
bool 0 or 1 (true or false )
18
© IEEE – NIIT Student Chapter
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;
19
© IEEE – NIIT Student Chapter
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;
20
Lesson-3
Input / Output
© IEEE – NIIT Student Chapter
Commonly used Escape Sequences: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 ‘
22
© IEEE – NIIT Student Chapter
“ endl “ Operator
•The endl stands for end of line•This operator has the same effect as the
escape sequence “ \n “
23
cout
© IEEE – NIIT Student Chapter
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.
25
© IEEE – NIIT Student Chapter
<<
•Put to Operator or Insertion Operator•It directs the output to the output device.
26
© IEEE – NIIT Student Chapter
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.cppcout << “\n I love \”C++\””; // I love “C++”cout << “I am a student of \n BICSE”;cout << “I am a student of “ << endl << “BICSE”;
}
27
cin
© IEEE – NIIT Student Chapter
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.
29
© IEEE – NIIT Student Chapter
>>
•Get From Operator or Extraction Operator
•It gets the input from the input device and assigns it to the variable
30
© IEEE – NIIT Student Chapter
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”
31
© IEEE – NIIT Student Chapter
Remember:
•cout goes with “ << “ and •cin goes with “ >> ”
32
© IEEE – NIIT Student Chapter
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;
}
33
© IEEE – NIIT Student Chapter
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;
}
34
Lesson-4
Comments
© IEEE – NIIT Student Chapter
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 */
36
Lesson-5
Maths Operators
© IEEE – NIIT Student Chapter
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.
38
© IEEE – NIIT Student Chapter
Math Operators
•Most of the more complicated functions, like square roots, are found in “math.h”▫i.e. #include <math.h>
39
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
© IEEE – NIIT Student Chapter
Math OperatorsSymbol 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
40
Lesson-6
if – else statement
© IEEE – NIIT Student Chapter
If – else Statement
if (condition ){
// if condition is true.}else{
// if condition is false.}
42
© IEEE – NIIT Student Chapter
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.
43
© IEEE – NIIT Student Chapter
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.";}
}
44
© IEEE – NIIT Student Chapter
if – else Statement
•Is the last program perfect....???•Or there is something wrong with it...???
45
© IEEE – NIIT Student Chapter
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";}
}
46
© IEEE – NIIT Student Chapter
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...
47
© IEEE – NIIT Student Chapter
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 ”;}
}
48
© IEEE – NIIT Student Chapter
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.";}
}
49
© IEEE – NIIT Student Chapter
Nested If-elseif (condition-1 ){
if (condition -2){
// if condition-2 is true.}else{
// if condition-2 is false}
}
50
Lesson-7
LOOPS
© IEEE – NIIT Student Chapter
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
52
For Loop
© IEEE – NIIT Student Chapter
For Loop
for ( initialization; condition; inc/dec ){
----------
}
54
© IEEE – NIIT Student Chapter
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.
55
© IEEE – NIIT Student Chapter
For Loop
int count = 0;
for (count=0; count<=10; count++){
----------
}
56
© IEEE – NIIT Student Chapter
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.
57
© IEEE – NIIT Student Chapter
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.
58
© IEEE – NIIT Student Chapter
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.
59
© IEEE – NIIT Student Chapter
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; }
}
60
© IEEE – NIIT Student Chapter
output0123456789101112131415Press any key to continue...
61
© IEEE – NIIT Student Chapter
For loop
•If we have to print the same 15 numbers, but in reverse order, i.e. From 15 to 0
•Then...
62
© IEEE – NIIT Student Chapter
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; }
}
63
© IEEE – NIIT Student Chapter
output1514131211109876543210Press any key to continue...
64
© IEEE – NIIT Student Chapter
For Loop
•Now, what if we have to write a table of 2 using this FOR loop...???
65
© IEEE – NIIT Student Chapter
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; }
}
66
© IEEE – NIIT Student Chapter
Output:Table of 2 using For Loop: 2 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 182 x 10= 20Press any key to continue...
67
© IEEE – NIIT Student Chapter
For Loop
•Infinite for loop.
for( ; ; ){
cout << “ $$$ God is ONE $$$ ” << endl;}
68
While Loop
© IEEE – NIIT Student Chapter
While Loop
while( condition ){
------- ------- // incrementing or decrementing the
condition}
70
© IEEE – NIIT Student Chapter
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.
71
© IEEE – NIIT Student Chapter
While Loop
Now, what if we want to write the same table of 2 using this While loop...???
72
© IEEE – NIIT Student Chapter
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++; }
}
73
© IEEE – NIIT Student Chapter
OutputTable of 2 using While Loop: 2 x 1 = 22 x 2 = 42 x 3 = 62 x 4 = 82 x 5 = 102 x 6 = 122 x 7 = 142 x 8 = 162 x 9 = 182 x 10= 20Press any key to continue...
74
© IEEE – NIIT Student Chapter
Example – while loop
•If we have to write a program to find the sum of the following series:
1 + ½ + 1/3 + ¼ + 1/5 + .......... + 1/45 = ??????
75
© IEEE – NIIT Student Chapter
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;
}
76
Do-while loop
© IEEE – NIIT Student Chapter
Do-While Loop
do{
------- ------- // incrementing or decrementing the
condition
} while( condition );
78
© IEEE – NIIT Student Chapter
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.
79
© IEEE – NIIT Student Chapter
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.
80
© IEEE – NIIT Student Chapter
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”;}
81
© IEEE – NIIT Student Chapter
outputdo-while loop starts now:press 0 (zero) to Exit or Press any other key to continue... fpress 0 (zero) to Exit or Press any other key to continue... Tpress 0 (zero) to Exit or Press any other key to continue... 5press 0 (zero) to Exit or Press any other key to continue... 7press 0 (zero) to Exit or Press any other key to continue... ?press 0 (zero) to Exit or Press any other key to continue... 1press 0 (zero) to Exit or Press any other key to continue... 0do-while loop Ends now :-)Press any key to continue...
82
© IEEE – NIIT Student Chapter
83
Any Question...???
© IEEE – NIIT Student Chapter
IEEE-NIIT Student Chapter
•That completes our today’s help session...
•For any further Help, feel free to contact:▫[email protected]
84