0% found this document useful (0 votes)
28 views48 pages

L01 C++Preliminaries Fall2023

This program uses a do-while loop to repeatedly prompt the user to enter a number until they enter 0. It will execute the code block inside the do-while at least once before checking the condition.

Uploaded by

yahiag73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views48 pages

L01 C++Preliminaries Fall2023

This program uses a do-while loop to repeatedly prompt the user to enter a number until they enter 0. It will execute the code block inside the do-while at least once before checking the condition.

Uploaded by

yahiag73
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 48

C++ Preliminaries

CUFE
Dr. Lydia Wahid
Acknowledgment
 These slides have been created by
Prof. Magda Fayek
Agenda
 Basic Parts of a Program (in C++)
  Program Life cycle and IDE
 C++ Basics
 Basic Parts of Memory dedicated to a
program
Example 1: Program Hello World
# include <iostream>
using namespace std;
int main( )
{
……//Code statements
return 0; // Indicates normal termination.
}
Program life cycle
Text Editor Program Source file
(High Level Language)
e.g. C++

Compiler
(Translating Program)
Linker

Debugger Executable File


(Machine Language)

Compiler
Integrated+ Development
Editor + Debugger
Environment
+ Linker
IDE
IDE includes:
 Text Editor to write your code
 Compiler to produce object code
 Linker to link your object code files and
produce the final .exe file.
 Debugger (optional) to find logical errors in
your code.
 Additions: Help, Library
 Example IDEs: Visual Studio, Eclipse, Net
Beans, … etc.
Steps to Produce .exe

Source compiler
Object
File File

Linker

Executable

Source compiler Object


File File Libraries
Agenda
 Basic Parts of a Program (in C++)
 Program
Life cycle and IDE
  C++ Basics
 Basic Parts of Memory dedicated to a
program
Basic Parts of a Program
 Declarations and Definitions:
 Data (Variables)
 Functions
 Statements (Instructions )
1. Input / Output
2. IF statement
3. Switch Case
4. Loops:
 Conditional: while, do { …. } while
 Non-Conditional (iteration): for ( … ; … ; …. )
Variables
 Syntax
 type variableName ;
 type variableName = value;
 type variableName = Expression;
 Examples:
 int x;
 int x=4;
 int x=3;
int y=x+4;
Prepared by Dr. Lydia Wahid
Variables Names Rules
 Only underscore, letters and digits are allowed in a
variable name
 int x1; //An integer variable named “x1”
 int xyz1_w; //An integer variable named “xyz1_w”
 Variable names must start with underscore or letter:
 int 1x; //error
 No spaces or special characters are allowed.
 You cannot use a C++ keyword (a reserved word) as a
variable name.

Prepared by Dr. Lydia Wahid


Variables Names Rules
 Variable names must be unique i.e. no two
variables can have the same name
 C++ is case-sensitive
 int x; //x is written as lowercase
int X; //X is written as uppercase
No error happens here since the above are
considered two different names since one is
written in lowercase and the other is written in
uppercase
Prepared by Dr. Lydia Wahid
Declarations in c++

 Declaration then initialization


int a=3, b=4;
…………
int c; // value of c is undetermined
c = a+b;

 Declaration and initialization


int a=3, b=4;
…………
int c = a+b;
Variables: Local scope
• A name declared within a block is accessible only
within that block and blocks enclosed by it, and
only after the point of declaration.

• Blocks are defined by a pair of braces


Variables: Local scope
Example :
int main ()
{
// here we don’t know x
{
int x; // Local variable
// here we know x
} // life time of x is ended
// here we don’t know x
}
Variables: Local scope
Exercise:

int main ()
{ int y=1;
{
int x=3;
y=x+4;
}
int x=2; ?Does this line cause an error

int z=x+y; ?What is the value stored in variable z

}
Prepared by Dr. Lydia Wahid
Variables: Global Scope
• Global variables are defined outside of all the functions,
usually on top of the program. The global variables will
hold their value throughout the lifetime of your program.
Example:
#include <iostream>
using namespace std; // allows using names cin, cout
int g; // global variable
// here we know g
int main()
{
…. // here we know g
}
Data Types in C++
Data Type
char
short
int
float
double
bool
Operators in C++
 Mathematical
 Postfix and Prefix
 Logical
 Comparison
 Input/Output
Mathematical Operations in C++
 a = a + b; a += b;

 a = a - b; a -= b;

 a = a * b; a *= b;

 a = a / b; a /= b;
Postfix and Prefix Operations
• int n=0, m=0;
• n = ++m;
• cout << n << m;

 Output : 1 1

• int n=0, m=0;


• n = m++;
• cout << n << m;

 Output : 0 1
Logical Operations in C++
 a = a | b; a |= b;

 a = a & b; a &= b;

 a = a ^ b; a ^= b;

 a = a << b; a <<= b;

 a = a >> b; a >>= b;
