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

Lecture 1 CSC 202

Computer programming in c notes

Uploaded by

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

Lecture 1 CSC 202

Computer programming in c notes

Uploaded by

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

Welcome to

CSC 202

Computer Programming-II
Course Introduction

The goal of this course is to equip students with the necessary skills for
programming using the ANSI (American National Standards Institute)
standards. It aims to provide a solid understanding of the C++
Programming Language and cover special topics in Advanced
Programming.
Course Outline information

Topics covered include fundamentals of algorithms,


flowcharts, problem solving, programming concepts,
classes and methods, control structures, arrays,
and
strings. Throughout the semester, problem solving skills
will be stressed and applied to solving computing
problems. Laboratory session experiments will provide
hands-on experience in topics covered in this course.
1- Couse Introduction
2- Variables / types of variables
3 Input / output streams and validation of data
4 Data types, Variables, Operators and Decision Making.
5 Control Structure
6 Arrays / multi-dimensional arrays &Loops - for /
while /
do-while
Weekly 7 Method, overloading Method, passing variables to
functions etc.
Lectures 8 Some Topic in DS such as Stack, Queue,
Midterm Examination Week
break 9 Classes and Objects, Special Topic in advanced

down programming.
10 Method: in deep look
11 Constructors/Destructors
12 Function Overloading &Overriding
13 Memory Management and OOP Concept
14 Free Lecture/Revision week
15 Final Examination week
Lecture
1
Introduction to C++
What is C++ Programming?

C++ is a powerful, general-purpose programming language that was developed as


an extension of the C programming language. It was created by Bjarne Stroustrup
at Bell Labs in the early 1980s and has since become widely used in various
application domains, including systems programming, game development,
embedded systems, and high-performance applications.
A First Simple Program

#include<iostream>  in C++
#include<stdio.h>  in C language
int main()
{
cout << "C++ is power
programming."; printf(“ C is power
Programming”); return 0;
}
• C++ provides built-in data types that correspond to
integers, characters, floating-point values, and
Boolean values.

• These are the ways that data is commonly stored and


C++ Data manipulated by a program.

Types • C++ allows you to construct more sophisticated types,


such as classes, structures, and enumerations, but
these too are ultimately composed of the built-in
types.
keyword and
identifiers in C++
language
• There are 63 keywords
currently defined for
Standard C++.
• Early versions of C++ defined
the overload keyword, but
it is obsolete.
• Keep in mind that C++ is a
case-sensitive language, and it
requires that all keywords be
in lowercase.
Variables

We might want to give a value a name so we can refer to


it later. We do this using variables.
A variable is a named location in memory.

For example, say we wanted to use the value 4 + 2 multiple times. We


might call it x and use it as follows:

x = 4 + 2;
Example

# include <iostream >


using namespace std ;

