conditional statement

Download Conditional statement

If you can't read please download the document

Upload: maxie-santos

Post on 16-Apr-2017

8.816 views

Category:

Documents


1 download

TRANSCRIPT

Conditional Statement

Conditional StatementPresented by:Maximino E. Santos Jr.Computer Teacher

Conditional Statement

Conditional statement are statements that check an expression then may or may not execute a statement or group of statements depending on the result of the condition.

The IF statementThe general form of the if statement is:if (expression)statement;where:if is a reserve word in Turbo Cexpression is relational or boolean expression that evaluates to a TRUE (1) or FALSE (0) valuestatement may either be a single Turbo C statement or a block of Turbo C statements.

The IF statementThe general form of the if statement with block of statements is:if (expression){statement_sequence;}In an if statement, if the expression evaluates to TRUE(1), the statement or the block statements that forms the target of the if statement will be executed. Otherwise, the program will ignore the statement or the block of statements.

The IF statementNote:

Never place a semicolon after the expression in an if statement.

Example 1Write a program that will output Congratulations you PASSED! if the students grade is greater than or equal to 60.

Example 2Write a program that will ask for a price. If the price is greater than 1000, compute a 10% discount from the original price. Display the computed discount.

The IF-ELSE statementThe general form of the if-else statement is:if (expression)statement_1;elsestatement_2;

The IF-ELSE statementwhere:if is a reserve word in Turbo Cexpression is relational or boolean expression that evaluates to a TRUE (1) or FALSE (0) valuestatement_1 and statement_2 may either be a single Turbo C statement or a block of Turbo C statements.

The IF-ELSE statementThe general form of the if-else statement with block of statements is:if (expression){statement_sequence;}else{ statement_sequence;}

The IF-ELSE statementIf an if-else statement, if the expression is TRUE (1), the statement or block of statements after the if statement will be executed; otherwise, the statement or block of statements in the else statement will be executed.

The IF-ELSE statementNote:

Only the code associated with the if or the code that is associated with the else executes, never both.

Example 3Write a program that will output Congratulations you PASSED! if the students grade is greater than or equal to 60. Otherwise output, Sorry you FAILED!

Fin