If Statement

Download Report

Transcript If Statement

If Statement
I.Mona Alshehri
If Statement:
Statement 1
The if operation can be
stated in words like this:
Statement 2
If test is TRUE, execute
the if statements.
Otherwise, (test is
FALSE) bypassed to the
next statement.
if
TRUE
Test
expression
FALSE
(zero)
Statement 3
(non zero)
if
statements
If Statement
Syntax:
if (expression)
statement
An expression is any combination of variables, constants,
or function calls that evaluate to a value.
If expression is true, statement is executed; otherwise
statement is skipped.
if statement format
if (test expression)
{
if statement 1;
if statement 2;
.
.
if statement n;
}
Test expression
must be enclosed
within parentheses
Each if statement
must terminated by
a semicolon ;
Note: when you have more than one if statement, you
must use curly braces {}.
if statement Examples
Determine the output for each of the following
program segments? Assume that x=2 and y=3
if(x<y)
{
Output1
cout<<“x=“<<x<<“\n”;
x=2
cout<<“y=“<<y<<“\n”;
}
y=3
if(x)
Output2
{
cout<<“The value of x is non-zero.\n”; The value of x is non-zero.
}
if((x<y) || (x-y<0))
{
++x;
Output3
--y;
x=3
cout<<“x=“<<x<<“\n”;
y=2
cout<<“y=“<<y<<“\n”;
}
RELATIONAL OPERATORS
A relational operator allows you to make comparisons in a
program.
Test expression
 The test expression is a conditional test with a TRUE
or FALSE result.
 It’s may contains one or more conditions.
 To test a single condition, you will use the relational
Boolean operators:
== , != , < , > , <= , >=
 To test multiple conditions, you must use logical
Boolean operators:
|| , &&
Test expression Examples
Write a test expression for the following:
 x equal y
x == y
 x larger than 0
x>0
 x not equal y and a less than b
((x != y) && (a < b((
Note: when you have more than one condition it’s
better to use parentheses().
Truth Table for logical operators:
&&, ||, !
a
0
0
1
1
(F)
(F)
(T)
(T)
0
1
0
1
b
a || b
(F)
(T)
(F)
(T)
0
1
1
1
(F)
(T)
(T)
(T)
a && b
0
0
0
1
(F)
(F)
(F)
(T)
!a
1
1
0
0
(T)
(T)
(F)
(F)
Program1
Write a program that will read a number from user and
tests if it’s modules 2 is equal zero, writes a message ?
Output Screen:
Enter any number:
The number is even
Inputs: Number
Outputs: Message
Processing:
If number % 2 == zero, write “The number is even”
Program2
void main()
{
int no;
cout <<"Enter any number : ";
cin >>no;
if(no%2==0)
cout<<"The number is even";
}
if else statement
 Causes one of two alternative statements to execute
depending on the condition is true.
Its syntax is:
if (condition)
statement1;
else
statement2;
The statement1 will be executed if the expression is non
zero
Otherwise, statement2 will be executed.
Program2
Write a program that will read 2 numbers from user and
Finding the minimum values?
int x, y, min;
cout << “Input three integers: “;
cin >> x >> y;
if (x < y)
min = x;
else
min = y;
cout << “The minimum value is “ << min << ‘\n’;
Nested if statement
A nested if statement is an if statement that is included
within another if statement.
Syntax
if (expression1)
{
if (expression2)
statement
}
else if statement
Syntax:
If(expression1)
statement1
else if (expression2)
statement2
...
else if (expressionN)
statementN
else
last statement
next statement
else if statement Example
if (total >=90)
grade = ‘A’;
else if (total >= 80)
grade = ‘B’;
else if (total >= 70)
grade = ‘C’;
else if (total >= 60)
grade = ‘D’;
else
grade = ‘E’;
next statement
Program 3:
Write a program that request the user’s language and then print a
greeting in that language.
Output:
Engl. , Fren. , Ger. , Ital. , or Rus. ? (e,f,g,i,r):e
Welcome
Where the greeting will be:
English="Welcome"
France="Bon jour"
Germany=“Guten"
Italian=“Bon giorno"
Russian="Dobre utre";
Other language=“Sorry, we don't speak your language";
Program 3 code:
{
char language;
cout<< "Engl. , Fren. , Ger. , Ital. , or Rus. ? (e,f,g,i,r): ";
cin>>language;
if(language=='e')
cout<<"Welcome";
else if(language=='f')
cout<<"Bon jour";
else if(language=='g')
cout<<"Guten";
else if(language=='i')
cout<<"Bon giorno";
else if(language=='r')
cout<<"Dobre utre";
else
cout<<"Sorry, we don't speak your language";
getch();
}