SlideShare a Scribd company logo
Computing Fundamentals
Dr. Muhammad Yousaf Hamza
Deputy Chief Engineer
switch statement
Dr. Yousaf, PIEAS
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The number must be
any integer value from 1 to 7n");
scanf("%d",&day);
if (day == 1)
printf("Mondayn");
else if (day == 2)
printf("Tuesdayn");
else if (day == 3)
printf("Wednesdayn");
// You may complete it yourself
Dr. Yousaf, PIEAS
The if/else Selection Structure
Nested if/else structures
• In previous example, the nested if/else structure is to
be used.
• Test for multiple cases by placing if/else selection
structures inside if/else selection structures
• Deep indentation usually not used in practice
• How can we solve this example more conveniently?
• Switch statement is a convenient way to code it.
Dr. Yousaf, PIEAS
Switch Statement
If you have a large decision tree, and all
the decisions depend on the value of the
same variable, you will probably want to
consider a switch statement instead of a
ladder of if...else or else if constructions.
Dr. Yousaf, PIEAS
// Example of switch statement
#include <stdio.h>
int main()
{
int day;
printf("The day number 1 means Mondayn");
printf("Please enter the number of day. n The
number must be any integer value from
1 to 7n");
scanf("%d",&day);
// Contd. (next page)
Dr. Yousaf, PIEAS
switch(day)
{
case 1: // 1 is one of possible value of day and so on.
printf("Mondayn");
break;
case 2:
printf("Tuesdayn");
break;
// please write cases 3 to 6 yourself
case 7:
printf("Sundayn");
break;
default:
printf("The number must be any integer value from 1 to 7n");
break;
}
getchar(); return 0;
}
Dr. Yousaf, PIEAS
• switch
– Useful when a variable or expression is tested for all the
values which can happen and different actions are taken
• Format
– Series of case labels and an optional default case
switch ( value )
{
case 1:
actions
case 2:
actions
default:
actions
}
– break; exits from structure
Dr. Yousaf, PIEAS
The switch Structure
• Flowchart of the switch structure
true
false
.
.
.
case a case a
action(s)
break
case b case b
action(s)
break
false
false
case z case z
action(s)
break
true
true
default
action(s)
Dr. Yousaf, PIEAS
• The expression used in a switch statement must have an integral or
character type.
• You can have any number of case statements within a switch. Each case
is followed by the value to be compared and a colon.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements
following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of
control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must
appear at the end of the switch. The default case can be used for
performing a task when none of the cases is true. No break is needed in
the default case.
Dr. Muhammad Yousaf Hamza
Switch Statement
Mathematical Functions
Dr. Yousaf, PIEAS
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
y = sin(PI/2.0); // argument is in radian
printf("%lf", y);
getchar();
return 0;
}
Output: 1.000000
Dr. Yousaf, PIEAS
// If given theta is in degree, then convert it first into radians.
#include<stdio.h>
#include<math.h> // use of math.h
#define PI 3.14
int main()
{
double y;
float theta_deg, theta_rad;
theta_deg = 90.0;
theta_rad = (PI/180)*theta_deg;
y = sin(theta_rad); // argument is in radian
printf("%lf", y);
getchar(); return 0; }
Output: 1.000000
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
• log() ……..> natural log
• log10() …………> base-10 logarithm
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
#include<stdio.h>
#include<math.h>
int main()
{
int k, j, r;
double x = 25.0, y, z;
y = sqrt(x);
printf("y = %lfn", y); // y = 5.000000
z = pow(y,3); // y^3, z = 125.000000
printf(" z = %lfn", z);
k = floor(15.2);
printf(" k = %dn",k);
// output: 15
j = ceil (15.2);
printf(" j = %dn",j);
// output: 16
r = abs (-67);
printf(" r = %dn",r);
// output: 67
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 1 star as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*");
getchar();
return 0;
}
Examples
Write a program that prints 2 stars as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 2 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*n*");
getchar();
return 0;
}
Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
Examples
Write a program that prints 10 stars as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("*n*n*n*n*n*n*n*n*n*");
getchar();
return 0;
}
Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Dr. Yousaf, PIEAS
Examples
Write a program that prints 100 stars in the format
shown in the previous slides.
Too much labor work?
There MUST be an easier way
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting 1 to 10 as
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting 1 to 10 as
Dr. Yousaf, PIEAS
#include<stdio.h>
int main()
{
printf("1n2n3n4n5n6n7n8n9n10");
getchar();
return 0;
}
Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
Dr. Yousaf, PIEAS
Examples
Write a program that prints counting from 1 to 1000 in
the format shown in the previous slide.
• Too much labor work?
• There MUST be an easier way
• Is there any?
• YES
LOOPS
Dr. Yousaf, PIEAS
Loops
Dr. Yousaf, PIEAS
For Loop
Dr. Yousaf, PIEAS
Printing the stars
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
printf("*n");
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 10
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 10; i++)
printf("%dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
Printing the counting from 1 to 1000
#include<stdio.h>
int main()
{
int i;
for (i = 1; i <= 1000; i++)
printf("i = %dn", i);
getchar();
return 0;
}
Dr. Yousaf, PIEAS
20-Spans System
I/P
O/P
80 km
80 km 80 km
Span 1
OA
DCF1
Span 2 Span 20
OAOA OADCF2 DCF20OA
The for Statement
• The most important looping structure in C.
• Generic Form:
for (initial ; condition ; increment )
statement
• initial, condition, and increment are C expressions.
• For loops are executed as follows:
1. initial is evaluated. Usually an assignment statement.
2. condition is evaluated. Usually a relational expression.
3. If condition is false (i.e. 0), fall out of the loop (go to step 6.)
4. If condition is true (i.e. nonzero), execute statement
5. Execute increment and go back to step 2.
6. Next statement
The for Statement
//For statement examples
#include <stdio.h>
int main ()
{
int count;
/* 1. simple counted for loop */
printf("Output with increment in loopn");
for (count =1; count <=10; count++)
printf ("%dn", count);
/* 2. counting backwards */
printf("Output with decrement in loopn");
for (count = 56; count >48; count--)
printf("%dn", count);
/* 3. for loop counting by
5's */
printf("Output with increment
of 5n");
for (count=0; count<32;
count += 5)
printf("%dn", count);
getchar();
return 0;
}
The for Statement
#include <stdio.h>
int main ()
{
int count;
/* initialization outside of loop */
count = 1;
for ( ; count < 7; count++)
printf("%d ", count);
printf(“n");
/* increment outside of loop */
count = 1;
for ( ; count < 7; )
{
printf("%d ", count);
count++;
}
getchar(); return 0; }
#include <stdio.h>
int main ()
{
int count;
int x, y;
/* compound statements increment */
for (x=0, y=100; x<y; x++, y--)
{
printf("%d, %dn", x,y);
}
getchar();
return 0;
}
Dr. Yousaf, PIEAS
The for Statement
The for Repetition Structure
• Format when using for loops
for ( initialization; loopContinuationTest; increment )
statement
• Example:
for (i = 1; i <= 10; i++)
printf("i = %dn", i);
–Prints the integers from one to ten
No
semicolon
(;) after last
expression
Dr. Yousaf, PIEAS
The for Structure: Observations
• Arithmetic expressions
Initialization, loop-continuation, and increment can
contain arithmetic expressions.
x = 2;
y = 10;
for ( j = x; j <= 4 * x * y; j += y / x )
is equivalent to
for ( j = 2; j <= 80; j += 5 )
Dr. Yousaf, PIEAS
The for Structure: Observations
• Notes about the for structure:
– "Increment" may be negative (decrement)
– If the loop continuation condition is initially
false
• The body of the for structure is not
performed
• Control proceeds with the next statement
after the for structure
– Control variable // for (i = 1; i <= 10; i++)
• Often printed or used inside for body, but not
necessary
Dr. Yousaf, PIEAS
For Loop
#include <stdio.h>
int main()
{
int x;
/* The loop goes if x < 10, and x increases by one in every
loop*/
for ( x = 0; x < 10; x++ )
{
/* Keep in mind that the loop condition checks
the conditional statement before it loops again.
consequently, when x equals 10 the loop breaks.
x is updated before the condition is checked. */
printf( "%dn", x );
}
return 0;
}
Dr. Yousaf, PIEAS

More Related Content

Similar to C Language Lecture 5

C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
Shahzaib Ajmal
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
Shahzaib Ajmal
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
Shahzaib Ajmal
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
Hemantha Kulathilake
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
Shahzaib Ajmal
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
Shahzaib Ajmal
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
Shahzaib Ajmal
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
SmitaAparadh
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
Shahzaib Ajmal
 
control statement
control statement control statement
control statement
Kathmandu University
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
imtiazalijoono
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
Mohammad Imam Hossain
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
SaraswathiRamalingam
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
ishaparte4
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
Sayed Ahmed
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
kinan keshkeh
 
Bsit1
Bsit1Bsit1
Bsit1
jigeno
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
Shahzaib Ajmal
 
Looping
LoopingLooping

Similar to C Language Lecture 5 (20)

C Language Lecture 7
C Language Lecture 7C Language Lecture 7
C Language Lecture 7
 
C Language Lecture 17
C Language Lecture 17C Language Lecture 17
C Language Lecture 17
 
C Language Lecture 6
C Language Lecture 6C Language Lecture 6
C Language Lecture 6
 
COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops COM1407: Program Control Structures – Repetition and Loops
COM1407: Program Control Structures – Repetition and Loops
 
C Language Lecture 16
C Language Lecture 16C Language Lecture 16
C Language Lecture 16
 
C Language Lecture 4
C Language Lecture  4C Language Lecture  4
C Language Lecture 4
 
C Language Lecture 3
C Language Lecture  3C Language Lecture  3
C Language Lecture 3
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptx
 
C Language Lecture 9
C Language Lecture 9C Language Lecture 9
C Language Lecture 9
 
control statement
control statement control statement
control statement
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions
 
SPL 8 | Loop Statements in C
SPL 8 | Loop Statements in CSPL 8 | Loop Statements in C
SPL 8 | Loop Statements in C
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAM
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptx
 
Introduction to c
Introduction to cIntroduction to c
Introduction to c
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets
 
Bsit1
Bsit1Bsit1
Bsit1
 
C Language Lecture 13
C Language Lecture 13C Language Lecture 13
C Language Lecture 13
 
Looping
LoopingLooping
Looping
 

More from Shahzaib Ajmal

C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
Shahzaib Ajmal
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
Shahzaib Ajmal
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
Shahzaib Ajmal
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
Shahzaib Ajmal
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
Shahzaib Ajmal
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
Shahzaib Ajmal
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
Shahzaib Ajmal
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
Shahzaib Ajmal
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
Shahzaib Ajmal
 

More from Shahzaib Ajmal (9)

C Language Lecture 21
C Language Lecture 21C Language Lecture 21
C Language Lecture 21
 
C Language Lecture 20
C Language Lecture 20C Language Lecture 20
C Language Lecture 20
 
C Language Lecture 15
C Language Lecture 15C Language Lecture 15
C Language Lecture 15
 
C Language Lecture 14
C Language Lecture 14C Language Lecture 14
C Language Lecture 14
 
C Language Lecture 12
C Language Lecture 12C Language Lecture 12
C Language Lecture 12
 
C Language Lecture 11
C Language Lecture  11C Language Lecture  11
C Language Lecture 11
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
C Language Lecture 2
C Language Lecture  2C Language Lecture  2
C Language Lecture 2
 
C Language Lecture 1
C Language Lecture  1C Language Lecture  1
C Language Lecture 1
 

Recently uploaded

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
RitikBhardwaj56
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Dr. Vinod Kumar Kanvaria
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
RAHUL
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
Jean Carlos Nunes Paixão
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
Celine George
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Akanksha trivedi rama nursing college kanpur.
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
AyyanKhan40
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
Priyankaranawat4
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
tarandeep35
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
sayalidalavi006
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
Nicholas Montgomery
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Excellence Foundation for South Sudan
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
amberjdewit93
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
Katrina Pritchard
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
Academy of Science of South Africa
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
TechSoup
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
History of Stoke Newington
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
simonomuemu
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Fajar Baskoro
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
Nicholas Montgomery
 

Recently uploaded (20)

The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...The simplified electron and muon model, Oscillating Spacetime: The Foundation...
The simplified electron and muon model, Oscillating Spacetime: The Foundation...
 
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
Exploiting Artificial Intelligence for Empowering Researchers and Faculty, In...
 
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UPLAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
LAND USE LAND COVER AND NDVI OF MIRZAPUR DISTRICT, UP
 
A Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdfA Independência da América Espanhola LAPBOOK.pdf
A Independência da América Espanhola LAPBOOK.pdf
 
How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17How to Fix the Import Error in the Odoo 17
How to Fix the Import Error in the Odoo 17
 
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama UniversityNatural birth techniques - Mrs.Akanksha Trivedi Rama University
Natural birth techniques - Mrs.Akanksha Trivedi Rama University
 
PIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf IslamabadPIMS Job Advertisement 2024.pdf Islamabad
PIMS Job Advertisement 2024.pdf Islamabad
 
clinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdfclinical examination of hip joint (1).pdf
clinical examination of hip joint (1).pdf
 
S1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptxS1-Introduction-Biopesticides in ICM.pptx
S1-Introduction-Biopesticides in ICM.pptx
 
Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5Community pharmacy- Social and preventive pharmacy UNIT 5
Community pharmacy- Social and preventive pharmacy UNIT 5
 
writing about opinions about Australia the movie
writing about opinions about Australia the moviewriting about opinions about Australia the movie
writing about opinions about Australia the movie
 
Your Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective UpskillingYour Skill Boost Masterclass: Strategies for Effective Upskilling
Your Skill Boost Masterclass: Strategies for Effective Upskilling
 
Digital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental DesignDigital Artefact 1 - Tiny Home Environmental Design
Digital Artefact 1 - Tiny Home Environmental Design
 
BBR 2024 Summer Sessions Interview Training
BBR  2024 Summer Sessions Interview TrainingBBR  2024 Summer Sessions Interview Training
BBR 2024 Summer Sessions Interview Training
 
South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)South African Journal of Science: Writing with integrity workshop (2024)
South African Journal of Science: Writing with integrity workshop (2024)
 
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat  Leveraging AI for Diversity, Equity, and InclusionExecutive Directors Chat  Leveraging AI for Diversity, Equity, and Inclusion
Executive Directors Chat Leveraging AI for Diversity, Equity, and Inclusion
 
The History of Stoke Newington Street Names
The History of Stoke Newington Street NamesThe History of Stoke Newington Street Names
The History of Stoke Newington Street Names
 
Smart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICTSmart-Money for SMC traders good time and ICT
Smart-Money for SMC traders good time and ICT
 
Pengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptxPengantar Penggunaan Flutter - Dart programming language1.pptx
Pengantar Penggunaan Flutter - Dart programming language1.pptx
 
Film vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movieFilm vocab for eal 3 students: Australia the movie
Film vocab for eal 3 students: Australia the movie
 

C Language Lecture 5

  • 1. Computing Fundamentals Dr. Muhammad Yousaf Hamza Deputy Chief Engineer
  • 3. #include <stdio.h> int main() { int day; printf("The day number 1 means Mondayn"); printf("Please enter the number of day. n The number must be any integer value from 1 to 7n"); scanf("%d",&day); if (day == 1) printf("Mondayn"); else if (day == 2) printf("Tuesdayn"); else if (day == 3) printf("Wednesdayn"); // You may complete it yourself Dr. Yousaf, PIEAS
  • 4. The if/else Selection Structure Nested if/else structures • In previous example, the nested if/else structure is to be used. • Test for multiple cases by placing if/else selection structures inside if/else selection structures • Deep indentation usually not used in practice • How can we solve this example more conveniently? • Switch statement is a convenient way to code it. Dr. Yousaf, PIEAS
  • 5. Switch Statement If you have a large decision tree, and all the decisions depend on the value of the same variable, you will probably want to consider a switch statement instead of a ladder of if...else or else if constructions. Dr. Yousaf, PIEAS
  • 6. // Example of switch statement #include <stdio.h> int main() { int day; printf("The day number 1 means Mondayn"); printf("Please enter the number of day. n The number must be any integer value from 1 to 7n"); scanf("%d",&day); // Contd. (next page) Dr. Yousaf, PIEAS
  • 7. switch(day) { case 1: // 1 is one of possible value of day and so on. printf("Mondayn"); break; case 2: printf("Tuesdayn"); break; // please write cases 3 to 6 yourself case 7: printf("Sundayn"); break; default: printf("The number must be any integer value from 1 to 7n"); break; } getchar(); return 0; } Dr. Yousaf, PIEAS
  • 8. • switch – Useful when a variable or expression is tested for all the values which can happen and different actions are taken • Format – Series of case labels and an optional default case switch ( value ) { case 1: actions case 2: actions default: actions } – break; exits from structure Dr. Yousaf, PIEAS
  • 9. The switch Structure • Flowchart of the switch structure true false . . . case a case a action(s) break case b case b action(s) break false false case z case z action(s) break true true default action(s) Dr. Yousaf, PIEAS
  • 10. • The expression used in a switch statement must have an integral or character type. • You can have any number of case statements within a switch. Each case is followed by the value to be compared and a colon. • The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal. • When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached. • When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement. • Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached. • A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case. Dr. Muhammad Yousaf Hamza Switch Statement
  • 12. #include<stdio.h> #include<math.h> // use of math.h #define PI 3.14 int main() { double y; y = sin(PI/2.0); // argument is in radian printf("%lf", y); getchar(); return 0; } Output: 1.000000 Dr. Yousaf, PIEAS
  • 13. // If given theta is in degree, then convert it first into radians. #include<stdio.h> #include<math.h> // use of math.h #define PI 3.14 int main() { double y; float theta_deg, theta_rad; theta_deg = 90.0; theta_rad = (PI/180)*theta_deg; y = sin(theta_rad); // argument is in radian printf("%lf", y); getchar(); return 0; } Output: 1.000000 Dr. Yousaf, PIEAS
  • 15. • log() ……..> natural log • log10() …………> base-10 logarithm Dr. Yousaf, PIEAS
  • 17. #include<stdio.h> #include<math.h> int main() { int k, j, r; double x = 25.0, y, z; y = sqrt(x); printf("y = %lfn", y); // y = 5.000000 z = pow(y,3); // y^3, z = 125.000000 printf(" z = %lfn", z); k = floor(15.2); printf(" k = %dn",k); // output: 15 j = ceil (15.2); printf(" j = %dn",j); // output: 16 r = abs (-67); printf(" r = %dn",r); // output: 67 getchar(); return 0; } Dr. Yousaf, PIEAS
  • 19. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS
  • 20. Examples Write a program that prints 1 star as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*"); getchar(); return 0; }
  • 21. Examples Write a program that prints 2 stars as Dr. Yousaf, PIEAS
  • 22. Examples Write a program that prints 2 stars as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*n*"); getchar(); return 0; }
  • 23. Examples Write a program that prints 10 stars as Dr. Yousaf, PIEAS
  • 24. Examples Write a program that prints 10 stars as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("*n*n*n*n*n*n*n*n*n*"); getchar(); return 0; }
  • 25. Examples Write a program that prints 100 stars in the format shown in the previous slides. Dr. Yousaf, PIEAS
  • 26. Examples Write a program that prints 100 stars in the format shown in the previous slides. Too much labor work? There MUST be an easier way Dr. Yousaf, PIEAS
  • 27. Examples Write a program that prints counting 1 to 10 as Dr. Yousaf, PIEAS
  • 28. Examples Write a program that prints counting 1 to 10 as Dr. Yousaf, PIEAS #include<stdio.h> int main() { printf("1n2n3n4n5n6n7n8n9n10"); getchar(); return 0; }
  • 29. Examples Write a program that prints counting from 1 to 1000 in the format shown in the previous slide. Dr. Yousaf, PIEAS
  • 30. Examples Write a program that prints counting from 1 to 1000 in the format shown in the previous slide. • Too much labor work? • There MUST be an easier way • Is there any? • YES LOOPS Dr. Yousaf, PIEAS
  • 33. Printing the stars #include<stdio.h> int main() { int i; for (i = 1; i <= 10; i++) printf("*n"); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 34. Printing the counting from 1 to 10 #include<stdio.h> int main() { int i; for (i = 1; i <= 10; i++) printf("%dn", i); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 35. Printing the counting from 1 to 1000 #include<stdio.h> int main() { int i; for (i = 1; i <= 1000; i++) printf("i = %dn", i); getchar(); return 0; } Dr. Yousaf, PIEAS
  • 36. 20-Spans System I/P O/P 80 km 80 km 80 km Span 1 OA DCF1 Span 2 Span 20 OAOA OADCF2 DCF20OA
  • 37. The for Statement • The most important looping structure in C. • Generic Form: for (initial ; condition ; increment ) statement • initial, condition, and increment are C expressions. • For loops are executed as follows: 1. initial is evaluated. Usually an assignment statement. 2. condition is evaluated. Usually a relational expression. 3. If condition is false (i.e. 0), fall out of the loop (go to step 6.) 4. If condition is true (i.e. nonzero), execute statement 5. Execute increment and go back to step 2. 6. Next statement
  • 38. The for Statement //For statement examples #include <stdio.h> int main () { int count; /* 1. simple counted for loop */ printf("Output with increment in loopn"); for (count =1; count <=10; count++) printf ("%dn", count); /* 2. counting backwards */ printf("Output with decrement in loopn"); for (count = 56; count >48; count--) printf("%dn", count); /* 3. for loop counting by 5's */ printf("Output with increment of 5n"); for (count=0; count<32; count += 5) printf("%dn", count); getchar(); return 0; }
  • 39. The for Statement #include <stdio.h> int main () { int count; /* initialization outside of loop */ count = 1; for ( ; count < 7; count++) printf("%d ", count); printf(“n"); /* increment outside of loop */ count = 1; for ( ; count < 7; ) { printf("%d ", count); count++; } getchar(); return 0; }
  • 40. #include <stdio.h> int main () { int count; int x, y; /* compound statements increment */ for (x=0, y=100; x<y; x++, y--) { printf("%d, %dn", x,y); } getchar(); return 0; } Dr. Yousaf, PIEAS The for Statement
  • 41. The for Repetition Structure • Format when using for loops for ( initialization; loopContinuationTest; increment ) statement • Example: for (i = 1; i <= 10; i++) printf("i = %dn", i); –Prints the integers from one to ten No semicolon (;) after last expression Dr. Yousaf, PIEAS
  • 42. The for Structure: Observations • Arithmetic expressions Initialization, loop-continuation, and increment can contain arithmetic expressions. x = 2; y = 10; for ( j = x; j <= 4 * x * y; j += y / x ) is equivalent to for ( j = 2; j <= 80; j += 5 ) Dr. Yousaf, PIEAS
  • 43. The for Structure: Observations • Notes about the for structure: – "Increment" may be negative (decrement) – If the loop continuation condition is initially false • The body of the for structure is not performed • Control proceeds with the next statement after the for structure – Control variable // for (i = 1; i <= 10; i++) • Often printed or used inside for body, but not necessary Dr. Yousaf, PIEAS
  • 44. For Loop #include <stdio.h> int main() { int x; /* The loop goes if x < 10, and x increases by one in every loop*/ for ( x = 0; x < 10; x++ ) { /* Keep in mind that the loop condition checks the conditional statement before it loops again. consequently, when x equals 10 the loop breaks. x is updated before the condition is checked. */ printf( "%dn", x ); } return 0; } Dr. Yousaf, PIEAS