0% found this document useful (0 votes)
10 views

UNIT-II STARTING PART

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

UNIT-II STARTING PART

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

Decision Making with if statement:

The conditional statements (also known as decision control structures) such


as if, if else, switch, etc. are used for decision-making purposes in Programs.
They are also known as Decision-Making Statements and are used to evaluate
one or more conditions and make the decision whether to execute a set of
statements or not. These decision-making statements in programming
languages decide the direction of the flow of program execution.
Need of Conditional Statements
There come situations in real life when we need to make some decisions and
based on these decisions, we decide what should we do next. Similar situations
arise in programming also where we need to make some decisions and based
on these decisions we will execute the next block of code. For example, in C if
x occurs then execute y else execute z. There can also be multiple conditions
like in C if x occurs then execute p, else if condition y occurs execute q, else
execute r. This condition of C else-if is one of the many ways of importing
multiple conditions.
Types of Conditional Statements in C:-

Following are the decision-making statements available in C


1. if Statement
2. if-else Statement
3. Nested if Statement
4. if-else-if Ladder
5. switch Statement
6. Conditional Operator
7. Jump Statements:
 break
 continue
 goto
 return
Simple if statement:
The if statement is the most simple decision-making statement. It is used to
decide whether a certain statement or block of statements will be executed or
not i.e if a certain condition is true then a block of statements is executed
otherwise not.
Syntax:

The general form of a simple if statement is,

if(expression)

Statement1 are executed;

Statement2 are executed;

If the expression returns true, then the statement1 will be executed,


otherwise statement1 is skipped and only the statement2 is executed.
Flowchart of if Statement:-

Example:
#include <stdio.h>
int main()
{
int i = 10;
if (i > 15)
{
printf("10 is greater than 15");
}
printf("I am Not in if");
}
Output:
I am Not in if
As the condition present in the if statement is false. So, the block below the if
statement is not executed.
If else statement:
The if statement alone tells us that if a condition is true it will execute a block
of statements and if the condition is false it won’t. The if-else
statement consists of two blocks, one for false expression and one for true
expression.
Syntax:
The general form of a simple if...else statement is,
if(expression)
{
statement block1;
}
else
{
statement block2;
}
If the expression is true, the statement-block1 is executed, else statement-
block1 is skipped and statement-block2 is executed.
Flowchart of if-else Statement:-

Example:
#include <stdio.h>
void main( )
{
int x, y;
x = 15;
y = 18;
if (x > y )
{
printf("x is greater than y");
}
else
{
printf("y is greater than x");
}
}
Output:
Y is greater than x.
Nested if-else in C:
A nested if in C is an if statement that is the target of another if statement.
Nested if statements mean an if statement inside another if statement. Yes,
both C and C++ allow us to nested if statements within if statements, i.e, we
can place an if statement inside another if statement.
Syntax:
The general form of a nested if...else statement is,
if( expression )
{
if( expression1 )
{
statement block1;
}
else
{
statement block2;
}
}
else
{
statement block3;
}
if expression is false then statement-block3 will be executed, otherwise the
execution continues and enters inside the first if to perform the check for the
next if block, where if expression 1 is true the statement-block1 is executed
otherwise statement-block2 is executed.
Flowchart for Nested if else statement:
Example:
#include <stdio.h>

void main( )
{
int a, b, c;
printf("Enter 3 numbers...");
scanf("%d%d%d",&a, &b, &c);
if(a > b)
{
if(a > c)
{
printf("a is the greatest");
}
else
{
printf("c is the greatest");
}
}
else
{
if(b > c)
{
printf("b is the greatest");
}
else
{
printf("c is the greatest");
}
}
}

else if ladder:-

The if else if statements are used when the user has to decide among multiple
options. The C if statements are executed from the top down. As soon as one of the
conditions controlling the if is true, the statement associated with that if is executed,
and the rest of the C else-if ladder is bypassed. If none of the conditions is true, then
the final else statement will be executed. if-else-if ladder is similar to the switch
statement.

Syntax:
The general form of else-if ladder is,
if(expression1)
{
statement block1;
}
else if(expression2)
{
statement block2;
}
else if(expression3 )
{
statement block3;
}
else
default statement;
The expression is tested from the top(of the ladder) downwards. As soon as
a true condition is found, the statement associated with it is executed.
Flowchart of if-else-if Ladder:

Example:
#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}

