Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

H) - Lec 7 Control Structures

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 20

Fundamentals of Computer

Programming

Jehangir Arshad Meo

COMSATS Institute of Information Technology, Sahiwal

1
Increment operator
Increment operator is represented by (++) sign.

It is used to add 1 to the value of a variable.

It can be used before and after the variable.

b = ++a; => a = a + 1; then b = a;

If a= 10 => 10+1 = 11 => b = a = 11

b = a++; => b = a; then a = a+1;

If a = 10 => b = 10 => a + 1 => a = 11

2
Decrement operator
Increment operator is represented by (- -) sign.

It is used to subtract 1 from the value of a variable.

It can also be used before and after the variable.

b = -- a; => a = a - 1; then b = a;

If a= 10 => 10 - 1 = 9 => b = a = 9

b = a --; => b = a; then a = a - 1;

If a = 10 => b = 10 => a - 1 => a = 9

3
Type Casting
Conversion of a value from one type to the other is called
type casting.

There are two types of casting:


1. Implicit type casting: Arithmetic operation can only be
performed if both operators are of same data type. If they
are not of same type then we should convert a lower order
data type to higher order data type.
char => Int => long => flot =>double
2. Explicit type casting: In this type of conversion cast operator
is used to convert one numeric data type to the other.

4
Keywords:
Already known values for compiler are called
keywords.
Cannot be used as identifiers or variable names
C ++ Ke yw o rd s

Keywords common to the


C and C++ programming
languages
auto break case char const
continue default do double else
enum extern float for goto
if int long register return
short signed sizeof static struct
switch typedef union unsigned void
volatile while
C++ only keywords
asm bool catch class const_cast
delete dynamic_cast explicit false friend
inline mutable namespace new operator
private protected public reinterpret_cast
static_cast template this throw true
try typeid typename using virtual
wchar_t
5
Indenting:
Indenting makes programs easier to read
 C ignores whitespace characters
 tabs

 spaces

Example:
#include <conio.h>
#include <iostream.h>
main()
{
int a,b; // variable declaration as Integer data type
float c,d; // variable declaration as Integer float type

a = 15; //assigning value to variable (a)


b = a / 2; //defining the value of (b)

cout<<“value of b is =“<<b<<endl;

c = 15.3;
d = c / 3;
cout<<“value of d is =“<<d<<endl;
}
output in c:
 For output we use printf:
cout<<“control string” << List of arguments;
It consist of two parts:
1- Control string:
It consists of text and the escape sequences, It is written in
double quotes.
a) - Text: specifies the message that is to be printed along
with the values.
b) - Escape sequence: It is used to control printing on the
monitor.
2- List of arguments:
It consists of the list of variables , constants or arithmetic
expressions, separated by commas, whose values are to be printed.
Example (cout)
Example 1:

cout<<“I am Comsian”;

Example 2:
For a= 10 and b= 20:
cout<<“ A =“<<“B =” <<a<<b;
Program:
Write a program to input the marks obtained by a student in three subjects. Calculate the total marks and there average and print the
results on the screen?

# include <iostream.h>
#include <math.h>
voide main ()
{

float total, s1, s2, s3, avg;

cout<<“Enter the marks of first subject 1: ”;


cin>>s1;

cout<<“Enter the marks of first subject 1: ”;


cin>>s2;

cout<<“Enter the marks of first subject 1: ”;


cin>>s3;

total = s1+s2+s3;
avg = total / 3;

cout<<“the total marks of the student are= ”<<total;


cout<<“the average marks of the student are=<<avg;
}
Practice Program:
Program to input radius of the sphere. Compute its volume(v= 4/3*pi(R*R*R)) and
surface area( surface area = 4* pi* R*R) where pi is 3.14.

#include <iostream.h>
int main ()
{
float R, V, area;

cout<<“enter the value of radius R of sphere? “;


cin>>R;

area = 4.0 * 3.14 * R *R ;


V = 4/3* 3.14 * R * R *R;

cout<<“ the volume of the sphere is = %f \n ”<<V;


cout<<“ the area of the sphere is = %f \n ”, <<area;

}
Getch() and getche()Function:
 Stands for Get Character.
 Are used to get single character from keyboard during the program
execution.
 As the character is pressed from key board:
1- The entered character is not displayed on the screen.
2- By pressing enter control transfer to the next statement.
 This function is basically used to pause the execution of the program.
 It is defined in (conio.h) header file.
 Written as: variable = getch(); or variable = getche();
 Variable is optional.
 Both are same just difference is:
If we use getch() character will not displayed on screen and execution
pause until user press enter.

If we use getche() character will be displayed on screen and execution


pause until user press enter
Control Structures

12
Control Structures
Transfer of control

Next statement executed that is not next one in sequence

There are three control structures to transfer control.

A- Sequence structure
 Programs executed sequentially by default
B- Selection structures
 if, if/else, switch
C- Repetition structures
 while, do/while, for

13
if Selection Structure
Selection structure
Choose among alternative courses of action
Pseudo code example:
If student’s grade is greater than or equal to 60
Print “Passed”
If the condition is true
 Print statement executed, program continues to next

statement
If the condition is false
 Print statement ignored, program continues

14
if Selection Structure
Translation into C
If student’s grade is greater than or equal to 60
Print “Passed”

if ( grade >= 60 )
 
cout<<"Passed“;
Diamond symbol (decision symbol)
Indicates decision is to be made
Contains an expression that can be true or false
 Test condition, follow path
if structure
Single-entry/single-exit

15
if Selection Structure
Flowchart of pseudo code statement

A decision can be made on


any expression.
zero - false
true nonzero - true
grade >= 60 print “Passed”
Example:
3 - 4 is true

false

16
if/else Selection Structure
if
Performs action if condition true
if/else
Different actions if conditions true or false
Pseudo code
if student’s grade is greater than or equal to 60
cout<<“Passed”
else
cout<<“Failed”
C code
if ( grade >= 60 )
cout<<"Passed“;
else
cout<<"Failed“;
17
if/else Selection Structure
Ternary conditional operator (?:)
Three arguments (condition, value if true, value if
false)
Code could be written:
cout<<“ grade >= 60 ? “Passed” : “Failed”;
Condition Value if true Value if false

false true
grade >= 60

print “Failed” print “Passed”

18
if/else Selection Structure
Nested if/else structures
One inside another, test for multiple cases
Once condition met, other statements skipped
if student’s grade is greater than or equal to 90
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”

19
if/else Selection Structure
Example
if ( grade >= 90 ) // 90 and above
cout<<"A“;
else if ( grade >= 80 ) // 80-89
cout<<“B“;
else if ( grade >= 70 ) // 70-79
cout<<“C“;
else if ( grade >= 60 ) // 60-69
cout<<“D“;
else // less than 60
cot<<“E“;

20

You might also like