Branching Statements - Module 3
Branching Statements - Module 3
Branching Statements
Prepared by
Sneha Kashyap
Assistant Professor
CS & IT
AJU
C – If statement
Syntax of if statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false
then the statements inside “if” are skipped.
C If else statement
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of
“else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are
executed.
C Nested If..else statement
C – else..if statement
Important Points:
1. else and else..if are optional statements, a program having only “if” statement would run fine.
4. If none of the conditions are met then the statements in else block gets executed.
5. Just like relational operators, we can also use logical operators such as AND ( &&), OR(||) and NOT(!).
C For loop
5) As mentioned above, the counter variable can be decremented as well. In the below example the variable gets
decremented each time the loop runs until the condition num>10 returns false.
Multiple initialization inside for Loop in C
2. It has two test conditions joined together using AND (&&) logical operator. Note: You cannot use multiple test
conditions separated by comma, you must use logical operator such as && or || to join conditions.
int main()
int i,j;
return 0;
}
C – while loop
Syntax of while loop:
step1: The variable count is initialized with value 1 and then it has been tested for the condition.
step2: If the condition returns true then the statements inside the body of while loop are executed else control
comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested again for the loop condition.
step1: The variable count is initialized with value 1 and then it has been tested for the condition.
step2: If the condition returns true then the statements inside the body of while loop are executed else control
comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested again for the loop condition.
2. This can also be used in switch case control structure. Whenever it is encountered in switch-case block, the
control comes out of the switch-case(see the example below).