Output: i is 20
switch Statement in C:-
The switch case statement is an alternative to the if else if ladder that can be
used to execute the conditional code based on the value of the variable
specified in the switch statement. The switch block consists of cases to be
executed based on the value of the switch variable. If there is no match,
then default block is executed(if present).
Syntax:
The general form of switch statement is,
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}

Rules for using switch statement

1. The expression (after switch keyword) must yield an integer value i.e the
expression should be an integer or a variable or an expression that
evaluates to an integer.
2. The case label values must be unique.
3. The case label must end with a colon(:)
4. The next line, after the case statement, can be any valid C statement.
Flowchart of switch:-

Example:
#include<stdio.h>
int main()
{
int day;
printf(“Enter a day:”);
scanf(“%d”,&day);
switch(day)
{
Case1:printf(“Sunday”);
Case2:printf(“Monday\n”);
Case3:printf(“Tuesday\n”);
Case4:printf(“Wednesday\n”);
Case5:printf(“Thrusday\n”);
Case6:printf(“Friday\n”);
Case7:printf(“Saturday\n”);
break;
default:printf(“Invalid choice\n”);
}
return 0;
}
Output:
Enter a day: 4
Wednesday
Thursday
Friday
Saturday

Difference between if and switch:


IF SWITCH
If statement can evaluate float Switch statement cannot evaluate float
conditions. conditions.
If statement can evaluate relational Switch statement cannot evaluate
operators relational operators

Conditional Operator(?:):
The conditional operator is used to add conditional code in our program. It is
similar to the if-else statement. It is also known as the ternary operator as it
works on three operands.

Syntax of Conditional Operator

(condition) ? [true_statements] : [flase_statements];

Flowchart of Conditional Operator:-

Example:
#include<stdio.h>
#include<conio.h>
int main()
{
int a,b;
printf(“enter the values:”);
scanf(“%d%d”,&a,&b);
(a>b)?printf(“a is greater than b”):printf(“a is not greater than b”);
return 0;
}
Output:
Enter the values: 20 10
a is greater than b

Jump Statements in C:
These statements are used in C for the unconditional flow of control
throughout the functions in a program. They support four types of jump
statements:
break
This loop control statement is used to terminate the loop. As soon as
the break statement is encountered from within a loop, the loop iterations stop
there, and control returns from the loop immediately to the first statement after
the loop.
Syntax of break
break;
Basically, break statements are used in situations when we are not sure about
the actual number of iterations for the loop or we want to terminate the loop
based on some condition.
continue
This loop control statement is just like the break statement.
The continue statement is opposite to that of the break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or
execute the next iteration. When the continue statement is executed in the
loop, the code inside the loop following the continue statement will be skipped
and the next iteration of the loop will begin.
Syntax of continue
continue;
Flowchart of Continue:

Example:-
#include <stdio.h>

int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) {

// If i is equals to 6,
// continue to next iteration
// without printing
if (i == 6)
continue;

else
// otherwise print the value of i
printf("%d ", i);
}
return 0;
}

Output: 1 2 3 4 5 6 7 8 9 10
goto :
The goto statement in C/C++ also referred to as the unconditional jump
statement can be used to jump from one point to another within a function.
Syntax of goto
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;

In the above syntax, the first line tells the compiler to go to or jump to the
statement marked as a label. Here, a label is a user-defined identifier that
indicates the target statement. The statement immediately followed after
‘label:’ is the destination statement. The ‘label:’ can also appear before the
‘goto label;’ statement in the above syntax.

Example:
#include <stdio.h>
int main()
{
int num,i=1;
printf("Enter the number whose table you want to print?");
scanf("%d",&num);
table:
printf("%d x %d = %d\n",num,i,num*i);
i++;
if(i<=10)
goto table;
}
Output:
Enter the number whose table you want to print?10
10 x 1 = 10
10 x 2 = 20
10 x 3 = 30
10 x 4 = 40
10 x 5 = 50
10 x 6 = 60
10 x 7 = 70
10 x 8 = 80
10 x 9 = 90
10 x 10 = 100

Loops in C
During programming, sometimes we might need to execute a certain code
statement again and again. We can write the code statement as many times as
we need it to execute but that would be very inefficient, because what if you
want a code statement to execute a 100 times? This is why we use loops.

In any programming language including C language, loops are used to execute a


single statement or a set of statements, repeatedly, until a particular condition is
satisfied.

How Loops in C works?

The below diagram depicts a loop execution,


