Lecture 3: Structured Programming and Control Structures in C - Prof. Lan Xiang, Study notes of Electrical and Electronics Engineering

The concepts of structured programming, control structures, if statement, if-else statement, switch statement, relational operators, and logic operators in c. It includes examples and sample code for grade calculation and quadratic equation root finding.

Typology: Study notes

Pre 2010

Uploaded on 03/11/2009

koofers-user-byz-1
koofers-user-byz-1 🇺🇸

5

(1)

10 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
1
EE114:
Programming Concepts for Engineers
Lecture 3
2
Overview
nStructured programming
nSelection structure
nif statement
nif-else statement
nswitch statement
3
Structured Programming
nSequence structure
nSelection structure
nif statement
nif-else statement
nswitch statement
nRepetition structure
nwhile loop
ndo-while loop
nfor loop
nAll these 3 structures together, selection, sequence, and
repetition, are referred as control structures. All C programs
could be written in terms of these three types of control
structures only and we call this structured programming.
4
If statement
if (expression)
statement;
Expression Statement
True
False
Exit
Entry
if (grade < 60)
printf("Sorry, you failed this class!\n");
flowchart
pf3
pf4

Partial preview of the text

Download Lecture 3: Structured Programming and Control Structures in C - Prof. Lan Xiang and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!

EE114:

Programming Concepts for Engineers

Lecture 3

Overview

n Structured programming

n Selection structure

n if statement

n if-else statement

n switch statement

Structured Programming

n Sequence structure

n Selection structure

n if statement

n if-else statement

n switch statement

n Repetition structure

n while loop

n do-while loop

n for loop

n All these 3 structures together, selection, sequence, and

repetition, are referred as control structures. All C programs

could be written in terms of these three types of control

structures only and we call this structured programming.

If statement

if (expression)

statement;

Expression

Statement

True

False

Exit

Entry

if (grade < 60)

printf("Sorry, you failed this class!\n");

flowchart

if (expression) {

statement_1;

statement_2;

……

statement_n;

}

If statement with compound statement

if (a<5) {

printf("a is less than 5");

printf("Sorry, data is not correct!\n");

If-else statement

if (expression)

statement0;

else

statement1;

Expression Statement

True

False

Exit

Entry

Statement

if (a>=0)

printf(“Positive!\n”);

else

printf(“Negative!\n”);

If-else with compound statement

if (expression) {

statement0;

statement1;

else {

statement3;

statement4;

if (a>=0){

printf(“Positive!\n”);

x=a;

else {

printf(“Negative!\n”);

x=-a;

Relational Operators

n > if (c>b) if c is great than b

n >= if (c>=b) if c is great than or equal to b

n < if (c<b) if c is less than b

n <= if (c<=b) if c is less than or equal to b

n == if (c==b) if c is equal to b

n != if (c!=b) if c is not equal to b

#include <stdio.h> #include <math.h> /* math libaray is included for sqrt() function */

int main( void ) { float a, b, c, delta, div;

printf( "Enter Value for Coefficient 'a', 'b', 'c' : "); scanf( "%f %f %f", &a, &b, &c);

delta = bb - 4.0 * a c; div = 2.0 * a;

if( a == 0 ) { /* if a is 0, only one single root / printf( "Single root = %g\n", -c/b) ; } else { /if a is not 0, then we have two roots / if (delta >= 0.0) { / if roots are real/ printf( "Roots are real\n"); printf( "Root1 = %g\n ", (-b + sqrt(delta))/div ); printf( "Root2 = %g\n ", (-b - sqrt(delta))/div ); } else { / if roots are complex */ printf( "Roots are complex\n"); printf( "Root1 = (%g) + i(%g)\n", - b/div, sqrt(-delta)/div ); printf( "Root2 = (%g) - i(%g)\n", - b/div, sqrt(-delta)/div ); } } return 0 ; } 14

Enter value for Coefficient ‘a’, ‘b’, ‘c’ : 6 3 -

Roots are real

Root1 = 0.

Root2 = -1.

Enter value for Coefficient ‘a’, ‘b’, ‘c’ : 0 6 4

Single root = -0.

Enter value for Coefficient ‘a’, ‘b’, ‘c’ : 6 3 4

Roots are complex

Root1 = (0.25) + i(0.777282)

Root2 = (0.25) - i(0.777282)