0% found this document useful (0 votes)
26 views40 pages

Branching Statements - Module 3

Uploaded by

sanjibkumar2387
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views40 pages

Branching Statements - Module 3

Uploaded by

sanjibkumar2387
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 40

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

Syntax of 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.

2. else and else..if cannot be used without the “if”.

3. There can be any number of else..if statement in a if else..if block.

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

Step 1: First initialization happens and the counter variable gets


initialized.
Step 2: In the second step the condition is checked, where the counter
variable is tested for the given condition, if the condition returns true
then the C statements inside the body of for loop gets executed, if the
condition returns false then the for loop gets terminated and the control
comes out of the loop.
Step 3: After successful execution of statements inside the body of loop,
the counter variable is incremented or decremented, depending on the
operation (++ or –).
Various forms of for loop in C
I am using variable num as the counter in all the following
examples –
1) Here instead of num++, I’m using num=num+1 which is
same as num++.

2) Initialization part can be skipped from loop as shown below,


the counter variable is declared before the loop.

Note: Even though we can skip initialization part but semicolon


(;) before condition is must, without which you will get
compilation error.

3) Like initialization, you can also skip the increment part as


we did below. In this case semicolon (;) is must after condition
logic. In this case the increment or decrement part is done
inside the loop.
4) This is also possible. The counter variable is initialized before the loop and incremented inside the 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

We can have multiple initialization in the for loop as shown below.

for (i=1,j=1;i<10 && j<10; i++, j++)


What’s the difference between above for loop and a simple for loop?

1. It is initializing two variables. Note: both are separated by comma (,).

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.

3. It has two variables in increment part. Note: Should be separated by comma.


Example of for loop with multiple test conditions
#include <stdio.h>

int main()

int i,j;

for (i=1,j=1 ; i<3 || j<5; i++,j++)

printf("%d, %d\n",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.

The program is an example of infinite while loop.


Since the value of the variable var is same (there
is no ++ or – operator used on this variable,
inside the body of loop) the condition var<=2 will
be true forever and the loop would never
terminate.
C – break statement
1. It is used to come out of the loop instantly. When a break statement is encountered inside a loop, the control
directly comes out of loop and the loop gets terminated. It is used with if statement, whenever used inside loop.

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).

You might also like