303105104 - Computational Thinking for
Structured Design-1
Dr. Mukesh Kumar
Associate Professor
Computer Science & Engineering
CHAPTER-3
Conditional Flow Statements, Iterative Statements, Jumping
Statements and Pointers
Contents
1. Conditional Flow Statements(Decision making)
2. Iterative Statements (Loops)
3. Jumping statements (break and continue statements)
4. Pointers
1. Decision Making
● Decision making is used to specify the order in which statements
are executed.
● Decision making in a C program
using:-
○ if statement
○ if…else statement
○ if…else if…else statement
○ nested if...else statement
○ Ladder if else statement
○ Switch case Statement
1.1 if statement
Example: if statement
● Program to display #include <stdio.h>
int main()
only negative {
int number;
numbers on screen. printf("Enter an integer:");
scanf("%d", &number);
if (number < 0)
{
printf("You entered %d is negative .\n", number);
}
printf("The if statement is easy.");
return 0;
}
1.2 if...else statement
if...else statement executes some code if the
test expression is true (nonzero) and some
other code if the test expression is false (0).
Syntax of if...else
if (test Expression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
Example: if...else statement
// Example of if ..else Program to check whether an integer entered by the user is odd or even
#include <stdio.h>
int main()
{
int number;
printf("Enter an integer: ");
scanf("%d",&number);
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
}
1.3 if...else if....else Statement
● The if...else statement executes two different codes
depending upon whether the test expression is true or false.
Sometimes, a choice has to be made from more than 2
possibilities.
● The if...else if…else statement allows you to check for multiple
test expressions and execute different codes for more than two
conditions
Syntax of if...else if....else statement.
Example: if...else if....else statement
// Program to relate two integers using =, > or < #include //checks if number1 is greater than number2.
else if (number1 > number2)
<stdio.h> int main() {
{ printf("Result: %d > %d",
number1, number2);
int number1, number2; }
printf("Enter two integers: "); // if both test expression is false
else {
scanf("%d %d", &number1,&number2); printf("Result: %d < %d",number1, number2);
if(number1 == number2) }
return 0;
{ }
printf("Result: %d = %d“,number1,number2);
}
1.4 Nested if else statement
● Nested if else statement is same like if else statement, where
new block of if else statement is defined in existing if else
statement.
● Used when need to check more than one conditions at a time
Syntax of Nested If else Statement
Example of Nested if else Statement
2. Loops
► Loops are used in programming to repeat a specific block until
some end condition is met.
►
There are three loops in C programming:
o for loop
o while loop
o do...while loop
o Nested loops
2.1 for Loop
The syntax of a for loop is:
for (initialization; testExpression; Increment or decrement)
{
// codes
}
Flowchart of For Loop
Example: for loop
// Program to calculate the sum of first n
natural numbers
for(count = 1; count <= n; ++count)
// Positive integers 1,2,3...n are known as
natural numbers {
#include <stdio.h> sum =sum+count;
int main() }
{ printf("Sum = %d", sum);
int n, count, sum = 0; return 0;
printf("Enter a positive integer: "); }
scanf("%d", &n);
2.2 while loop
The syntax of a while loop is:
while (testExpression)
{
//codes
}
Example: while loop
/ Program to find factorial of a // loop terminates when number is less
number / / than or equal to 0
// For a positive integer n, factorial = 1*2*3...n while (number > 0) {
// factorial = factorial*number;
#include<stdio.h> factorial *= number;
int main(){ --number;
int number; long factorial; }
printf("Enter an integer: "); printf("Factorial= %lld",
scanf("%d",&number); factorial); return 0;
factorial =1; }
2.3 do...while loop
► The do..while loop is similar to the while loop with one important
difference.
► The body of do...while loop is executed once, before checking the test
expression.
► The do...while loop is executed at least once.
do...while loop Syntax
do
{
// codes
}
while (testExpression);
Example: do...while loop
// Program to add numbers until user enters zero
#include <stdio.h>
int main() {
double number, sum = 0;
// loop body is executed at least once
do{
printf("Enter a number: "); printf("Sum %.2lf",sum);
scanf("%lf", &number); return 0;
sum+= number; }
}while(number != 0.0);
2.4 Nested loops
Defining loop within another loop is
called nested loops for ( init; condition; increment )
{
for ( init; condition; increment )
{
statement(s); // inner loop
}
statement(s);// outer loop
}
#include <stdio.h> isPrime = 1;
#include <stdio.h> for (j = 2; j <= i / 2; j++) {
int main() if (i % j == 0)
{ {
int i, j, limit; int isPrime; isPrime = 0;
printf("Enter the limit: "); break; } }
scanf("%d", &limit); if (isPrime)
printf("Prime numbers up to %d are:\n", limit); { printf("%d ", i); }
for (i = 2; i <= limit; i++) } printf("\n"); return 0; }
2.4 Nestedloops (Con..)
► Syntax while while(condition)
loop {
while(condition)
{
statement(s);
}
statement(s);
}
while (j <= i)
#include <stdio.h>
{
int main()
{ sum += j;
int i = 1, j, sum; // Outer loop for numbers from 1 to 5 j++;
while (i <= 5) }
{
printf("Sum of numbers from 1 to %d is: %d\n", i, sum);
j = 1;
i++;
sum = 0;
// Inner loop to calculate sum of numbers from 1 to I
}
return 0;
}
3. Break And Continue Statement
► What is BREAK meant?
► What is CONTINUE meant?
Syntax of if...else if....else statement.
► The break statement terminates the loop immediately when it is
encountered.
► The break statement is used with decision making statement
such as if...else.
► Syntax of break statement
break;
How break statement works?
Flowchart Of Break Statement
Example: break statement
3.2 Continue Statement
► The continue statement skips some statements inside the
loop.
► The continue statement is used with decision making stateme such
as if...else.
► Syntax of continue Statement
continue;
Flowchart of Continue Statement
How Continue Statement Works?
Example: continue statement
// Program to calculate sum of maximum of 10 scanf("%lf",&number);
numbers // If user enters negative number,skip this number
if(number < 0.0) {
// Negative numbers are skipped
continue;
from calculation }
// sum = sum + number;
# include <stdio.h>
} sum += number;
int main(){
int i;
printf("Sum = %.2lf",sum);
double number, sum = 0.0;
return 0;
for(i=1; i <= 10; ++i) { printf("Enter a n%d: ",i);
3.2. Switch
Statement
► The if...else if…else statement allows you to execute a block code among many
alternatives. If you are checking on the value of a single variable in if...else if…else
statement, it is better to use switch statement.
► The switch statement is often faster than nested if...else (not always).
Also, the syntax of switch statement is cleaner and
easy to understand.
Syntax of switch...case
switch (n){
case constant1:
// code to be executed if n is equal to constant1;
break;
case constant2:
// code to be executed if n is equal to
constant2; break;
….
default:
// code to be executed if n doesn't match any constant
}
Switch Statement Flowchart
Example: switch Statement
// Program to create a simple calculator
// Performs addition, subtraction, multiplication or division
depending the input from user
# include <stdio.h>
int main() {
char operator;
double firstNumber,secondNumber;
printf("Enter an operator (+, -, *,): ");
scanf("%c", &operator); printf("Enter
two operands: ");
scanf("%lf %lf",&firstNumber, &secondNumber);
Syntax of if...else if....else statement.
switch(operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf",firstNumber, secondNumber, firstNumber+secondNumber); break;
case '-':
printf("%.1lf - %.1lf = %.1lf",firstNumber, secondNumber, firstNumber-secondNumber); break;
case '*':
printf("%.1lf * %.1lf = %.1lf",firstNumber, secondNumber, firstNumber*secondNumber); break;
case '/':
printf("%.1lf / %.1lf = %.1lf",firstNumber, secondNumber, firstNumber/firstNumber); break;
// operator is doesn't match any case constant (+, -, *, /)
default:
printf("Error! operator is not correct");
3.3 goto
Statement
► The goto statement is used to alter the normal sequence of a C program.
Syntax of goto Statement
goto label;
... .. ...
... .. ...
... .. ...
label:
statement;
What is Label?
► The label is an identifier. When goto statement is encountered, control of the program
jumps to label: and starts executing the code.
Example: goto Statement
#include <stdio.h>
int main()
{
int i; float number, sum = 0.0, average; jump:
for(i = 1; i <= 10; ++i)
average = sum / (i - 1);
{
printf("Enter a number: "); // Adjusting the divisor to the number of valid inputs
scanf("%f", &number);
printf("Sum = %.2f\n", sum);
if(number < 0.0)
goto jump; printf("Average = %.2f", average);
sum += number;
return 0;
// sum = sum + number
} }
bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double
TYPE CONVERSION
Type conversion in C is the process of converting one data type to another. Type conversion is
performed by a compiler.
There are two types of Conversion:
1. Implicit Type Conversion
Also known as ‘automatic type conversion’.
A. Done by the compiler on its own, without any external trigger from the user.
B. Generally takes place when in an expression more than one data type is present. In such conditions type
conversion (type promotion) takes place to avoid loss of data.
C. All the data types of the variables are upgraded to the data type of the variable with the largest data type.
bool -> char -> short int -> int -> unsigned int -> long -> unsigned -> long long -> float -> double -> long double
#include <stdio.h>
int main()
{
int x = 10; // integer x
char y = 'a'; // character c
// y implicitly converted to int. ASCII
// value of 'a' is 97
x = x + y;
// x is implicitly converted to float
float z = x + 1.0;
printf("x = %d, z = %f", x, z);
return 0;
}
Explicit Type Conversion
This process is also called type casting and it is user-defined. Here the user can typecast the result
to make it of a particular data type. The syntax in C Programming:
// C program to demonstrate explicit type
casting
#include<stdio.h>
int main()
{
double x = 1.2;
// Explicit conversion from double to int
int sum = (int)x + 1;
printf("sum = %d", sum);
return 0;
}
Tokens
A token in C can be defined as the smallest individual element of the C programming language that is
meaningful to the compiler. It is the basic component of a C program
Types of Tokens in C
The tokens of C language can be classified into six types based on the functions they are used to perform.
1.Keywords
2.Identifiers- Identifiers are used as the general terminology for the naming of variables, functions, and
arrays.
Certain rules should be followed while naming c identifiers which are as follows:
•They must begin with a letter or underscore(_).
•They must consist of only letters, digits, or underscore. No other special character is allowed.
•It should not be a keyword.
•It must not contain white space.
•It should be up to 31 characters long as only the first 31 characters are significant.
3.Constants 4. Strings 5. Special Symbols 6. Operators