Practical Programming -L09
Practical Programming -L09
In this syntax, we notice the use of curly braces { } to indicate the beginning
and end of the block of statements that belong to the If statement. If the
condition is true, the statements inside the block are executed. If the condition
is false, the program skips the block and continues.
Example
In this program, the user is prompted to enter their age. If the age
entered is less than 60, the program prints "You are pretty young!!".
If the age is 60 or older, nothing is printed.
If/Else Statement
• This form of the If statement is slightly different from the
simple If statement.
• If the condition is not met, an alternative action is taken. In
other words, if the condition is false, the program executes the
statements in the Else block.
If/Else Statement
The general syntax is as follows:
Example
In this example, if the age entered is less than 60, the program prints "You are
pretty young!". If the age is 60 or older, the program prints "You are old".
If/Else If Statement
• This form allows for multiple conditions to be checked sequentially.
• If the first condition is not met, the program checks the next condition,
and so on.
• If none of the conditions are met, the Else block is executed.
If/Else If Statement
The general syntax is as follows:
Example
In this program, the user is prompted to enter a mark. Based on the mark entered, the program
prints the corresponding grade. If the mark is greater than 90, it prints "EXCELLENT". If the
mark is between 81 and 90, it prints "VERY GOOD", and so on. If the mark is less than or
equal to 60, it prints "FAIL" and "TRY ANOTHER TIME".
2. The Switch Statement
The Switch statement is another conditional statement
similar to the If statement, but it differs in that it tests the
value of a single variable rather than a condition. In other
words, the Switch statement executes a specific block of
code based on the value of a variable.
2. The Switch Statement
The general syntax is as follows:
Example
Example
In this program, the user is prompted to enter a number
representing a day of the week. The Switch statement checks the
value of the variable day and prints the corresponding day. If the
number entered is not between 1 and 7, the program prints "Wrong
day number".