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

Mod 2 C

Uploaded by

krupamariam008
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)
37 views

Mod 2 C

Uploaded by

krupamariam008
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/ 18

Module 2

Decision Making in C
Decision-making statements decide the direction and the flow of the program. They are also
known as conditional statements because they specify conditions with boolean expressions
evaluated to a true or false boolean value. If the condition is true, a given block of code will
execute; if the condition is false, the block will not execute.The conditional statements (also
known as decision control structures) such as if, if else, switch, etc. are used for decision-
making purposes in C programs.
Branching statements in C

The capacity to decide what to do and how to run distinct pieces of code based on specific
circumstances is essential in the realm of programming. This ability to manage the execution flow is
provided by branching statements in the C programming language.

Conditional Statements in C
Conditional statements in C are used to execute certain blocks of code based on certain
conditions. These statements allow the program to make decisions and control the flow of
execution.
Types of Conditional Statements in C
 Simple if statements
 if-else statements
 else-if statements
 Nested if statements
 switch case statements
If statement in C
The “if” statement is one of the most common conditional statements in C programming.
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 of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
Flow chart

Example of if in C
#include <stdio.h>
void main()
{
int i = 10;
if (i > 15) {
printf("10 is greater than 15");
}
getch();
}
If-else statement in C
The if-else statement in the C programming language allows you to execute a block of code if
a certain condition is true, and another block of code if the condition is false. It provides a
way to make decisions and control the flow of the program based on different conditions.
This else block is optional.
Syntax
if(condition)
{//statement(s) 1;
}
else
{//statement(s) 2;
}
Flow chart
How if-else statement works?
 If the test condition is evaluated as true, then the statement inside the body of if is
executed. So the statement inside the body of else will be skipped from execution.
 If the test condition is evaluated as false, then the statement inside the body of else is
executed. So the statement inside the body of if will be skipped from execution.
else-if statements in C
 Another conditional statements in C is the “else if” statement, which allows for testing
multiple conditions.
 In this case, if the condition of the preceding “if” statement is false, the program
checks the condition of the “else if” statement.
 If it is true, the code within the corresponding block is executed.
 Lastly, the “else” statement is used as a default option when all the preceding
conditions are false.
 If none of the conditions in the “if” and “else if” statements are true, the code within
the “else” block is executed.
Syntax
if (condition1)
{statement(s) 1;
}
else if (condition2)
{statement(s) 2;
}
else
{statement(s) 3;
}
Flow chart
Example: Check whether a number is positive, negative or 0
#include <stdio.h>
void main()
{ int n = 5;

if (n > 0)
{
printf("Positive");
}
else if (n < 0)
{
printf("Negative");
}
else {
printf("Zero");
}
getch();
}
Output:positive
nested if-else statements
Nested if-else statements in the C programming language refer to using if statements inside
another if statement. This allows for more complex decision-making in a C program. In
simple terms, a nested if-else statement is an if-else statement inside another if-else
statement. It helps programmers to create multiple branches of conditions and execute
different blocks of code based on those conditions. By nesting if-else statements,
programmers can evaluate multiple conditions without limitations.
Syntax
if (condition 1)
{
if (condition 2)
{
//statements 1
}
else
{
//statements 2
}
}
else
{
if (condition 3)
{
//statements 3
}
else
{
//statements 4
}
}
Flow chart
Example :example of a nested if-else statement in C.
#include <stdio.h>
void main()
{
int number;
printf("Please enter an integer number: ");
scanf("%d", &number);
if (number > 0)
{
printf("The entered number is positive.\n");
if (number % 2 == 0)
{printf("The entered number is even.\n");}
else
{printf("The entered number is odd.\n");}
}
else if (number < 0)
{printf("The entered number is negative.\n");}
else
{printf("The entered number is zero.\n");}
return 0;
}
Output:
Please enter an integer number: 25
The entered number is positive.
The entered number is odd.
Switch Statement in C
Switch case statement evaluates a given expression and based on the evaluated
value(matching a certain condition), it executes the statements associated with it. Basically, it
is used to perform different actions based on different conditions(cases).

Syntax of switch Statement in C


switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;
default: default_statement;
}
Rules of the switch case statement
Following are some of the rules that we need to follow while using the switch statement:
1. In a switch statement, the “case value” must be of “char” and “int” type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
5. The default Statement is also optional.
Flow Chart

C Program:
#include <stdio.h>
void main() {
int num = 2;
switch (num)
{
case 1:
printf("Value is 1\n");
break;
case 2:
printf("Value is 2\n");
break;
case 3:
printf("Value is 3\n");
break;
default:
printf("Value is not 1, 2, or 3\n");
break;
}
getch();
}

Output:Value is 2
Jump Statements in C:
Jump statements are control flow statements that let you change the order in which programs
execute. These statements provide flexibility and control over program logic to the
programmer. Types of Jump Statements in C
1. break
2. continue
3. goto