Comparison Operations in C++
 a < b; a <= b;

 a > b; a >= b;

 a == b; a != b;
Input and Output Operators
#include <iostream>
using namespace std; // allows using names cin, cout
int main()
{
int a;
cout << “Enter a value for a: “ << endl;
cin >>a;
cout << “You Entered: “ << a << endl;
}

Describe this program


STATEMENTS in C++

 If statement
 Switch Case
 Conditional Loops
 Iterative Loops
IF Statements

 If statements True False


if (condition) condition
{
S1;
} S1
S2;

S2

Prepared by Dr. Lydia Wahid


IF Statements
 If statements with else
if (condition)
True False
{ condition
S1;
}
else S1 S2
{
S2;
}
S3;
S3
IF Statement Example
#include <iostream>
using namespace std; // allows using cin, cout
int main()
{
int a;
cin >>a;
if (a>=0)
cout << “Number is positive or zero”;
else
cout << “Number is negative”;
}

Describe this program


IF Statements
 If statements with else if
if (condition1)
{
:Notes
S1; -Any number of else if can be used
} -Only one else statement can be used
else if (condition2) at the end
{ -If statement doesn’t necessarily end
with an else
S2;
}
else
{
S3;
}
S4;
Prepared by Dr. Lydia Wahid
IF Statement Example
#include <iostream>
using namespace std; // allows using cin, cout
int main()
{
int a;
cin >>a;
if (a>0)
cout << “Number is positive”;
else if (a<0)
cout << “Number is negative”;
else
cout << “Number is zero”;
}

Prepared by Dr. Lydia Wahid


Common errors (If statements)
 if (condition) ;
x=y + z ;

 if (condition) if (condition)
x=y + z;
y++; { x=y + z;
else y+
x=y - z; +; }
y--; else

{ x=y - z;
Note
 A condition can be ANDed or ORed with
other condition(s). Examples:
 C1 && C2
 C1 || C2
 C1 && (C2 || C3)
 Note: && have higher precedence than || if
no parentheses are used

Prepared by Dr. Lydia Wahid


Switch Case
 switch(expression) {
case x:
// code block
break;
case y:
// code block
break;
default:
// code block
}
Prepared by Dr. Lydia Wahid
Switch Case Example
 int day; cin>>day;
switch (day) {
case 1:
cout << "Today is Saturday";
break;
case 2:
cout << "Today is Sunday";
break;
default:
cout << "Not indicated";
}
Prepared by Dr. Lydia Wahid
Switch Case Example
 What happens if break statements are
removed?
 What happens if default statement is
removed?
Conditional Loops

While statement
True False
while (condition) condition

{
S1; S1
S2

}
S2;
While Example
int i=1;
while (i < 10)
{
i ++;
}
What is the value of variable i after finishing the while loop?

Prepared by Dr. Lydia Wahid


Conditional Loops
S1
 Do While statement

False
do True
condition

{
S1; S2

} while (condition) ;
S2;
Do While Example
#include <iostream>
using namespace std;
int main()
{
int n;
do {
cout << "Enter Any number (0 to end): ";
cin >> n;
cout << "You entered: " << n << ‘\n’;
} while (n != 0);
}

Describe this program


Iterative loops
Initialization
for (initialization; condition ; step)
{ STEP
S1;
} S1
True
condition

S2;
False

S2
For Loop Example
int n = 10;
int x=0;
for (int i = 0; i < n; i++)
{
x=x+1;
}

Describe this program

Prepared by Dr. Lydia Wahid


Agenda
 Basic Parts of a Program (in C++)
 Program Life cycle and IDE
 C++ Preliminaries

  Basic Parts of Memory dedicated to a


program
Basic Parts of Memory dedicated to
a Program
 Code Segment: for instructions part
 Data Segment: for global and static variables
 Stack Segment:
 Return addresses
 Local data
 Heap: Additional Memory  for dynamic
memory allocation of pointers
Thank you
Exercises
1. If a1 is false, a2 is true and a3 is false,
then which of the following statements is
true?
A) The expression (a1 || a2 && ! a3) is false
B) The expression (! a1 && a2 || a3) is false
C) The expression (a1 && ! a2 || a3) is false
D) The expression (a1 || ! (a2 && a3)) is false
E) None of the above is true
Exercises
2. What is the output of the following?
A. if ( false )
if ( false ) {
printf("\"Have a great semester!\"\n") ;
} else
printf("\"Have a great semester!\"\n") ;
Exercises
2. What is the output of the following?
B. if ( false );
if ( false );
printf("\"Have a great semester!\"\n") ;
// else
printf("\"Have a great semester!\"\n") ;
Exercises
2. What is the output of the following?
C. if ( false ) {
if ( false )
printf("\"Have a great semester!\"\n") ;
} else
printf("\"Have a great semester!\"\n") ;

You might also like