Conditional Statements
Conditional Statements checks the condition or compares two variables using comparison operators and performs action.
We use if, if-else, else-if as conditional statements.
If statement
If statement checks the condition and executes the specific code.
If-else
If-else is used as conditional statement. In if else statements, if one condition is true, it will be executed and all others will be ignored.
- If statement requires condition.
- Else statement does not require condition.
Else if
if you want to check conditions more than one time then use else if.
If else with logical operators
If you want to check two conditions in single if statement, you will use logical operators.
Nested if else
In nested if else, we use if statement within if statement.
Dangling else Grammar
If you are using multiple if statements and single else statement, then there is dangling else problem.
Compiler does not know that with which if statement, else will go.
Ternary Operator
It is also used to check condition like if else. It is a single line statement.
Note: You can use ternary operator when logic is simple. It makes your code more compact and easier to use.
Goto Statement
As name indicates goto statement is used to go to a particular line of code. We labelled a line of code and at some point, we write "goto label" and our control jumps to that label.
Practice Exercises
Complete these exercises to reinforce your learning and earn XP