1. Break Statement in C
The break statement in C language is used to exit from a loop or switch statement,
prematurely, before the loop or switch block has been fully executed. When a break statement
is encountered inside a loop, it immediately terminates the loop, and control is transferred to
the next statement outside the loop. Similarly, when a break statement is encountered inside a
switch block, it terminates the block, and control is transferred to the next statement outside
the block.
The below image depicts the working of the break statement in C:

#include <stdio.h>
int main() {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
break;
}
printf("%d\n", i);
}

getch();
}
Output
1
2
3
4

2. C continue statement
The continue statement in C language is used to bring the program control to the beginning of
the loop. The continue statement skips some lines of code inside the loop and continues with
the next iteration. It is mainly used for a condition so that we can skip some code for a
particular condition.
Syntax:
//loop statements
continue;

Flowchart of continue in C
Continue statement example 1
#include<stdio.h>
int main(){
int i=1;
for(i=1;i<=7;i++)
{
if(i==4)
{ continue;
}
printf("%d \n",i);
}
getch()
}
Output
1
2
3
5
6
7
(5 is not printed on)
3. Goto Statement in C
The C goto statement is a jump statement which is sometimes also referred to as an
unconditional jump statement. The goto statement can be used to jump from anywhere to
anywhere within a function.
Flowchart of goto Statement in C

Syntax:
goto label;
.
.
label:
Examples: write a program where we need to check if a number is even or not
#include <stdio.h>
void main()
{
Int num=6;
if (num % 2 == 0)
goto even;
else
goto odd;

even:
printf("%d is even", num);
return;
odd:
printf("%d is odd", num);
}

getch();
}
Output:6 is even
C Loops
The looping can be defined as repeating the same process multiple times until a specific
condition satisfies. There are three types of loops used in the C language. In this part of the
tutorial, we are going to learn all the aspects of C loops.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It enables us to alter the
flow of the program so that instead of writing the same code again and again, we can repeat
the same code for a finite number of times. For example, if we need to print the first 10
natural numbers then, instead of using the printf statement 10 times, we can print inside a
loop which runs up to 10 iterations.
Advantage of loops in C
1) It provides code reusability.
2) Using loops, we do not need to write the same code again and again.
3) Using loops, we can traverse over the elements of data structures (array or linked lists).

Types of C Loops
There are three types of loops in C language that is given below:
 do while
 while
 for
1. for loop in C
A for loop is a control structure that enables a set of instructions to get executed for a
specified number of iterations. It is an entry-controlled loop.
for loop Flowchart
Syntax
for(initialization; test condition; update expression){
//code to be executed
}
 Here, the initialization statement is executed first and only once.
 The test condition is checked, if false the loop terminates
 If the test condition is true, the body of the loop executes
 The update expression gets updated
 Again the test condition is evaluated
 The process repeats until the test condition becomes false.

Example:Program to print numbers from 1 to 5


#include <stdio.h>
void main() {
int i;
for (i = 0; i < 5; i++)
{
printf("%d\n", i+1);
}
getch();
}
Output
1
2
3
4
5
 We know it will take 5 iterations to print5 numbers so, we have used the for loop.
 i is initialized to 0.
 The condition i<5 will be checked. It is true, therefore i+1 i.e. 1 gets printed.
 Then i increments to 1 again the condition, i<5 is evaluated.
 The process will repeat until i become 5.
2. while loop in C
It repeatedly carries out a series of instructions till a condition is true. It is an entry-
controlled loop. The while loop in C is used when we don’t know the number of iterations.

Syntax
while(test condition){
//code to be executed
}
If the test condition inside the () becomes true, the body of the loop executes else loop
terminates without execution. The process repeats until the test condition becomes false.
Example: Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
Run Code >>
 In the above code, i is initialized to 1 before the start of the loop.
 The test condition, i<=5 is evaluated. If true, the body of the loop executes.
 If the condition becomes false in the beginning itself, the program control does not
even enter the loop once.
 The loop executes until i becomes 5.
Output
1
2
3
4
5

3.do...while loop in C
It is an exit-controlled loop. It prints the output at least once before checking the condition.
Afterwards, the condition is checked and the execution of the loop begins.
do...while loop Flowchart
Syntax
do{
//code to be executed
}while(test condition);
 The body of the loop executes before checking the condition.
 If the test condition is true, the loop body executes again.
 Again the test condition is evaluated.
 The process repeats until the test condition becomes false.

Example: Print numbers from 1 to 5


#include <stdio.h>
int main()
{
int i = 0;
do {
printf("%d\n", i+1);
i++;
}
while (i < 5);
getch();}
Output
1
2
3
4
5
Run Code
 The above code prints numbers from 1 to 5 using the do while loop in C.
 It prints 1 before checking if i less than 5
 It checks the condition i<5cand executes until i becomes 5

You might also like