80 likes | 235 Views
The if Statement. The basic if statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true. This is illustrated in the Figure 1. no. if (condition) statement; next statement; or if (condition) { statement;
E N D
The if Statement • The basic if statement allows your program to execute a single statement, or a block of statements enclosed between braces, if a given condition is true. • This is illustrated in the Figure 1. tMyn
no if (condition) statement; next statement; or if (condition) { statement; statement; … } next statement; condition is true? yes Statement or Block of Statements Next Statement Figure 1. The statement or block of statements that follows the if is only executed If condition is true. tMyn
A simple example of an if statement to test the value of a variable called letter: if($letter==‘A’) echo “The first capital! <br>"; echo “This is not part of if structure!! <br>"; The condition to be tested appears ALWAYS in parentheses!! tMyn
The statement that is to be executed when the condition in an if statement is true can itself be an if statement. • This arrangement is called a nested if. • The condition of the inner if is only tested if the condition for the outer if is true. • An if that is nested inside another can also contain a nested if. tMyn
If this is NOT true, we will never visit here!! if($letter>=‘A’) { if($letter<=‘Z’) { echo “You entered an upper case letter. <br>"; … } } tMyn