Download - Lecture 1: basic syntax
![Page 1: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/1.jpg)
Placement PreparationC++ BasicsShreyas Basarge
![Page 2: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/2.jpg)
Target Audience
People who have no prior coding experience,
didn’t take CS101 seriously but want to learn
some basics of coding quickly.
If you know how write a program to print
fibonacci numbers, do NOT waste your time by
attending this tutorial.
![Page 3: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/3.jpg)
Motivation
1. Most companies visiting IIT Guwahati
require coding skills.
2. Everyone wants to get placed.
1 & 2 => You have to learn coding
![Page 4: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/4.jpg)
Motivation
![Page 5: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/5.jpg)
Hello World
Your first fully functional program:
![Page 6: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/6.jpg)
Running C++ code
Linux:
1. Install g++
2. g++ file_name.cpp
3. ./a.out
Windows:
1. Open your favorite IDE
2. Write some code
3. Click the ‘play’ button
n00b:
1. Go to ideone.com
2. Write some code. Select language (C++)
3. Click on Run
![Page 7: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/7.jpg)
Variables
Letters in which we can store numbers and other data like
user input.
Example:
int x = 42;
int y;
y = x + 10;
int z;
cin >> z; // whatever user types in is stored in z
cout << z; // whatever is in z is shown on the screen
![Page 8: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/8.jpg)
Variables
Example: Find square of a number
![Page 9: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/9.jpg)
Variables
Demo:
1. Take two integers from the user.
2. Find sum of their cubes. (If user’s numbers
are 2 and 3, output should be
2x2x2 + 3x3x3 = 8 + 27 = 35)
![Page 10: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/10.jpg)
Variables
Variables can store integers, real numbers and
some other things.
int x; // x can store an integer
x = 3;
float y; // y can store a real number
y = 3.14;
![Page 11: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/11.jpg)
Variables
Example: Find hypotenuse of a right angled
triangle.
![Page 12: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/12.jpg)
if-else
When you want to do different
things depending on some
condition.
Example: Write a program to
find absolute value of given
integer.
![Page 13: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/13.jpg)
if-else
![Page 14: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/14.jpg)
if-else
if (x>3 || y<9) { …
if (x>8 && y<4) { …
if (x == 0) { …
if (x*y >= 100) { …
if (x != 7) { ...
![Page 15: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/15.jpg)
if-else
![Page 16: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/16.jpg)
loops
Example: display all numbers from 1 to 20
![Page 17: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/17.jpg)
loops
Demo: Sum numbers from 1 to n.
![Page 18: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/18.jpg)
loops
The ‘for loop’
![Page 19: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/19.jpg)
loops
Demo: Sum of all even numbers from 1 to 100
![Page 20: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/20.jpg)
Examples
1. Check if number is prime
2. Print the pattern:
#
##
###
####
3. Find all triplets that sum to 10
![Page 21: Lecture 1: basic syntax](https://reader034.vdocument.in/reader034/viewer/2022042607/55953afa1a28ab95098b4620/html5/thumbnails/21.jpg)
Examples
4. Print fibonacci series
5. Find square root of x