if
statement, the if-else
statement, and the switch
statement.if
statements are valid(3 + 2 % 5)
evaluates to 5 and since 5 is non-zero it is considered to be true. Hence the printf('This works');
gets executed.if
upto and not including the else
is called an if block
. Similarly,the statements after the else
form the else block
.else
is written exactly below the if
. The statements in the if block
and those in the else block
have been indented to the right.if block
and only one statement in the else block
we could have dropped the pair of braces.if
statement, the default scope of else
is also the statement immediately after the else
. To override this default scope a pair of braces as shown in the above 'Multiple Statements within if' must be used.if-else
construct within either the body of the if
statement or the body of an else
statement. This is called 'nesting' of ifs
. This is demonstrated as -else-if
is the most general way of writing a multi-way decision.else
part handles the 'none of the above' or default case where none of the other conditions is satisfied.switch
statement is a multi-way decision that tests whether an expression matches one of a number of constant
integer values, and branches accordingly. The switch
statement that allows us to make a decision from the number of choices is called a switch
, or more correctlya switch-case-default
, since these three keywords go together to make up the switch
statement.switch…case
command, each case
acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one of case
sections or labels from where the execution of the program will continue. The program will continue execution until it reaches break
command.break
statements have vital rule in switch
structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end of switch
block will be executed (while most of the time we just want one case
section to be run).default
section will be executed if none of the case sections match switch comparison.switch
.Example Statement for If Statement and Switch Case in C Language |
---|
1. Find Perfect Leap Year |
2. ATM money dispatch count while currencies are 1000,500 and 100 |
3. Display day for the entered date by user |
4. Switch case (Resolve Error) |