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

Inelec Lessons Officie-Introduction Programming

inelec Lessons Office-Introduction Programming

Uploaded by

Amar Aoudia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

Inelec Lessons Officie-Introduction Programming

inelec Lessons Office-Introduction Programming

Uploaded by

Amar Aoudia
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

M’Hamed BOUGARA university

Institute of Electrical and Electronic Engineering (IGEE)

EE 121. Office suite

Lecture 3: Introduction to programming and C


programming language

Dr. F. Kerouh
Electronics Department
IGEE, UMBB

2015-2016 Chap 3. Introduction to programming and C language © 1


Part III. Program looping
III.1 Conditional
– if (expr) { … } else {…}
– switch (expr) { case c1: case c2: … }

III.2 Iteration
– while (expr) { … } zero or more iterations
– do … while (expr) at least one iteration
– for ( init ; valid ; next ) { … }

III.3 Jump
– goto label

2015-2016 Chap 3. Introduction to programming and C language © 2


Part III.1 Decision making elements

2015-2016
Chap 3. Introduction to programming and 3
C language ©
Outlines
III.1. 1 Introduction to Decision Making Statements

III.1. 2 If statement
 Examples

III.1. 3 If-else statement


 Examples

III.1. 4 Switch statement


 Examples
2015-2016
Chap 3. Introduction to programming and 4
C language ©
III.1.1 Introduction
 Decision making elements are used to have a program that
executes different statements depending on certain conditions.

 In a sense, they make a program “smarter” by allowing


different choices to be made. In C, there are three decision
making statements:

1. if execute a statement or not


2. if-else choose to execute one of two statements
3. switch choose to execute one of a number of statements

2015-2016 Chap 3. Introduction to programming and C language © 5


III.1.2 If statement
 The if statement allows branching (decision making)
depending upon a condition. Program code is executed
or skipped.

The basic syntax is


if (control expression)
program statement;

 If the control expression is TRUE, the body of the if


statement is executed. If it is FALSE, the body of the if
statement is skipped.

2015-2016 Chap 3. Introduction to programming and C language © 6


III.1.2.1 If statement examples
 Example 1:
if (x!=0)
y/=x;

 Example 2:
if (grade>=90)
{
printf("\nYour grade is %d",grade);
printf("\nCongratulations!");
}

 Example 3:
if (nbr1>=nbr2) if (( nbr1>=nbr2 ) && ( nbr1>=nbr3) )
if (nbr1>=nbr3) printf(“%d is the greater number”,nbr1);
printf(“%d is the greater number“,nbr1);

2015-2016 Chap 3. Introduction to programming and C language © 7


III.1.2. 2 If –else statement
Used to decide between two courses of action. The syntax of the
if-else statement is:
if (expression)
statement1;
else
statement2;

• If the expression is TRUE, statement1 is executed; statement2 is


skipped.
• If the expression is FALSE, statement2 is executed; statement1
is skipped.
2015-2016 Chap 3. Introduction to programming and C language © 8
III.1.2.2 If –else statement: Examples

if (x<y) if (letter == 'e') {


min=x; ++e_count;
else ++vowel_count; }
min=y; else
++other_count;

2015-2016 Chap 3. Introduction to programming and C language © 9


III.1.2. 3 If –else Ladder
Write a program that
counts how many vowels
there are in a piece of
text?

This is possible by
nesting if-else statements
together to make what is
called an if-else ladder.

2015-2016
Chap 3. Introduction to programming and 10
C language ©
III.1. 3 switch statement
 It is a better way of writing a program which employs an if-else ladder. It is C’s built-in
multiple branch decision statement. The syntax for the switch statement is as follows:

Should be
included at the Marks the Begin
end of each case and the end of the
statement. switch statement
It causes an exit
from the switch
shunt.

Optional: equivalent to
else used with if
statement

2015-2016 Chap 3. Introduction to programming and C language © 11


III.1. 3 switch statement: Example

2015-2016
Chap 3. Introduction to programming and 12
C language ©
III.1. 3 switch statement
 The switch statement works as follows:

1. Integer control expression is evaluated.

2. A match is looked for between this expression


value and the case constants. If a match is found,
execute the statements for that case. If a match is not
found, execute the default statement.

3. Terminate switch when a break statement is


encountered or by “falling out the end”.

2015-2016
Chap 3. Introduction to programming and 13
C language ©
III.1. 3 switch statement
 Important rematks while using a switch
statement:

1. Case values must be unique.

2. Switch statement only tests for equality.

3. The control expression can be of type


character since they are internally treated as
integers.
2015-2016
Chap 3. Introduction to programming and 14
C language ©
III.1. 3 switch statement: Example

2015-2016
Chap 3. Introduction to programming and 15
C language ©
III.1. 4 Conclusion
Law of Mosher about software development:

“Don’t worry if it doesn’t work right. If


everything did, you’d be out of a job.”…

2015-2016
Chap 3. Introduction to programming and 16
C language ©

You might also like