1 / 18

CPCS202- LAB4

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.

aldona
Download Presentation

CPCS202- LAB4

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CPCS202- LAB4 If Statement I.Mona Alshehri

  2. 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.

  3. 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.

  4. 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 ;

  5. 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

  6. RELATIONAL OPERATORS A relational operator allows you to make comparisons in a program.

  7. 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: || , &&

  8. 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((

  9. Truth Table for logical operators: &&, ||, !

  10. 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”

  11. Program2 void main() { int no; cout <<"Enter any number : "; cin >>no; if(no%2==0) cout<<"The number is even"; }

  12. 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.

  13. 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’;

  14. Nested if statement A nested if statement is an if statement that is included within another if statement. Syntax if (expression1) { if (expression2) statement }

  15. else if statement Syntax: If(expression1) statement1else if (expression2)statement2. . .else if (expressionN)statementNelselast statementnext statement

  16. 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

  17. 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";

  18. 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(); }

More Related