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

Lecture2 2 C Programming II

This document summarizes key points from a lecture on C programming: - It discusses assignment operations, symbolic constants, and computational forms of scientific numbers. Basic arithmetic, relational, and logical operators are also covered. - Conditional statements like if-else, switch, and ternary operators ?: are explained. Examples are given to illustrate relational, logical, and conditional expressions. - Exercises are provided to practice assignments, conditionals, and getting user input using scanf. Homework involves writing a program to check if a user input is past or fail based on being above or below 60.

Uploaded by

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

Lecture2 2 C Programming II

This document summarizes key points from a lecture on C programming: - It discusses assignment operations, symbolic constants, and computational forms of scientific numbers. Basic arithmetic, relational, and logical operators are also covered. - Conditional statements like if-else, switch, and ternary operators ?: are explained. Examples are given to illustrate relational, logical, and conditional expressions. - Exercises are provided to practice assignments, conditionals, and getting user input using scanf. Homework involves writing a program to check if a user input is past or fail based on being above or below 60.

Uploaded by

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

LECTURE 2-2

C PROGRAMMING II
March 17, 2016
Outline
• Assignment Operation
• Symbolic Constant
• Computational Form of Scientific Numbers
• Basic Arithmetic Operations
• Relational Operators and if-conditional
• Logical Operators
• switch-conditional
• ‘?’ conditionals
Assignment Operation
Assignment of a value (constant) to a variable

x=100;

Overwriting by the assignment operation


x=100;
x=x+1;
These two successive operation assigns 101 to x
Symbolic Constants
• You can define a symbol by any constant value. You do it like

#define clight 2.99792458e8 // no semi-colon!


float omega(…)
{ … float w=clight*k; …}

• Since clight is defined, you cannot use it as a variable inside blocks.

• Such a definition helps to make the code more understandable


Computational Form of Scientific Numbers

Scientific Number Computer-friendly Form


2.99792458x108 2.99792458e8 or 2.99792458e+8
-3.458x1023 -3.458e23 or -3.458e+23
1.602x10-19 1.602e-19
Basic Arithmetic Operation
• Sum, subtract, multiplication, division, remainder
Action Symbol Description Example
sum + Sum x and y x+y
subtraction - Subtract y from x x-y
multiplication * Multiply y to x x*y
division / Divide x by y x/y
Remainder after dividing x
remainder % x%y
by y
y  mx  b y = m*x + b

y  ax 2  bx  c y = a*x*x + b*x +c
x yx
m m = (x + y + z) / 3
3

Note: How to represent power such as x3? There is no operator for the
power. It should be done like x*x*x, or a math-library function
‘pow(x,3)’ should be called.
Basic Arithmetic Operation
++x raise x-value by 1, and use x for other operation
x++ use x first, and raise its value by 1
--x Lower x-value by 1, and use x for other opera
x-- Use x first, and lower its value by 1
Basic Arithmetic Operation
x += y x=x+y
x -= y x=x-y
x *= y x=x*y
x /= y x=x/y

Example
x += 1 // x = x + 1
x *= 5 // x = x * 5
x -= y + 1 // x = x - (y + 1)
x *= y + 1 // x = x * (y + 1)
x += y / z // x = x + y / z
x %= x + y // x = x % (x + y)
Relational Operators and if-conditional
• If-conditional statements are used as follows
if(conditional) { …}
else if(conditional) { …}
else if(conditional) {…}

else { … }

• Nested if-sentences can be used


if(conditional)
{
if(conditional2) { …. }
}
else
{
if(conditional3) {…}
else if(conditional4) {…}
else { …}
}
Relational Operators and if-conditional
• Compare two variables or values
• The result is 1 (true) or 0 (true)

Operator Description Example


== if x and y are the same? x == y
!= if x and y are different to each other? x != y
> If x is larger than y? x>y
< If x is smaller than y? x<y
>= If x is larger than or equal to y? x >= y
<= If x is smaller than or equal to y? x <= y
Logical Operators
• Operators for combination of multiple conditionals
• Results in 1 (true) or 0 (false)

A && B And operator: 1 (true) only when both A and B are true
Or operator: 1 (true) when either A and B is true. 0 (false) only when
A || B
both A and B are false
!A Not operator: 1(true) when A is 0 (false), vice versa

Examples

int x=1; int y=2;


if(x==1 && y==2) { … } // it is true, so … is excuted.
if(x==1 && y==3) { … } // it is false, so … is not excuted
if(x==1 || y==3) { … } // it is true,
if(x==1 || y==2) {… } // it is true,
if(x==3 || y==5) { … } // False
if(!(x==1)) { … } // False
if(!(x==2)) { … } // True
if(!x) { … } // False
if(!0) { … } // True
switch Statements
• Convenient when selecting the true case among multiple choices

switch(integer)
{
case c1: yes
statement 1; Statement 1
break; no
yes
case c2:
statement 2; Statement 2
break; no
yes
…..
Statement 3
default: no
statement;
break;
}
‘?’ Conditionals
• When exp1 is true, return exp2, otherwise, return exp3

exp1 ? exp2 : exp3

Exp1 is true
Exp1 is false

(5 > 2) ? 5 : 2 //Return 5
(1.2 < 1.1) ? 1 : 0 // Return 0
(x == 0) ? 1: 2 // when x== 0, return 1. Otherwise return 2
Exercise
Exercise 1
Make a C-program. Assign any value to a variable x. if x is the same as 2.5, then
print 2x on the screen. If not, print a sentence ‘Different!’ followed by the value of x.

Exercise 2
Make a C-program. Assign any values to variables x and y. Compare x with 1.3.
Compare y with 2.5. If both are the true, print the product of them on the screen.
If either x=1.3 OR y=2.5 is (are) true, then print the product of x2 and y2. If both are
false, print x-y.
Homework 2-2
Question 1
Make a C-program, which prints ‘Pass’ if an input value is larger than 60, and ‘Fail’ if
smaller than 60. For a C-program to get user’s input, you have to use scanf()
function. How to use scanf function can be easily found in many web sites.

Due on midnight March 24, 2016


Get User’s Input by scanf() function
The format of scanf() is very similar to printf(), but it uses ‘call-by-reference’.

Example.

#include “stdio.h”
#include “stdlib.h”

main()
{
float x,y;
int a;

scanf(“%g %g %d”,&x,&y,&a);

printf(“x=%g y=%g a=%d\n”,x,y,a);


}
END OF
LECTURE 2-2

You might also like