180 likes | 332 Views
CPCS202- LAB4. If Statement I.Mona Alshehri. Statement 1. Statement 2. if. TRUE (non zero). Test expression. FALSE (zero). if statements. Statement 3. If Statement:. The if operation can be stated in words like this: If test is TRUE , execute the if statements.
E N D
CPCS202- LAB4 If Statement I.Mona Alshehri
Statement 1 Statement 2 if TRUE (non zero) Test expression FALSE (zero) if statements Statement 3 If Statement: The if operation can be stated in words like this: If test is TRUE, execute the ifstatements. Otherwise, (test is FALSE)bypassed to the next statement.
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; } Note: when you have more than one if statement, you must use curly braces {}. Test expression must be enclosed within parentheses Each if statement must terminated by a semicolon ;
if statement Examples Determine the output for each of the following program segments? Assume that x=2 and y=3 if(x<y) { cout<<“x=“<<x<<“\n”; cout<<“y=“<<y<<“\n”; } if(x) { cout<<“The value of x is non-zero.\n”; } if((x<y) || (x-y<0)) { ++x; --y; cout<<“x=“<<x<<“\n”; cout<<“y=“<<y<<“\n”; } Output1 x=2 y=3 Output2 The value of x is non-zero. Output3 x=3 y=2
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 larger than 0 • x not equal y and a less than b Note: when you have more than one condition it’s better to use parentheses(). x == y x > 0 ((x != y)&&(a < b((
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) statement1else if (expression2)statement2. . .else if (expressionN)statementNelselast statementnext 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(); }