Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Unit 2 CP

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 37

PROGRAMMING IN C

UNIT 2
Control Statements
Conditional Statements
if statement:
• The if statement is the most simplest decision-making
statement. It is used to decide whether a certain
statement or block of statements will be executed or
not.
Syntax:
If(condition)
{
//statements to execute if condition is true
}
Flowchart:
Example
#include <stdio.h>
int main()
{
int i = 15;

if (i > 10) {
printf("15 is greater than 10");
}

printf("I am Not in if");


}
If-else statement :
• The if-else statement consists of two blocks, one for false
expression and one for true expression.
Syntax:
If(condition)
{
//statements to execute if condition is true
}
else
{
//statements to execute if condition is false
}
Flowchart:
Example:
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
printf("i is smaller than 15");
}
else {
printf("i is greater than 15");
}
return 0;
}
Nested if-else statement:
• Nested if statements mean an if statement inside another if statement.
Syntax:
If(condition 1)
{
//executes if condition 1 is true
If(condition 2)
{
//executes if condition 2 is true
}
else
{
//executes if condition 2 is false
}
}
Flowchart:
Example:
#include <stdio.h>
int main()
{
int i = 10;
if (i == 10) {
if (i < 15) {
printf("i is smaller than 15\n");
}
else {
printf("i is greater than 15");
}
}

return 0;
}
else-if Ladder:
• The if else if statements are used when the user has to decide among
multiple options.
• The 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.
Syntax:
If(condition)
Statements;
else if(condition)
Statements;
.
.
.
else
Statements;
Flowchart:
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");
}
Switch Statement
• Multiple actions can be taken using the "switch" statement based on the value of
a variable or expression. When managing several instances, it is especially
helpful.
• The switch block consists of cases to be executed based on the value of the
switch variable.
• The switch expression should evaluate to either integer or character. It cannot
evaluate any other data type.
Syntax:
switch (expression) {
case value1:
statements;
case value2:
statements;
.
.
.
default:
statements;
}
Example:
#include <stdio.h>
int main() {
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
The ? : Operator
Syntax:
Condition? Expression1 : Expression2;
• Here, Condition is the condition to be evaluated. If the
condition is true then we will execute and return the result
of Expression1 otherwise if the condition is false then we will
execute and return the result of Expression2.
• We may replace the use of if..else statements with
conditional operators.
Example:
a = 10;
b = 15;
x = (a>b) ? a : b;
Unconditional Statements
• C uses unconditional branching statements to
alter the typical course of program execution.
These statements give programmers the freedom
to jump to a specific location in their code under
any circumstance.
• The various categories of unconditional branching
statements in C are as follows:
1. goto Statement
2. break Statement
3. continue Statement
Goto statement
• The "goto" command enables programmers to leap directly
to a designated labelled statement within their code.
Syntax:
goto label;

...

label:
// code to be executed
Example:
#include <stdio.h>
int main()
{
int num = 1;
if (num == 1) {
goto label;
}
printf("This statement is skipped.\n");
label:
printf("The value of num is 1.\n");
return 0;
}
Break Statement
• The "break" statement is frequently employed in
switch statements as well as looping constructions
like "for", "while", and "do-while".
• It enables you to skip the statement that follows
the loop or switch and end the execution of the closest
enclosing loop or switch statement.
Syntax:
break;
Example:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i<= 5; i++)
{
if (i == 3)
{
break;
}
printf("%d ", i);
}
return 0;
}
Continue Statement
• The continue statement is used to go to the next
iteration of a loop while skipping the current iteration.
• When a loop encounters the continue statement, the
current iteration of the loop is ended, and control is
returned to the top of the loop to begin the
subsequent iteration.
• Usually, this statement is used in loops like for or
while.
Syntax:
continue;
Example:
#include <stdio.h>
int main()
{
int i;
for (i = 1; i<= 5; i++)
{
if (i == 3)
{
continue;
}
printf("%d ", i);
}
return 0;
}
Looping Statements
• Loops in programming are used to repeat a block of code until
the specified condition is met.
• A loop statement allows programmers to execute a statement or
group of statements multiple times without repetition of code.
There are mainly two types of loops in C Programming:
1.Entry Controlled loops: In Entry controlled loops, the test
condition is checked before entering the main body of the
loop. For Loop and While Loop is Entry-controlled loops.
2.Exit Controlled loops: In Exit controlled loops the test
condition is evaluated at the end of the loop body. The loop
body will execute at least once, irrespective of whether the
condition is true or false. do-while Loop is Exit Controlled loop.
Sentinel controlled loops:
• A sentinel controlled loop is also called an
indefinite repetition loop because the number of
iterations is not known before the loop starts
executing.
• In a sentinel controlled loop, a special value called
sentinel value is used to change the loop control
expression from true to false in order to
determine whether to execute the loop body.
Counter controlled loops:
• A counter controlled loop is also known as definite
repetition loop, since the number of iterations is
known before the loop begins to execute.
For Loop
• For loop in C programming is a repetition control
structure that allows programmers to write a loop that
will be executed a specific number of times.
• For loop enables programmers to perform n number of
steps together in a single line.
Syntax:
for (initialization; test condition; increment/decrement(+
+/--))
{
body of for loop
}
Example:
#include <stdio.h>
int main()
{
int i = 0;
for (i = 1; i <= 10; i++)
{
printf( "Hello World\n");
}
return 0;
}
While Loop
• The while loop in c is to be used in the scenario where we
don't know the number of iterations in advance.
• The block of statements is executed in the while loop until the
condition specified in the while loop is satisfied.
Syntax:
initialization;
while (test_condition)
{
// body of the while loop

increment/decrement;
}
Example:
#include <stdio.h>
int main()
{
int i = 2;
while(i < 10)
{
printf( "Hello World\n");
i++;
}
return 0;
}
Do-While Loop
• The do-while loop is similar to a while loop but the only difference lies
in the do-while loop test condition which is tested at the end of the
body.
• In the do-while loop, the loop body will execute at least
once irrespective of the test condition.
Syntax:
initialization;
do
{
// body of do-while loop

increment/decrement;

} while (test_condition);
Example:
#include <stdio.h>
int main()
{
int i = 2;
do
{
printf( "Hello World\n");
i++;
} while (i < 1);
return 0;
}

You might also like