H) - Lec 7 Control Structures
H) - Lec 7 Control Structures
H) - Lec 7 Control Structures
Programming
1
Increment operator
Increment operator is represented by (++) sign.
2
Decrement operator
Increment operator is represented by (- -) sign.
b = -- a; => a = a - 1; then b = a;
If a= 10 => 10 - 1 = 9 => b = a = 9
3
Type Casting
Conversion of a value from one type to the other is called
type casting.
4
Keywords:
Already known values for compiler are called
keywords.
Cannot be used as identifiers or variable names
C ++ Ke yw o rd s
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
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 ()
{
total = s1+s2+s3;
avg = total / 3;
#include <iostream.h>
int main ()
{
float R, V, 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.
12
Control Structures
Transfer of 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
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
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