meljun cortes c++ chap2 c program controls structure

55
Introducing s Prog ram ontrol Statements C Programming Language MELJUN CORTES

Upload: meljun-cortes-mbampa

Post on 02-Jun-2018

225 views

Category:

Documents


0 download

TRANSCRIPT

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 1/55

Introducing ’s Program

ontrol Statements

C Programming Language

MELJUN CORTES

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 2/55

MELJUN CORTES

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 3/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   3

Ex. 1

Read two numbers to A and B. Increase 1 number to A and decrease 1 number to B.

START

A = A + 1

B = B - 1

END

A, B 

READ A, B

#include <stdio.h>

void main() {

int a, b;

printf(“Input two numbers : ”);

scanf(“%d%d”, &a, &b);

}

a = a + 1;b = b - 1;

printf(“A’s value : %d\n”, a);

printf(“B’s value : %d\n”, b);

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 4/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   4

Ex. 2

 Read two numbers to A and B. Swap A for B.

A B

A B

temp

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 5/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   5

1. Become familiar with the if

If statements C’s selection statements

Conditional statements

Logical data

true

false

C doesn’t have boolean datatype.

Use int, char datatype for logical data

if(expression)  statement ;

Execute when expression is true

The result is true or false

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 6/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   6

if (10>9) printf(“true”);

1. Become familiar with the if

 Relational operator   > : greater than

expression

statement

true

false

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 7/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   7

if (9 < 5) printf(“this will not print”);

1. Become familiar with the if

 Relational operator   < : less than

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 8/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   8

1. Become familiar with the if

 Example 1

#include <stdio.h>

int main(void) {

int answer;

printf(“What is 10 + 14 ? ”);

scanf(“%d”, &answer);

if(answer == 10+14)printf(“Right!”);

return 0;

}

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 9/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   9

1. Become familiar with the if

 Example 2 (feet– meters conversion program)

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 10/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   10

1. Become familiar with the if

 Exercises

Write a program that asks the user for an integer

and then tells the user if that number is even or odd.(Hint, use C’s modulus operator %.)

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 11/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 12/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   12

if…

 else logic flow

2. Add the else

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 13/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 14/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   14

Example 2

2. Add the else

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 15/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   15

Exercises

2. Add the else

Write a program that requests two numbers and then

displays either their sum or product, depending on

what the user selects.

input two number : 28 5

select operator ( 1 : sum, 2 : product ) : 1

28 + 5 = 33

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 16/55MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   16

Exercises

2. Add the else

Write a program that requests two numbers and then

displays bigger number.

input two number : 28 5

28 > 5

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 17/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   17

Code block : { the statements }

3. Create blocks of code

One logical

unit

One logical

unit

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 18/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   18

If the user enters any positive number, thisfragment prints the message

3. Create blocks of code

scanf(“%d”, &num) ;

if(num > 0) {

printf(“This is ”) ;

printf(“an example of ”) ;

printf(“a code block.”) ;

}

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 19/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   19

Example 1 

3. Create blocks of code

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 20/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   20

Example 2 

3. Create blocks of code

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 21/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   21

Exercises 

3. Create blocks of code

Write a program that either adds or subtracts two

integers. First, prompt the user to choose an operation;

then prompt for the two numbers and display the result.

select operator ( 1 : add, 2 : subtract ) : 1

input two number : 28 5 28 + 5 = 33

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 22/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 23/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 24/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 25/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   25

4. Use the for loop

Simple exampleThis program uses a for loop to print the

numbers 1 through 10 on the screen

#include<stdio.h>

int main(void){

int num;

for(num=1; num<11; num=num+1)

printf(“%d ”, num) ;

printf(“terminating”);

return 0;

} 1 2 3 4 5 6 7 8 9 10 terminating

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 26/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 27/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   27

4. Use the for loop

Example (A for loop can run negatively)

#include<stdio.h>

int main(void){

int num;

//this loop will not execute

for(num=20; num>0; num=num-1)

printf(“%d ”, num) ;

printf(“terminating”);

return 0;

}20 19 18 17 …… 3 2 1 terminating

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 28/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 29/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   29

4. Use the for loop

Example (increment or decrement by more than one) This program counts to 100 by fives.

#include<stdio.h>

int main(void){

int i;

for(i=0; i<101; i=i+5)

printf(“%d ”, i) ;

return 0;

}0 5 10 15 20 …… 90 95 100

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 30/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   30

