 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double,

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
For loops For loops are controlled by a counter variable. for( c =init_value;c
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
Problem Solving and Program Design in C (5th Edition) by Jeri R. Hanly and Elliot B. Koffman Chapter 4 (Conditional Statements) © CPCS
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Some loop programs 1: Read in 10 integers and output their sum
CS1061: C Programming Lecture 8: Repetition A. O’Riordan, 2004.
C programming an Introduction. Types There are only a few basic data types in C. char a character int an integer, in the range -32,767 to 32,767 long.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
לולאות 02 יולי יולי יולי 1502 יולי יולי יולי 1502 יולי יולי יולי 15 1 Department of Computer Science-BGU.
Programming Control Flow. Sequential Program S1 S2 S5 S4 S3 int main() { Statement1; Statement2; … StatementN; } Start End.
1 Agenda - Loops while for for & while Nested Loops do-while Misc. & Questions.
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
12-2 Know how if and switch C statements control the sequence of execution of statements. Be able to use relational and logical operators in the conditional.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Programming Variables. Named area in the computer memory, intended to contain values of a certain kind (integers, real numbers, characters etc.) They.
C programming: Variables, Expressions part II. Data Types of Arithmetic Expressions Relational Expressions Logical Expressions Multiple Assignments Compound.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Conditional Statement
C programming for Engineers Lcc compiler – a free C compiler available on the web. Some instructions.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
1 If statement and relational operators –, >=, ==, != Finding min/max of 2 numbers Finding the min of 3 numbers Forming Complex relational expressions.
Incremental operators Used as a short-hand i++ or ++i  ==  i = i + 1 i-- or --i  ==  i = i – 1 i += a  ==  i = i + a i -= a  ==  i = i - a i *=
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
1 Flowchart notation and loops Implementation of loops in C –while loops –do-while loops –for loops Auxiliary Statements used inside the loops –break –continue.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
C Programming Lecture 7 : Control Structures. Control Structures Conditional statement : if, switch Determine a block of statements to execute depending.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
CSCI 171 Presentation 5. The while loop Executes a block as long as the condition is true general form: while (condition) { statement 1; statement 2;
Agenda Perform Quiz #1 (20 minutes) Loops –Introduction / Purpose –while loops Structure / Examples involving a while loop –do/while loops Structure /
1 Agenda If Statement True/False Logical Operators Nested If / Switch Exercises & Misc.
CSCI 171 Presentation 3. Operators Instructs C to perform some operation Assignment = Mathematical Relational Logical.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
CHAPTER 4 REPETITION STRUCTURES 1 st semester King Saud University College of Applied studies and Community Service Csc 1101 A.AlOsaimi.
The Repetition control structure using while loop.
C++ Laboratory Instructor: Andrey Dolgin
CSE 220 – C Programming Loops.
Chapter 4 (Conditional Statements)
Chapter 4 - Program Control
Week 4 – Repetition Structures / Loops
CSE1320 INTERMEDIATE PROGRAMMING Operators+Conditionals+Loop
מבוא כללי למדעי המחשב תרגול 2
Control Structures Lecture 7.
INC 161 , CPE 100 Computer Programming
Introduction to Programming
Looping.
- Additional C Statements
C Programming Variables.
CS1100 Computational Engineering
Chapter 4 - Program Control
A function with one argument
Incremental operators
Week 2 Variables, flow control and the Debugger
REPETITION STATEMENTS
Dale Roberts, Lecturer IUPUI
Flow of Control.
DATA TYPES There are four basic data types associated with variables:
Presentation transcript:

 Real numbers representation - Floating Point Notation  First C Program  Variables Declaration  Data Types in C ◦ char, short, int, long, float, double, unsigned int, etc  Magic sizeof() operator  prinft/scanf conversion codes  Char is a number

 int k = 5;  int *p = NULL;  p = &k;  *p = 4;  scanf(“%d”, &k);

 Selects statements to execute based on the value of an expression ◦ The expression is sometimes called the controlling expression  Selection statements: ◦ if statement ◦ switch statement

 used to execute conditionally a statement or block of code. if (expression) statement  If expression is true, statement is executed (what is true?).  statement can be replaced by a block of statements, enclosed in curly braces.

/* This program displays the absolute value of a number given by the user */ #include int main() { double num; printf("Please enter a real number: "); scanf("%lf", &num); if (num<0) num = -num; printf("The absolute value is %g\n", num); return 0; }

if (expression) statement 1 else statement 2  if expression is true, statement 1 is executed.  if expression is false, statement 2 is executed  both statements can be (and very often are) replaced by blocks of statements (“compound statements”)

int first, second, min; /* … */ if (first < second) { min = first; printf ("The first number is smaller than the second.\n"); } else { min = second; printf ("The second number is smaller than the first\n"); } printf("The smaller number is equal to %d\n", min);

 In C, every expression has a numeric value  An expression is ‘true’ when its value is non-zero  If it is zero, it is false  Therefore, in the following – if (expression) statement statement is executed if expression is non zero.

 A < B evaluates to zero if A is larger than or equal to B, and some non-zero value if A is smaller than B  Value of A+B is the sum of A and B  Assignment Operator A = B

#include int main() { int i = 3; printf("i = %d\n", i); printf("(i==7) = %d\n", i==7); printf("i = %d\n", i); printf("(i=7) = %d\n", i=7); printf("i = %d\n", i); }

 They are: ◦ A == B (Note the difference from A = B!!!!!) ◦ A != B ◦ A < B ◦ A > B ◦ A <= B ◦ A >= B  The value of the expression is non-zero if it’s true, zero if it’s false

int a, b; printf("Enter two numbers\n"); scanf("%d%d", &a, &b); if (a == b) { printf("The numbers equal %d\n", a); printf("The expression a == b is %d\n", a == b); } else { printf("The numbers are not equal\n"); printf("The expression a == b is %d\n", a == b); }

 Allows to evaluate two or more expressions ◦ !A – ‘not’ - True when A is not, and vice versa. ◦ A && B – ‘and’ - True when both A and B are true ◦ A || B – ‘or’ (inclusive or) - True when either A or B (or both) are true

#include int main() { int grade; printf("Please enter your grade: "); scanf("%d", &grade); if (grade 100) { printf(“Invalid grade!\n"); return 1; } else printf("This is a grade.\n"); return 0; }

if (expression) statement else if (expression) statement else if (expression) statement else statement

if (grade >= 90) printf ("A\n"); else if (grade >= 80) printf ("B\n"); else if (grade >= 70) printf ("C\n"); else if (grade >= 60) printf ("D\n"); else printf ("F\n");

 Input ◦ Two integers, A and B  Output ◦ Their relation (i.e. displays A==B, A B)

#include int main() { int A, B; printf("Enter two Numbers\n"); scanf("%d%d", &A, &B); if (A == B) printf("A==B\n"); else if (A > B) printf("A>B\n"); else printf("A<B\n"); return 0; }

 (expr1)? expr2 : expr3  If expr1 is true (non-zero), expr2 is evaluated. Otherwise, expr3 is evaluated

#include int main() { int i, j, min; printf("Please enter two numbers: "); scanf("%d%d", &i, &j); min = (i < j)? i : j; printf("The minimum between %d and %d is %d\n", i, j, min); return 0; }

 switch (expression) { case const-expr: statements case const-expr: statements … default: statements }

 expression must have an integer value  when the switch statement is executed: ◦ the expression is evaluated ◦ if a case matches the value of the expression, the program jumps to the first statement after that case label ◦ otherwise, the default case is selected ◦ the default is optional

switch (grade/10) { case 10: case 9: printf ("A\n"); break; case 8: printf ("B\n"); break; case 7: printf ("C\n"); break; case 6: printf ("D\n"); break; default: printf ("F\n"); }

#include int main( ) { double n1, n2, res; char op; printf("Please enter two numbers: "); scanf("%lf%lf", &n1, &n2); printf("Please enter an arithmetical operator (+, -, * or /): "); scanf(" %c", &op);

switch(op) { case '+': res = n1+n2; break; case '-': res = n1-n2; break; case '*': res = n1*n2; break; case '/': /* We're not checking for division by zero for clarity... */ res = n1/n2; break; default: printf("%c is an invalid arithmetical operator!\n", op); return 1; } /* Display the expression and its result */ printf("%g %c %g = %g\n", n1, op, n2, res); return 0; }

 Write a program that accepts a number between 1 and 100 from the user. If there is a coin of that value in cents, it should display its name. Otherwise, it should report that there is no such coin  1 = cent, 5 = nickel, 10 = dime, 25 = quarter, 100 = dollar  Remember to check for the validity of the input!

/#include int main() { int num; printf("Please enter a number from 1 to 100: "); scanf("%d", &num); /* Make sure the input is valid */ if (num 100) { printf("Invalid input!\n"); return 1; } /* Display the correct coin name, or a default message if there's no such coin */ switch (num) { case 1: printf("It's a cent!\n"); break; case 5: printf("It's a nickel!\n"); break; case 10: printf("It's a dime!\n"); break; case 25: printf("It's a quarter!\n"); break; case 100: printf("It's a whole dollar!\n"); break; default: printf("It's not a coin!\n"); } return 0; }

i++ or ++I== i = i + 1 i-- or --i == i = i – 1 i += a == i = i + a i -= a == i = i - a i *= a == i = i * a i /= a == i = i / a

 for, while, do-while loops.

while ( condition ) { statement(s); }

#include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); i=1; /* i is the counter */ while (i<=n) { fact = fact*i; i++; /* increment i */ } printf("the factorial is %d\n", fact); return 0; }

fib1 = 0; fib2 = 1; printf("%d ", fib1); while (fib2 < lim) { printf("%d ", fib2); fib_next = fib1 + fib2; fib1 = fib2; fib2 = fib_next; }

for (c = begin; c <= end; c += inc) { loop body }

#include int main() { int i, n, fact = 1; printf("Enter a number\n"); scanf("%d", &n); for (i=1; i<=n; ++i) { fact *= i; } printf("the factorial is %d\n", fact); return 0; }

 Depends on nature of the application  for – repeat operation certain number of times  while – repeat operation until some condition is true

/* Print a rectangle of #. The height and width are defined by the user */ #include int main( ) { int i, j; int height, width; printf("Please enter the two box dimensions: \n"); scanf("%d%d", &height, &width); for (i = 1; i <= height; i++) { for (j = 1; j <= width; j++) printf(“#"); printf("\n"); }

Write a program that prints an upside-down half triangle of *. The height of the pyramid is the input. ***** *** ** **** *

#include int main() { int i, j, size; printf(“Please enter a size:\n”); scanf(“%d”,&size); for (i = 1; i <= size; i++) { for (j = i; j <= size; j++) printf("*"); printf("\n"); } return 0; }

#include int main() { int i, j, last; printf("enter a number\n"); scanf("%d", &last); for (i = 2; i <= last; i++) { for (j = 2 ; j < i; j++) { if (i % j == 0) break; } if (j == i) printf("the number %d is prime\n", i); } return 0; }

#include int main() { int i; printf("Please enter a positive number.\n"); do { scanf("%d", &i); if (i <= 0) printf("Try again.\n"); } while (i<=0); /* The program continues.... */ return 0; }