int main () {
int x;
x = 4 + 2;
cout << x /
3 << " " <<
x * 2;

return 0;
Example: User Inputting from
keyboard
# include <iostream >
using namespace std ;

int main () {
int x;
cin >> x;
cout << x / 3 << ’ ’ << x *
2<<endl;
return 0;
}
Keyword/
Reserve
words in C+
+
At the core of the C++ type
system are the seven basic data
types shown here:
Double vs Float

The main difference between `float` and `double` data types lies in their precision. `double` has double the
precision of `float` (64 bits vs. 32 bits), making it capable of representing larger and more precise floating-
point values. However, `double` consumes more memory than `float`. Use `double` when higher precision
is required, and `float` when memory efficiency is a priority.
Lecture
2
Control Structures in C++
Control structures are portions of program code
that contain statements within them and,
Control depending on the circumstances, execute these
Structur statements in a certain way. There are typically
es two kinds:
conditionals and loops.
Condition In order for a program to change its behavior

als
depending on the input, there must a way to test
that input. Conditionals allow the program to
check the values of variables and to execute (or
not execute) certain statements.

C++ has if and switch-case


conditional
structures.
Operato
rs
Conditionals use two kinds of special
operators: relational and logical.
These are used to determine whether
some condition is true or false.

The relational operators are used to


test a relation between two
expressions:
Logical Operators
The logical operators are
often used to combine
relational expressions into
more complicated Boolean
expressions:
The operators return true or
false, according to the rules of
logic
The Not !
Operator
The ! operator is a unary operator, taking only one argument and negating its value:
Examples using logical operators (assume x =
6 and y = 2):

Example !(x > 2) → false


(x > y) && (y > 0) → true
s… (x < y) && (y > 0) → false
(x < y) || (y > 0) → true
The if conditional has the form:
if(condition)
{
statement1
statement2
if, if-else …
}
and
else if if(condition)
statement
Example
s…<iostream>
#include
using namespace std;

int main(){
int number = 6;

cout << "Number is: " << number << endl;

//check to see if number is positive


if(number>0){
cout << "Number is a
positive integer";
}

return 0;
}
if(condition)
{
statementA1
statementA2

The if- }

else else
{
form statementB1
statementB2

}
Example
#include <iostream>
s…
using namespace std;

int main(){
int number = -3;

cout << "Number is: " << number << endl;


if (number > 0){
cout << "This means it is a positive integer";
}
else{
cout << "This means it is a negative integer";
}
}
If the condition is met, the block corresponding to
the if is executed. Otherwise, the block
corresponding to the else is executed. Because the
condition is either satisfied or not, one of the blocks
in an if-else must execute. If there is only one
Carefu statement for any of the blocks, the curly braces for

l… that block may be omitted:

if(condition)
statementA1
else
statement
B1
The else if is used to decide between two or more
blocks based on multiple conditions:

if(condition1)
{
statementA1
The …
statementA2

else if }
else if(condition2)
conditio {
statementB1
n statementB2

}
Example if(number > 0){
cout << "This means that it is a positive integer" << endl;
s…
#include <iostream>
}

using namespace std;


else if(number < 0){
int main(){ cout << "This means that it is a negative integer" << endl;
int number; }

cout << "Enter an integer: "; else{


cin >> number; cout << "The number you entered is 0";
}
cout << "\nYou entered: " << number <<
endl;
return 0;
}
If condition1 is met, the block corresponding to
the if is executed. If not, then only if condition2
is met is the block corresponding to the else if
executed. There may be more than one else if,
Remar each with its own condition. Once a block whose

k… condition was met is executed, any else ifs after


it are ignored. Therefore, in an if-else-if
structure, either one or
no block is executed.
Example
s…
The switch-case is another conditional.

switch- structure that may or may not execute


certain statements. However, the switch-case
case has peculiar syntax and behavior:
switch(expression)
{
case constant1:
statementA1
statementA2
break;
case constant2:
statementB1
statementB2
break;
...
default:
statementZ1
statementZ2
}
The switch evaluates expression and, if expression is equal to
constant1, then the statements beneath case constant 1: are executed
until a break is encountered. If expression is not equal to constant1,
then it is compared to constant2. If these are equal, then the
statements beneath case constant 2: are executed until a break is
encountered. If not, then the same process repeats for each of the
constants, in turn. If none of the constants match, then the
statements beneath default: are executed.
Exam
ple
Conditionals execute certain statements if
certain conditions are met; loops execute
certain statements while certain conditions
Loo are met. C++ has three kinds of loops:
ps while, do-while, and for.
The while loop has a form similar to the if
conditional:
while(condition)

while and {
statement1
do- while statement2

}
While As long as condition holds, the block of

loop statements will be repeatedly executed.


If there is only one statement, the curly
braces may be omitted. Here is an
example:
using namespace std;
int main()
{
int x = 0;
while(x <= 10)
{
cout<<x<<"\n";
Exampl x++;

e.. }
return 0;
}

What will be the output of this portion of code..?


Do-while Loop
The do-while loop is a variation that guarantees the block of statements will
be
executed at least once:

do
{
statement1
statement2

}
while(condit
ion);
Exampl
e…
using namespace std;
int main()
{
int x = 0;
do
{
x++;
cout<<x<<"\n";
return 0;
} while(x <= 10);
}
Remar
k…
The block of statements is executed and then, if the condition holds, the
program returns to the top of the block. Curly braces are always
required. Also note the semicolon after the while condition.
The For loop
The for loop works like the while loop but with some change in
syntax: for(initialization; condition; incrementation)
{

statement1
statement2

}
Remar
k…
The for loop is designed to allow a counter variable that is initialized at
the beginning of the loop and incremented (or decremented) on each
iteration of the loop. Curly braces may be omitted if there is only one
statement.
Here is an example:
Example…

using namespace std;


int main()
{
int i;

for(i=0;i<=10;i++)
cout<<i<<endl;
return 0;
}
Nested Control Structures

It is possible to place ifs inside of ifs and loops inside of loops by simply
placing these structures inside the statement blocks. This allows for
more complicated program behavior. Here is an example using nesting if
conditionals
Exampl
e…
Question
s?

You might also like