You are on page 1of 30

Control Structures

In this lecture we will see


n Conditional statements
n Statements that are executed if a condition
is satisfied (true)
n if
n if-else
Example
n Write a C++ program that performs the following:
n Ask the student to enter his/her average
n Read student’s average
n Display the average back to student
n Print “Congratulations” message to the student if
student’s average is higher than 95%

Notice the condition

3
Conditional Statements
(Selection Statements)
n A conditional statement allows us to control
whether a program segment is executed or not.

n Conditional Statements
n if statement
n if

n if-else

n if-else-if

n switch statement
#include <iostream>
using namespace std;
int main()
{ Back to our problem

•Ask the student to enter his/


her average

•Read student’s average


return 0;
} •Display the average back to
student

•Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

•Ask the student to enter his/


her average

•Read student’s average


return 0;
} •Display the average back to
student

•Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


•Ask the student to enter his/
her average

•Read student’s average


return 0;
} •Display the average back to
student

•Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


cin >> average; •Ask the student to enter his/
her average

•Read student’s average


return 0;
} •Display the average back to
student

•Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


cin >> average; •Ask the student to enter his/
cout<<“ your average is: ”<<average <<endl; her average
•Read student’s average

return 0; •Display the average back to


student
}
•Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


cin >> average; •Ask the student to enter his/
cout<<“ your average is: ”<<average <<endl; her average
•Read student’s average

•Display the average back to


student
cout<<“\n Congratulations! That’s a high score! \n”;
•Print “Congratulations”
return 0; message to the student if
student’s average is higher
}
than 95%

Here we need a
conditional
statement
Conditional statement:
One-Way if Selection
n Syntax:
if (condition) /* no semicolon after () */
{
Action
}

n Action is one statement (braces are optional) or


group of statements (braces are must);
n if the condition is true, then execute action(s)
n if the condition is false, then skip action(s)
11
Conditional statement:
One-Way if Selection (cont)
n Syntax:
if (condition) /* no semicolon after () */
{
Action
}
n Condition is a statement which is either true of false
n Examples:
n Temperature > 40 // Temperature is a variable
n Age == 18 // Age is a variable
n Password != SecretCode // Password and SecretCode
are two variables
n 2x+1 > 0 // x is a variable
12
One-Way if Selection

13
Conditional statement:
One-Way if Selection

14
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


cin >> average; •Ask the student to enter his/
cout<<“ your average is”<<average; her average
if (average> 95) •Read student’s average
cout<<“\n Congratulations! That’s a high score! \n ”;
•Display the average back to
student
return 0;
} •Print “Congratulations”
message to the student if
student’s average is higher
than 95%
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem

cout << “Please enter your average\n” ;


cin >> average; •Ask the student to enter his/
cout<<“ your average is”<<average; her average
if (average> 95) •Read student’s average

{ •Display the average back to


student\n ”;
cout<<“\n Congratulations! That’s a high score!
cout << “Thank you \n” ;
•Print “Congratulations”
message to the student if
} student’s average is higher
return 0; than 95%
}
Conditional statement:
One-Way if Selection: Exercise
n Write a program that asks the user to enter his
age, then prints “Eligible for driving” if his age
is greater than 18.

17
#include <iostream>
using namespace std;
int main()
{ int Age;
cout << “Please enter your Age\n” ;
cin >> Age;
if (Age > 18)
cout<<“\n Eligible for driving \n ”;

return 0;
}
Conditional statement:
One-Way if Selection: Another example
n Write a C++ program that performs the following:
n Ask the user to enter his Age
n Read his age
n Print “Infant ” message if his age is less than 2
n Print “Adult” if his age is above 18

19
#include <iostream>
using namespace std;
int main()
{
int Age;

return 0;
}
#include <iostream>
using namespace std;
int main()
{
int Age;
cout << “Please enter your Age\n” ;
cin >> Age;

return 0;
}
#include <iostream>
using namespace std;
int main()
{
int Age;
cout << “Please enter your Age\n” ;
cin >> Age;
if (Age < 2)
cout<<“ Enfant\n”;

return 0;
}
#include <iostream>
using namespace std;
int main()
{
int Age;
cout << “Please enter your Age\n” ;
cin >> Age;
if (Age < 2)
cout<<“ Enfant\n”;
if (Age > 18)
cout<<“ Adult\n”;

return 0;
}
Conditional statement:
Two-Way (if … else) Selection

Problem:
Write a program that asks the user to enter his grade,
then prints “Pass” if his grade is greater or equal
than 60 , “Fail” Otherwise

24
Conditional statement:
Two-Way (if … else) Selection
n Syntax:
if (condition)
{
Action A;
}
else
{
Action B;
}
n if the condition is true, then Action A will be executed
and Action B will be skipped.
n if the condition is false, then Action A will be skipped and
25
Action B will be executed.
Two-Way (if…else) Selection
#include<iostream>
using namespace std;
int main()
{ int grade;
cout << “Please enter your grade\n” ;
cin >> grade;
if (grade>= 60)
cout<<“ Passed\n”;
else
cout<<“Failed\n”;
return 0;
}
Conditional statement:
Two-Way (if … else) Selection
Problem:
Write a program that asks the user to enter his grade,
then prints
“Pass”
“congratulations!”
if his grade is greater or equal than 60 , “Fail”
“Good luck Next time!”
Otherwise
/* what is wrong with the following code? */
#include<iostream>
using namespace std;
int main()
{ int grade; what is
cout << “Please enter your grade\n” ;
cin >> grade;
wrong
if (grade>= 60)

cout<<“ Passed\n ”; with the


cout<<“ Congratulations\n ”;
Following
else

cout<<“Failed\n”;
Code?
cout<<“ Good luck Next time!\n”;

return 0;
}
/* what is wrong with the following code? */
#include<iostream>
using namespace std;
int main()
{ int grade;
cout << “Please enter your grade\n” ;
cin >> grade;
if (grade>= 60)
{
cout<<“ Passed\n ”;
cout<<“ Congratulations\n ”;
}
else
{
cout<<“Failed\n”;
cout<<“ Good luck Next time!\n”;
}
return 0;
}

You might also like