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

Slides:



Advertisements
Similar presentations
Lecture 2 Introduction to C Programming
Advertisements

Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Introduction to C Programming
 2007 Pearson Education, Inc. All rights reserved Introduction to C Programming.
CS31: Introduction to Computer Science I Discussion 1A 4/2/2010 Sungwon Yang
Libraries Programs that other people write that help you. #include // enables C++ #include // enables human-readable text #include // enables math functions.
Chapter 2: Introduction to C++.
Introduction to C Programming
Basic Elements of C++ Chapter 2.
C++ Programming Language Day 1. What this course covers Day 1 – Structure of C++ program – Basic data types – Standard input, output streams – Selection.
Introduction to C++ Programming
Chapter 01: Introduction to Computer Programming
Introduction to C++ - How C++ Evolved Most popular languages currently: COBOL, Fortran, C, C++, Java (script) C was developed in 1970s at AT&T (Richie)
 2003 Prentice Hall, Inc. All rights reserved. 1 Introduction to C++ Programming Outline Introduction to C++ Programming A Simple Program: Printing a.
Input & Output: Console
CS161 Topic #21 CS161 Introduction to Computer Science Topic #2.
CS1 Lesson 2 Introduction to C++ CS1 Lesson 2 -- John Cole1.
Program A computer program (also software, or just a program) is a sequence of instructions written in a sequence to perform a specified task with a computer.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 2-1 Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Introduction to C Programming Angela Chih-Wei Tang ( 唐 之 瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan 2010 Fall.
Week 1 Algorithmization and Programming Languages.
C++ Programming: Basic Elements of C++.
Copyright © 2012 Pearson Education, Inc. Chapter 2: Introduction to C++
Fundamental Programming: Fundamental Programming Introduction to C++
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
C++ Basics C++ is a high-level, general purpose, object-oriented programming language.
THE BASICS OF A C++ PROGRAM EDP 4 / MATH 23 TTH 5:45 – 7:15.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 2: Introduction to C++
A first program 1. #include 2. using namespace std; 3. int main() { 4. cout
CSC 107 – Programming For Science. Announcements.
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
C++ Programming Lecture 3 C++ Basics – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
 2007 Pearson Education, Inc. All rights reserved. A Simple C Program 1 /* ************************************************* *** Program: hello_world.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 2 Introduction to C++
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
 2003 Prentice Hall, Inc. All rights reserved Basics of a Typical C++ Environment C++ systems –Program-development environment –Language –C++
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Basic concepts of C++ Presented by Prof. Satyajit De
Chapter 1.2 Introduction to C++ Programming
Chapter Topics The Basics of a C++ Program Data Types
Chapter 1.2 Introduction to C++ Programming
Chapter 1.2 Introduction to C++ Programming
REPETITION CONTROL STRUCTURE
Chapter 1.2 Introduction to C++ Programming
Chapter 2: Introduction to C++
Chapter 2 - Introduction to C Programming
Computing Fundamentals
Basic Elements of C++.
Introduction to C++ October 2, 2017.
Chapter 2 - Introduction to C Programming
Chapter 2: Introduction to C++
Basic Elements of C++ Chapter 2.
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
2.1 Parts of a C++ Program.
Introduction to C++ Programming
Chapter 2 - Introduction to C Programming
Chapter 2 - Introduction to C Programming
Introduction to C++ Programming
Computing Fundamentals
Chapter 2: Introduction to C++.
C++ Programming Lecture 3 C++ Basics – Part I
Chapter 2 - Introduction to C Programming
Unit 3: Variables in Java
Introduction to C Programming
Presentation transcript:

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() { 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's operations. 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

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

Lesson-2 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'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".

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:

0 to 9, or punctuation symbol Table of Data types: TYPE Size (Bytes) Range char 1 0 to 9, or punctuation symbol int 2 -32768 to 32767 unsigned int 0 to 65535 long int 4 -2147483647 to 2147483646 unsigned long int 0 to 4294967295 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^-4932 to 3.4 x 10^+4932 bool 0 or 1 (true or false )

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;

Lesson-3 Input / Output

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 ‘

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

cout

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

cin

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

Lesson-4 Comments

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

Lesson-5 Maths Operators

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

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

Lesson-6 if – else statement

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() { 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.";

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

Lesson-7 LOOPS

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

For Loop

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

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.

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

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.

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.

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.

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

output 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Press any key to continue...

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

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

output 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 Press any key to continue...

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

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

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

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

While Loop

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

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.

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

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++; }

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

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 = ??????

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;

Do-while loop

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

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.

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.

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

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

Any Question...???

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