4. Use the for loop

Example 1. (addition-drill)

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 31/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 32/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   32

4. Use the for loop

Exercises 1. 

Create a program that prints the numbers from 1 to 100.

1 2 3 ……  ……  8 9 10: : : :: : : :: : : :

91 92 93 ……  ……  98 99 100

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 33/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   33

4. Use the for loop

Exercises 2. 

Write a program that prints the numbers between 17 to

100 that can be evenly divided by 17.

34 51 68 85

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 34/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   34

4. Use the for loop

Exercises 3. 

Write a program similar to the prime-number tester,

except that it displays all the factors of a number entered

By the user.

For example, if the user entered 8, it would respond with

2 and 4.

Enter a number : 8

8’s factor => 2 4

5 Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 35/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   35

5. Substitute C s increment anddecrement Operators

Increment operator (++)  num = num +1;   num++;

 Using for loop

Decrement operator (--)

 count = count -1;   count--; 

for(num=1; num<some_value; num=num+1) …… 

for(num=1; num<some_value; num++) …… 

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 36/55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 37/55

5 Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 38/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   38

5. Substitute C s increment anddecrement Operators

Before the variable( ++num )

First : increment expression execute.

After : the incremented value of variable is used for

expression.

#include<stdio.h>int main(void){

int i, j;

i = 10;

j = ++i;

// this will print 11 11

printf(“i and j : %d %d\n”, i, j) ;

return 0;

}

5 Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 39/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   39

5. Substitute C s increment anddecrement Operators

Before the variable( num++ )

If max = 1

Result

max = 2, count = 20

Arrangement

count = 10 * ++max;

count = 10 * ++max;max = max + 1;

count = 10 * max;

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 40/55

5. Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 41/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   41

5. Substitute C s increment anddecrement Operators

Example 2.

5. Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 42/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   42

5. Substitute C s increment anddecrement Operators

Exercises 1. 

Change all appropriate assignment statements in this program

to increment or decrement statements. 

#include <stdio.h>

int main(void) {int a, b;

a=1;

a = a + 1;

b = a;

b = b  – 1;

printf(“%d %d”, a, b);

return 0;

}

5. Substitute C’s increment and

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 43/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   43

5. Substitute C s increment anddecrement Operators

Exercises 2. 

Enter a number. Compute summation until the number

from 1.

enter a number : 10

sum of 1 ~ 10 : 55

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 44/55

6 Expand printf()’s capabilities

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 45/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   45

6. Expand printf() s capabilities

Backslash codes

Represent characters that cannot be entered from the keyboard,

and non-printing characters

Escape character

6 Expand printf()’s capabilities

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 46/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   46

6. Expand printf() s capabilities

'\n' (newline character)

It translates it into a carriage return/linefeed combination.

• backslash codes are character constants.

char ch;

ch = '\t'; /* assign ch the tab character */

6 Expand printf()’s capabilities

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 47/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   47

number square cube

===============================1 1 1

2 4 8

3 9 27

: : :10 100 1000

6. Expand printf() s capabilities

 ExerciseUsing for loop (1~10)

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 48/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   48

gand Logical operators

 Relational operators Compare two values and return a true or false result based

upon that comparison.

 Logical operators

Connect together true/false results.

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 49/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   49

gand Logical operators

 Truth table (AND, OR, NOT)

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 50/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   50

gand Logical operators

 Precedence of operators The relational and logical operators are both lower in

precedence than the arithmetic operators.

10 + count > a + 12 (10 + count) > (a + 12)

var > max || ! (max==100) && 0 <= item

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 51/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   51

gand Logical operators

 Example 1.

if(count != 0) ……  if(count)

The expression !count is true only if count is zero.

if(count == 0) ……  if(!count)

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 52/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   52

gand Logical operators

 Example 2. (the outcome of a relational or logical operation)

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 53/55

7. Program with C’s relational

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 54/55

MELJUN CORTES MBA MPA BSCS

School of Computer I nformation   54

and Logical operators

 Example 3. (exclusive-OR)  Demonstrate the xor()

Skills Check

8/10/2019 MELJUN CORTES C++ Chap2 C Program Controls Structure

http://slidepdf.com/reader/full/meljun-cortes-c-chap2-c-program-controls-structure 55/55

Skills Check

 Page 66 ~ 67