0% found this document useful (0 votes)
3 views

Introduction to Programming-1

Uploaded by

prnvbtr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Introduction to Programming-1

Uploaded by

prnvbtr
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 73

By PEC ACM

INTRO TO
PROGRAMMING
WHAT AND WHY?

Programming is the process


of writing instructions for a
computer to perform tasks,
or solve problems.
Problem Solving
Automation
Creativity
Career opportunities
IDE
An integrated development environment(IDE) is a
software application that consists of at least of
source code editor, automation tools and a
debugger.
It helps in creating, editing, compiling, testing
and debugging programs.
Minimizes error
Ease of use
Efficiency

www.reallygreatsite.com
C++ BASIC SNYTAX
Data Types in C++
Integer Float Character
Numbers like 1, 2, Numbers with Single characters enclosed in
decimal points, like single quotes, like 'A', 'b'.
3,0,-1,-2,-3 etc.
1.25, 3.14. keyword = char
Keyword = int
Float can hold up to
7 decimal digits.
Consumes 4 bytes
for storage.
Keyword = float
Data Types in C++
Double Boolean

Double can hold up Stores either True


to 15 decimal or False value.
digits. Consumes 8 True =1
bytes for storage. False=0
Keyword = double Keyword = bool
Data Type Syntax

Integer int a = 10;

Float float b = 12.99;

Character char c = 'A';


Size of Data Types
Here are the sizes of the specified data types in C++:

char : 1 byte
int : 4 bytes
float : 4 bytes
double : 8 bytes
bool : 1 byte
Find the output?
Answer:
Error :
The entry point of the program must be named
''main'', not ''Main''.
Find the output?
Answer:
Error :
This tries to assign c to the result of a + b,
which is incorrect because you cannot assign a
value to an arithmetic expression.
Find the output?
Answer:
Error :
Any non-zero value is considered true
(represented as 1 when output)
Quick Quiz!
Type of variable that stores whole number without decimal.

1) long
2) int
3) string
4) float
Type of variable that stores upto
decimal digits?

1) long
2) int
3) string
4) float
Type of variable that stores True or False value.

1) long
2) int
3) boolean
4) float
The value 132.54 can be represented using which
data type?

a) double
b) void
c) int
d) bool
Operators in C++
1) Arithmetic Operators
These include : +, -, * , /, %
Increment Operator: ++
Decrement Operator: --
OUTPUT :

a + b = 11
a-b=5
a/b=2
a%b=2
2) Relation Operators
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
== Equal to
!= Not equal to
OUTPUT :

a == b is 0
a > b is 1
a >= b is 1
a < b is 0
a <= b is 0
a != b is 1
3) Logical Operators
&& - Logical AND
|| - Logical OR
! - Logical NOT
OUTPUT :

a && b is 1
a ! b is 1
!b is 0
3) Assignment Operators
Fast Input/Output in C++
In competitive programming, it is important to read input as
fast as possible so we save valuable time.It is often
recommended to use scanf/printf instead of cin/cout for fast
input and output. However, you can still use cin/cout and
achieve the same speed as scanf/printf by including the
following two lines in your main() function:
ios_base::sync_with_stdio(false);
cin.tie(NULL);
Moreover, you can include the standard template library (STL)
with a single include:
#include <bits/stdc++.h>

It is recommended to use cout << “\n”; instead of cout << endl;.


endl is slower because it forces a flushing stream, which is
usually unnecessary
So your template for competitive programming could look like this:
Loops in C++

For Loop
While Loop
Do-While Loop
Syntax for Loops
1) For Loop
SAMPLE CODE SNIPPET
OUTPUT :

1
2
3
4
.
.
100
Syntax for Loops
2) While Loop
SAMPLE CODE
OUTPUT :

1
2
3
4
.
.
100
SAMPLE CODE
Syntax for Loops
2) Do-While Loop
OUTPUT :

1
2
3
4
.
.
100
Guess the output?
OUTPUT :

-3
-2
-1
0
1
2
3
Find the output?
OUTPUT :

Infinite Loops will run since i-- is


written instead of i++.
Guess the output?
OUTPUT :

After that i=1+10=11 and since


11<11 is false so loop will not run
after running once.
What is correct syntax of a for loop in C++?

(A) for(initialization;condition; increment/decrement)


(B) for(increment/decrement; initialization; condition)
(C) for(initialization, condition, increment/decrement
(D) None of These
Answer
(A) for(initialization;condition; increment/decrement)
OPTION
a) error
b) 15
c) infinite times of printing n
d) none of the mentioned
Answer: c
Explanation: There is no
condition in the for loop, So it
will loop continuously.
OPTIONS
a) 0 1 2 3 4
b) 10 10 10 10 10
c) 20 20 20 20 20
d) Compile Errors
Answer: c
Explanation: int i = 20 is initialized in the body of loop so it
considers the value i = 20 inside loop only. For loop control, the i
declared outside is used and loop runs 5 times and print 20 five
times.
CONDITIONAL STATEMENTS
Conditionals are used to execute a certain section of code only if some specific
condition is fulfilled, and optionally execute other statements if the given
condition is false. The result of given conditional expression must be either
true or false.

Different variations of this conditional


statement are –

IF statement
IF - ELSE Statement
IF - ELSEIF - ELSE Statement
SWITCH CASE Statement
SYNTAX FOR CONDITIONAL STATEMENTS
1 IF Statement

Sample Code

// Output : Inside if statement


SYNTAX FOR CONDITIONAL STATEMENTS
2 IF - Else Statement

Sample Code

// Output : b is bigger
SYNTAX FOR CONDITIONAL STATEMENTS
3 IF - Else if - Else Statement Sample Code

// Output : Positive
SYNTAX FOR CONDITIONAL STATEMENTS
4 Switch Case Sample Code

// Output : Choice is A
DIFFERENCE BETWEEN SWITCH AND IF ELSE IF
FIND THE OUTPUT ?
OUTPUT
Inside if 15

EXPLANATION

The condition of ( x <= 15 ) is satisfied so the ‘else if’ condition is


not being checked
FIND THE ANSWER?
For what values of the x, the 'if-statement' block will not
be executed ?
OUTPUT
0

EXPLANATION

( 1 - 0 ) == ( 1 + 0 )
FIND THE OUTPUT?
OUTPUT

EXPLANATION
Switch Case without break
FIND THE OUTPUT?

OPTIONS
OUTPUT
B option

EXPLANATION
++ a * 5 is equivalent to (a + 1)*5
FIND THE OUTPUT?
OUTPUT

EXPLANATION
x XOR y = 0 ( false ) ,when ( x = y ) ; Therefore ! ( x ^ y ) = true
WHATSAPP GRP INSTAta

You might also like