CS-323 Programming Fundamentals 4 (3-2) CS-323 Programming Fundamentals 4 (3-2)

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 21

CS-323

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:

const float PI =3 .14159;


const int days_of_week = 7;

Programming Fundamentals 4
Relational
Relational Operators
Operators
< less than

<= less than or equal to

== equal to

>= greater than or equal to

> greater than

!= 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)

– if (age > 18 || height > 5)

Programming Fundamentals 6
Unary
Unary Not
Not Operator
Operator (!)
(!)
• !true = false

• !false = true

If (! (amirAge > saleemAge))


?
if ((interMarks > 45) && (testMarks >= passMarks))
{
cout << “ Welcome to BIMS”;
}

If (!((interMarks > 45) && (testMarks >= passMarks)))


?

Programming Fundamentals 7
Decision
Decision // Control
Control Structure
Structure
• A computer can process a program in one of the
following ways:
– Sequential execution

– Selection or Decision (the program executes specific statements


depending on some condition(s)
– Loop or Repetition (the program repeats specific statements for
specified number of times based on some condition(s).

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.

• Syntax: expression1 ? expression2 : expression3


– Example: Var = Condition ? trueCase : falseCase

Programming Fundamentals 15
The
The Switch
Switch Structure
Structure
• Large decision tree
– Depends on the value of same variable

– Switch statement instead of ladder of

if... else or if... elseif

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.

• The break statement is typically used for two purposes:


 To exit early from a loop.

 To skip the remainder of the switch / loop structure.

Programming Fundamentals 17
The
The switch-break
switch-break Structure
Structure

Programming Fundamentals 18
Assignment#
Assignment# 11 (Own
(Own Handwritten
Handwritten ++ Demo)
Demo)

1. Draw a Flowchart for addition, multiplication of two


numbers a, b and display their results.

2. Draw a Flowchart for nested if structure that display


the Grade of different students as ‘A’, ‘B’, ‘C’ etc.

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

2. Write a program that compute and print the area using


formula area = radius * radius * PI
– Instruction-1: Declare a constant value for PI.

– Instruction-2: Input values via key-board for radius and area.

3. Write a program to find the largest number between two


numbers.

Programming Fundamentals 20
THANK
THANK YOU
YOU

Programming Fundamentals 21

You might also like