21
Placement Preparation C++ Basics Shreyas Basarge

Lecture 1: basic syntax

Embed Size (px)

DESCRIPTION

This slide is part of course designed for placement preparation for students of IIT Guwahati.

Citation preview

Page 1: Lecture 1: basic syntax

Placement PreparationC++ BasicsShreyas Basarge

Page 2: Lecture 1: basic syntax

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

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

Motivation

Page 5: Lecture 1: basic syntax

Hello World

Your first fully functional program:

Page 6: Lecture 1: basic syntax

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

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

Variables

Example: Find square of a number

Page 9: Lecture 1: basic syntax

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

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

Variables

Example: Find hypotenuse of a right angled

triangle.

Page 12: Lecture 1: basic syntax

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

if-else

Page 14: Lecture 1: basic syntax

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

if-else

Page 16: Lecture 1: basic syntax

loops

Example: display all numbers from 1 to 20

Page 17: Lecture 1: basic syntax

loops

Demo: Sum numbers from 1 to n.

Page 18: Lecture 1: basic syntax

loops

The ‘for loop’

Page 19: Lecture 1: basic syntax

loops

Demo: Sum of all even numbers from 1 to 100

Page 20: Lecture 1: basic syntax

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

Examples

4. Print fibonacci series

5. Find square root of x