As per the above diagram, if the Test Condition is true, then the loop is
executed, and if it is false then the execution breaks out of the loop. After the
loop is successfully executed the execution again starts from the Loop entry and
again checks for the Test condition, and this keeps on repeating.

The sequence of statements to be executed is kept inside the curly


braces { } known as the Loop body. After every execution of the loop
body, condition is verified, and if it is found to be true the loop body is
executed again. When the condition check returns false, the loop body is not
executed, and execution breaks out of the loop.

Loops are broadly classified into two types:

1. Entry controlled loops

In this kind of loop, the condition is checked before executing the loop's body.
So, if the condition is never true, it won't execute even once. For
example, for and while loop.

2. Exit controlled loops

In this kind of loop, the condition is checked after the loop's body is executed,
i.e., in the end. Hence, even if the condition is not fulfilled, this loop will
execute one time. The do-while loop is an example of exit controlled loop.
Types of Loop in C

There are 3 types of Loop in C language, namely:

1. while loop
2. for loop
3. do while loop

while loop in C:

The while loop is an entry controlled loop. It is completed in 3 steps.

 Variable initialization.(e.g int x = 0;)


 condition(e.g while(x <= 10))
 Variable increment or decrement ( x++ or x-- or x = x + 2 )
Syntax of while Loop:

variable initialization;
while(condition)
{
statements;
variable increment or decrement;
}
The following flowchart shows the flow of execution when we use a while loop.
Here, we can see that firstly, we initialize our iterator. Then we check the
condition of while loop. If it is false, we exit the loop and if it is true, we enter
the loop. After entering the loop, we execute the statements inside
the while loop, update the iterator and then again check the condition. We do
the same thing unless the condition is false.
Program to print first 10 natural numbers using while loop

#include<stdio.h>

void main( )
{
int x;
x = 1;
while(x <= 10)
{
printf("%d\t", x);
/* below statement means, do x = x+1, increment x by 1 */
x++;
}
}
Output:
1 2 3 4 5 6 7 8 9 10

for loop in C

The for loop in C is used to execute a set of statements repeatedly until a


particular condition is satisfied. We can say it is an open ended loop. General
format is,

for(initialization; condition; increment/decrement)


{
statement-block;
}
In the for loop in C language, we have exactly two mandatory semicolons, one
after initialization and second after the condition. In this loop we can have
more than one initialization or increment/decrement as well, separated using
comma operator. But it can have only one condition.

The for loop is executed as follows:


1. It first evaluates the initialization code.
2. Then it checks the condition expression.
3. If it is true, it executes the for-loop body.
4. Then it evaluate the increment/decrement condition and again follows
from step 2.
5. When the condition expression becomes false, it exits the loop.

Following is a flowchart explaining how the for loop executes.

We first initialize our iterator. Then we check the condition of the loop. If it
is false, we exit the loop and if it is true, we enter the loop. After entering the
loop, we execute the statements inside the for loop, update the iterator and then
again check the condition. We do the same thing unless the test condition
returns false.
Program to print first 10 natural numbers using for loop

#include<stdio.h>
void main( )
{
int x;
for(x = 1; x <= 10; x++)
{
printf("%d\t", x);
}
}
Output:
1 2 3 4 5 6 7 8 9 10

Nested for loop in C

We can also have nested for loops, i.e one for loop inside another for loop in C
language. This type of loop is generally used while working with multi-
dimensional arrays.
Syntax: Nested for loops
for(initialization; condition; increment/decrement)
{
for(initialization; condition; increment/decrement)
{
statement ;
}
}

do while loop in C

In some situations it is necessary to execute body of the loop once before testing
the condition. Such situations can be handled with the help of do-while loop.
The do statement evaluates the body of the loop first and at the end, the
condition is checked using while statement. It means that the body of the loop
will be executed at least once, even though the starting condition inside while is
initialized to be false. General syntax is,

do
{
…………..
…………..
}
While(condition);
Following is the flowchart for do-while loop:

We initialize our iterator. Then we enter body of the do-while loop. We execute
the statement and then reach the end. At the end, we check the condition of the
loop. If it is false, we exit the loop and if it is true, we enter the loop. We keep
repeating the same thing unless the condition turns false.

Program to print first 10 multiples of 5 using do-while loop:-


#include<stdio.h>

int main()
{
int a, i;
a = 5;
i = 1;
do
{
printf("%d\t", a*i);
i++;
}
while(i <= 10);
}

.....
.....
}

You might also like