C has three major decision making instructions—the if
statement, the if-else
statement, and the switch
statement.
- Dynamic Programming Ppt
- Control Structures In C Programming Ppt Presentations
- Introduction To Computer Programming Ppt
- Introduction To C Programming Ppt
- Introduction To Programming Ppt
Book title: C Programming Book author: Nuha Ali Book subject: C Programming Tutorial Book bookid: 6495407 Book title: Telecom Billing Book author: Zara Ali Book subject: Telecom Billing Tutorial Book bookid: 6495700 Pointers to Structures. You can define pointers to structures in the same way as you define pointer to any other. PowerPoint Slides for Starting Out with C from Control Structures to Objects, 9th Edition Download Accessible Powerpoints Presentations (application/zip) (17.986 MB) Previous editions.
1. The if Statement
C uses the keyword if to implement the decision control instruction. The general form of if statement looks like this:
The more general form is as follows:
Here the expression can be any valid expression including a relational expression. We can even use arithmetic expressions in the ifstatement. For example all the following if
statements are valid
The expression (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.
2. The if-else Statement
The if statement by itself will execute a single statement, or a group of statements, when the expression following if evaluates to true. It does nothing when the expression evaluates to false. Can we execute one group of statements if the expression evaluates to true and another group of statements ifthe expression evaluates to false? Of course! This is what is the purpose of the else statement that is demonstrated as
Note
- The group of statements after the
if
upto and not including theelse
is called anif block
. Similarly,the statements after theelse
form theelse block
. - Notice that the
else
is written exactly below theif
. The statements in theif block
and those in theelse block
have been indented to the right. - Had there been only one statement to be executed in the
if block
and only one statement in theelse block
we could have dropped the pair of braces. - As with the
if
statement, the default scope ofelse
is also the statement immediately after theelse
. To override this default scope a pair of braces as shown in the above 'Multiple Statements within if' must be used.
3. Nested if-elses
Dynamic Programming Ppt
If we write an entire 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 -
4. The if-else Ladder/else-if Clause
The else-if
is the most general way of writing a multi-way decision.
The expressions are evaluated in order; if an expressionis true, the 'statement' or 'block of statement' associated with it is executed, and this terminates the whole chain. As always, the code for each statement is either a single statement, or a group of them in braces. The last else
part handles the 'none of the above' or default case where none of the other conditions is satisfied.
5. Switch Statements or Control Statements
Control Structures In C Programming Ppt Presentations
The 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.
- In
switch…case
command, eachcase
acts like a simple label. A label determines a point in program which execution must continue from there. Switch statement will choose one ofcase
sections or labels from where the execution of the program will continue. The program will continue execution until it reachesbreak
command. break
statements have vital rule inswitch
structure. If you remove these statements, program execution will continue to next case sections and all remaining case sections until the end ofswitch
block will be executed (while most of the time we just want onecase
section to be run).default
section will be executed if none of the case sections match switch comparison.
Introduction To Computer Programming Ppt
6. switch Versus if-else Ladder
There are some things that you simply cannot do with a switch
.
These are:
- A float expression cannot be tested using a switch
- Cases can never have variable expressions (for example it is
wrong to say case a +3 :) - Multiple cases cannot use same expressions.
Introduction To C Programming Ppt
7 . Quiz
Test what you learn from this tutorial. Click here to give a quiz based on this tutorial.
8. Examples
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) |
Introduction To Programming Ppt
Next - Loops (for, while, do-while)