CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)
CS-323 Programming
Programming Fundamentals
Fundamentals
4(3-2)
4(3-2)
Lecture#
Lecture#55
Parts
Parts of
of the
the Sample
Sample Program
Program
Programming Fundamentals 2
C++
C++ escape-sequence
escape-sequence characters
characters
• \a Alarm (the terminal’s bell)
• \b Backspace
• \n Newline (carriage return and line feed)
• \t Tab
• \\ Backslash (\)
• \? Question mark
• \’ Single quotation mark
• \” Double quotation mark
Programming Fundamentals 3
Constant
Constant Variables
Variables
• A constant never changes and a variable holds values
that change.
• In C++ terminology, you can declare variables to be
constants with the const keyword.
• Throughout your program, the constants act like
variables; you can use a constant variable anywhere, but
you cannot change constant variables.
• To declare a constant, put the keyword const in front of
the variable declaration, for example:
Programming Fundamentals 4
Relational
Relational Operators
Operators
< less than
== equal to
!= not equal to
Programming Fundamentals 5
Logical
Logical Operators
Operators
• To combine the relational Expressions
AND &&
OR ||
• In C / C++
– if (a > b && c > d)
Programming Fundamentals 6
Unary
Unary Not
Not Operator
Operator (!)
(!)
• !true = false
• !false = true
Programming Fundamentals 7
Decision
Decision // Control
Control Structure
Structure
• A computer can process a program in one of the
following ways:
– Sequential execution
Programming Fundamentals 8
Decision
Decision // Control
Control Structure…
Structure…
Programming Fundamentals 9
Selection
Selection or
or Decision
Decision Structure
Structure
• if Structure
• if…else Structure
• Conditional Operator (? :)
• switch Structure
Programming Fundamentals 10
ifif Statement
Statement // Structure
Structure (One-Way
(One-Way Selection)
Selection)
Programming Fundamentals 11
ifif Statement
Statement // Structure
Structure (One-Way
(One-Way Selection)
Selection)
• if (condition)
statement1 ;
For example;
if (age1 > age2)
cout << “Student 1 is older than student 2” ;
• if (condition)
{
statement1 ;
statement2 ;
:
}
Programming Fundamentals 12
ifif –– else
else
if (condition)
{
statement ;
-
-
}
else
{
statement ;
-
-
}
Programming Fundamentals 13
Nested
Nested ifif
Programming Fundamentals 14
Conditional
Conditional Operator
Operator (?:)
(?:)
• Certain if. . .else statements can be written in a more
concise way by using C++’s conditional operator.
– The conditional operator, written as ? :, is known as ternary
operator, which means that it takes three arguments.
Programming Fundamentals 15
The
The Switch
Switch Structure
Structure
• Large decision tree
– Depends on the value of same variable
Programming Fundamentals 16
The
The break
break statement
statement
• The break statement, when executed in a switch structure,
provides an immediate exit from the switch structure.
• Similarly, you can use the break statement in while, for,
and do. . .while loops.
• When the break statement executes in a repetition
structure, it immediately exits from the structure.
Programming Fundamentals 17
The
The switch-break
switch-break Structure
Structure
Programming Fundamentals 18
Assignment#
Assignment# 11 (Own
(Own Handwritten
Handwritten ++ Demo)
Demo)
Programming Fundamentals 19
Assignment#
Assignment# 22 (Own
(Own Handwritten
Handwritten ++ Demo)
Demo)
1. Write a program that produces the following output :
Output: a = 0, b = 0, x = 0, y = 35
a = 9, b = 7, x = 16, y = 16
Programming Fundamentals 20
THANK
THANK YOU
YOU
Programming Fundamentals 21