Programming C++
م.د .حسين أحمد علي الويس
ﺟﺎمﻌﺔ كركوك
كليﺔ علوم الحﺎسبﺎت وتكنلوﺟيﺎ المﻌلومﺎت
قسم تكنلوﺟيﺎ المﻌلومﺎت
46
Control Statements
• Normally, statements in a program are executed one after the other in the order
in which they’re written.
• This is called sequential execution.
• There are control statements enable you to specify that the next statement to
be executed may be other than the next one in sequence.
• This is called transfer of control.
• The control statements are categorized in almost two groups:
Selection control statements
Repetition control statements
SequentialofExecution
Transfer Control
48
Selection Statements : If Statement
• Selection statements are used to choose among alternative courses of action.
• For example, suppose the passing mark on an exam is 60. The pseudocode
statement
– If student’s marks is greater than or equal to 60 Then
Print “Passed”
In C++ , The syntax for the If statement
if ( Expression) •The Expression can be any valid
action statement ; expression including a relational expression
and even arithmetic expression
if ( Expression)
{ •In case of using arithmetic expressions , a
action statement 1 ;
non-zero value is considered to be true,
action statement 2 ;
. whereas a 0 is considered to be false
.
action statement n ;
}
if ( grade >= 60 )
cout <<"Passed\n“;
50
Relational Expression and Relational Operators
• Relational expression is an expression which compares 2 operands and returns a
TRUE or FALSE answer.
Example : a >= b , a == c , a >= 99 , ‘A’ > ‘a’
• Relational expressions are used to test the conditions in selection, and looping
statements.
Operator Means
== Equal To
!= Not Equal To
< Less Than
<= Less Than or Equal To
> Greater Than
>= Greater Than or Equal To
51
Selection Statements : If Statement
Example : write a program that accept an integer from the user and in case this
integer is even print out the following message
“This number is even “ .
52
Selection Statements : If .. Else Statement
• The IF…Else selection statement allows you to specify that there is a course of
actions are to be performed when the condition is true and another course of
actions will be executed when the condition is false.
• For example, the pseudocode statement
– If student’s mark is greater than or equal to 60
Print “Passed”
else
Print “Failed”
In C++ , The syntax for the If…Else statement
if ( Expression)
action statement ;
Else
action statement ;
if ( Expression)
{
action statements 1 ;
.
action statement n ;
}
Else
{
action statements 1 ;
.
action statement n ;
}
if ( grade >= 60 )
cout <<"Passed\n“;
Else
cout <<“Failed\n” 54
Selection Statements : If – else Statement
Example : write a program that accept an integer from the user and
print out whether it is Positive or Negative number.
55
Nested If
• Nested If : means to write an if statement within another if statement.
Example : write a program that accept an integer number from the user ,
in case the number is Positive , check and print out whether it is Even or Odd
number.
int main()
{
int number;
cout <<"Please Enter any number \n";
cin >>number;
if ( number >=0)
if (number % 2 == 0)
cout <<" This is an Even number \n";
else
cout <<"This is an Odd number \n \n";
}
56
IF – Else IF statement
• For example, write a program that ask the user to Enter 2 numbers and print out
whether they are equal or there is one which is greater than the other.
int main()
{
int num1, num2;
cout <<"Enter Number 1 , Number2 \n";
cin >>num1>>num2;
if ( num1 == num2 )
cout << "Both Are Equal \n";
else if (num1 > num2 )
cout <<"Number 1 is greater than number 2 \n";
else
cout <<"Number 2 is greater than number 1 \n";
} 57
IF – Else IF
• For example, the following code will print
A for exam grades greater than or equal to 90,
B for grades greater than or equal to 80,
C for grades greater than or equal to 70,
D for grades greater than or equal to 60, and
F for all other grades.
– if ( grade >= 90 )
cout << "A\n“ ;
else if ( grade >= 80 )
cout << "B\n”;
else if ( grade >= 70 )
cout << "C\n”;
else if ( grade >= 60 )
cout << "D\n”;
else
cout << "F\n“ ;
58
Combining more than one condition
• To combine more than one condition we use the logical operators.
Operator Means Description
&& And The Expression Value Is true If and Only IF both
Conditions are true
|| OR The Expression Value Is true If one Condition Is True
Example : check whether num1 is between 0 and 100
IF ( (num1 >= 0) && (num1 <=100) )
Cout <<“The Number Is between 1 and 100” ;
Else
Cout <<“ The Number Is Larger Than 100”;
59
Combining more than one condition
Example, print out the student grade according to the following formulas:
A for exam marks greater than or equal 90 and less than or equal 100 ,
B for exam marks greater than or equal 80 and less than 90 ,
C for exam marks than or equal to 70 and less than 80 ,
D for exam marks than or equal to 60, and less than 70 ,
F for all other marks.
– if ( marks >= 90 && marks <= 100)
cout << "A\n“ ;
else if (marks >= 80 && marks <90 )
cout << "B\n”;
else if (marks >= 70 && marks <80 )
cout << "C\n”;
else if (marks >= 60 && marks <70 )
cout << "D\n”;
else
cout << "F\n“ ;
60
Example : A company insures its Employees in the following cases:
– Employee is married.
– Employee is an Single male above 30 years of age.
– Employee is an Single female above 25 years of age.
– Conditions :
1. Marital status = ‘M’; OR
2. Marital Status =‘S’ and Sex=‘M’ and Age >30 OR
3. Marital Status =‘S’ and Sex=‘F’ and Age >25
61