Lecture 1: basic syntax
-
Upload
vivek-bhargav -
Category
Engineering
-
view
70 -
download
2
description
Transcript of Lecture 1: basic syntax
Placement PreparationC++ BasicsShreyas Basarge
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.
Motivation
1. Most companies visiting IIT Guwahati
require coding skills.
2. Everyone wants to get placed.
1 & 2 => You have to learn coding
Motivation
Hello World
Your first fully functional program:
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
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
Variables
Example: Find square of a number
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)
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;
Variables
Example: Find hypotenuse of a right angled
triangle.
if-else
When you want to do different
things depending on some
condition.
Example: Write a program to
find absolute value of given
integer.
if-else
if-else
if (x>3 || y<9) { …
if (x>8 && y<4) { …
if (x == 0) { …
if (x*y >= 100) { …
if (x != 7) { ...
if-else
loops
Example: display all numbers from 1 to 20
loops
Demo: Sum numbers from 1 to n.
loops
The ‘for loop’
loops
Demo: Sum of all even numbers from 1 to 100
Examples
1. Check if number is prime
2. Print the pattern:
#
##
###
####
3. Find all triplets that sum to 10
Examples
4. Print fibonacci series
5. Find square root of x