SlideShare a Scribd company logo
1 of 44
Download to read offline
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

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
 
CONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxCONTROL FLOW in C.pptx
CONTROL FLOW in C.pptxSmitaAparadh
 
Programming Fundamentals Decisions
Programming Fundamentals  Decisions Programming Fundamentals  Decisions
Programming Fundamentals Decisions imtiazalijoono
 
LAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMLAB PROGRAMS SARASWATHI RAMALINGAM
LAB PROGRAMS SARASWATHI RAMALINGAMSaraswathiRamalingam
 
unit 2-Control Structures.pptx
unit 2-Control Structures.pptxunit 2-Control Structures.pptx
unit 2-Control Structures.pptxishaparte4
 
Introduction to c
Introduction to cIntroduction to c
Introduction to cSayed Ahmed
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 
2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_sets2Bytesprog2 course_2014_c1_sets
2Bytesprog2 course_2014_c1_setskinan keshkeh
 

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

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

Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesPooky Knightsmith
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxMohamed Rizk Khodair
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文中 央社
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptxVishal Singh
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsISCOPE Publication
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital ManagementMBA Assignment Experts
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptxPoojaSen20
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024Borja Sotomayor
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...Nguyen Thanh Tu Collection
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................MirzaAbrarBaig5
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxAdelaideRefugio
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...EduSkills OECD
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxMarlene Maheu
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSean M. Fox
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17Celine George
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesAmanpreetKaur157993
 

Recently uploaded (20)

Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"Mattingly "AI and Prompt Design: LLMs with NER"
Mattingly "AI and Prompt Design: LLMs with NER"
 
Trauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical PrinciplesTrauma-Informed Leadership - Five Practical Principles
Trauma-Informed Leadership - Five Practical Principles
 
Supporting Newcomer Multilingual Learners
Supporting Newcomer  Multilingual LearnersSupporting Newcomer  Multilingual Learners
Supporting Newcomer Multilingual Learners
 
demyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptxdemyelinated disorder: multiple sclerosis.pptx
demyelinated disorder: multiple sclerosis.pptx
 
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文會考英文
 
The Liver & Gallbladder (Anatomy & Physiology).pptx
The Liver &  Gallbladder (Anatomy & Physiology).pptxThe Liver &  Gallbladder (Anatomy & Physiology).pptx
The Liver & Gallbladder (Anatomy & Physiology).pptx
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
Including Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdfIncluding Mental Health Support in Project Delivery, 14 May.pdf
Including Mental Health Support in Project Delivery, 14 May.pdf
 
Scopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS PublicationsScopus Indexed Journals 2024 - ISCOPUS Publications
Scopus Indexed Journals 2024 - ISCOPUS Publications
 
8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management8 Tips for Effective Working Capital Management
8 Tips for Effective Working Capital Management
 
ANTI PARKISON DRUGS.pptx
ANTI         PARKISON          DRUGS.pptxANTI         PARKISON          DRUGS.pptx
ANTI PARKISON DRUGS.pptx
 
UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024UChicago CMSC 23320 - The Best Commit Messages of 2024
UChicago CMSC 23320 - The Best Commit Messages of 2024
 
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
ĐỀ THAM KHẢO KÌ THI TUYỂN SINH VÀO LỚP 10 MÔN TIẾNG ANH FORM 50 CÂU TRẮC NGHI...
 
male presentation...pdf.................
male presentation...pdf.................male presentation...pdf.................
male presentation...pdf.................
 
Observing-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptxObserving-Correct-Grammar-in-Making-Definitions.pptx
Observing-Correct-Grammar-in-Making-Definitions.pptx
 
Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...Andreas Schleicher presents at the launch of What does child empowerment mean...
Andreas Schleicher presents at the launch of What does child empowerment mean...
 
PSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptxPSYPACT- Practicing Over State Lines May 2024.pptx
PSYPACT- Practicing Over State Lines May 2024.pptx
 
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading RoomSternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
Sternal Fractures & Dislocations - EMGuidewire Radiology Reading Room
 
How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17How To Create Editable Tree View in Odoo 17
How To Create Editable Tree View in Odoo 17
 
Major project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategiesMajor project report on Tata Motors and its marketing strategies
Major project report on Tata Motors and its marketing strategies
 

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