Professional Documents
Culture Documents
ENGR112 - Lecture 5
ENGR112 - Lecture 5
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
•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
•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
•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
•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
Here we need a
conditional
statement
Conditional statement:
One-Way if Selection
n Syntax:
if (condition) /* no semicolon after () */
{
Action
}
13
Conditional statement:
One-Way if Selection
14
#include <iostream>
using namespace std;
int main()
{ int average; Back to our problem
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<<“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;
}