To remind us We finished the last day by introducing If statements Their structure was:::::::::

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Character String Manipulation. Overview Character string functions sscanf() function sprintf() function.
Lecture 9. Lecture 9: Outline Strings [Kochan, chap. 10] –Character Arrays/ Character Strings –Initializing Character Strings. The null string. –Escape.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Searching and Sorting an Array 4 Searching and sorting are two fundamental algorithms often implemented with arrays –Search an array to determine the location.
Selection Statements Selects statements to execute based on the value of an expression The expression is sometimes called the controlling expression Selection.
1 Introduction to Computing Lecture 11 Character Strings Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 5, 2005.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
Some loop programs 1: Read in 10 integers and output their sum
Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters.
C. About the Crash Course Cover sufficient C for simple programs: variables and statements control functions arrays and strings pointers Slides and captured.
COMP 14 Introduction to Programming Miguel A. Otaduy May 20, 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.
JavaScript, Third Edition
1 Agenda Variables (Review) Example Input / Output Arithmetic Operations Casting Char as a Number (if time allow)
C Programming Strings. Array of characters – most common type of array in C  Let’s make them easier for use Denote the end of array using a special character.
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 13 Strings (Continued)
Introduction to C Programming Chapter 2 : Data Input, Processing and Output.
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
Decision making statements. Decision making statements are used to skip or to execute a group of statements based on the result of some condition. The.
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
Lecture 22: Reviews for Exam 2. Functions Arrays Pointers Strings C Files.
CS140: Intro to CS An Overview of Programming in C by Erin Chambers.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Arrays II (Strings). Data types in C Integer : int i; Double: double x; Float: float y; Character: char ch; char cha[10], chb[]={‘h’,’e’,’l’,’l’,’o’};
CSC141 Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture - 6.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
Strings Programming Applications. Strings in C C stores a string in a block of memory. The string is terminated by the \0 character:
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
UniMAP SEM I - 09/10EKT 120 Computer Programming1 Lecture 8 – Arrays (2) & Strings.
Gator Engineering Copyright © 2008 W. W. Norton & Company. All rights reserved. 1 Chapter 3 Formatted Input/Output.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
Dr. Sajib Datta Feb 21,  In the last class we discussed: ◦ Bubble sort  How it works  performance.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Module 6 – Decision Control Statements Objectives  Understands Increment/Decrement operators, Conditional and special operators in C  Understands significance.
1 Chapter 8 – Character Arrays and Strings Outline 8.1Introduction 8.2Declaring and Initializing String 8.3Input/output of strings 8.4String-handling Functions.
C ARRAYS.
EKT120 COMPUTER PROGRAMMING
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
- Standard C Statements
DEPARTMENT OF COMPUTER SCIENCE & APPLICATION
Lecture 8 String 1. Concept of strings String and pointers
Module 2 Arrays and strings – example programs.
Arrays in C.
Formatted and Unformatted Input/Output Functions
Arrays, For loop While loop Do while loop
Summary Two basic concepts: variables and assignments Basic types:
Exercise Arrays.
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Dale Roberts, Lecturer IUPUI
Strings Adapted from Dr. Mary Eberlein, UT Austin.
Presentation transcript:

To remind us We finished the last day by introducing If statements Their structure was:::::::::

If statements The simplest way to modify the control flow of a program is with an if statement, which in its simplest form looks like this: if(x > max) max = x; The syntax of an if statement is: If ( expression ) statement where expression is any expression and statement is any statement

Basic example #Include main() { int a; printf(“enter number \n”); scanf(“%d”,&a); if(a > 10) printf(“the number you entered %d is more than ten”,c); }

A series of statements after an if statement What if you have a series of statements, all of which should be executed together or not at all depending on whether some condition is true? The answer is that you enclose them in braces: if( expression ) { statement 1 statement 2 statement 3 }

Basic example #Include main() { int a,b,c; printf(“enter first number \n”); scanf(“%d”,&a); printf(“enter second number \n”); scanf(“%d”,&b ); c= a*b; If (c > 100) {printf(“ok \n”); printf(“the product of the numbers you entered is %d”,c); }

Expressions in If statements The expression in an if statement is a Boolean expression typically made up of Relational and Boolean operators The statements in an if stamenst are any legal statements

Relational Operators The relational operators such as, and >= are in fact operators, just like +, -, *, and /. The relational operators take two values, test them, and ``return'' a value of 1 or 0 depending on whether the tested relation was true or false. The complete set of relational operators in C is: < less than <= less than or equal > greater than >= greater than or equal == equal != not equal For example, 1 4 is 0, 5 == 5 is 1, and 6 != 6 is 0.

Boolean operators The three Boolean operators are: && and || or ! not (takes one operand; ``unary'')

If else if( expression ) statement 1 else statement 2 if (a > b) printf(a); Else printf(b);

If else if We can also have multiple specific alternatives using if … else if….. Suppose we have a variable grade containing a student's numeric grade, and we want to print out the corresponding letter grade. Here is code that would do the job: if(grade >= 90) printf("A"); else if(grade >= 80) printf("B"); else if(grade >= 70) printf("C"); else if(grade >= 60) printf("D"); else printf("F");

Basic example #Include main() { int a,b,c; printf(“enter first number \n”); scanf(“%d”,&a); printf(“enter second number \n”); scanf(“%d”,&b ); If (a > b !! a > 5) printf(“%d is greater than %d or 5”,a,b); Else printf(“%d is not greater than %d and not greater than 5”,a,b); }

Exercises Write a program which accepts an integer as input and determines whether or not it is even or odd Write a program which accepts a year as input and determines whether or not it is a leap year

Iterations and Loops The purpose of a loop is to repeat the same action a number of times We refer to each time an action is repeated as an iteration. We continue repeating the action until a terminating condition is satisfied. This terminating condition is called a loop guard The loop guard is represented by a boolean expression in the same way as the condition of an IF statement. Before the loop starts we initialise variables involved in the loop In C there are a number of ways to represent loops

For loops For loops are a common form of loop particularly for repeating a task a fixed number of times( counting loops) The syntax of a for loop is For (loop initialization, loop guard, increment) statement

Example of for loop for (i = 0; i < 10; i = i + 1) printf("i is %d\n", i); i=0 is the initialization of the loop counter i < 10 is the loop guard i.e. repeat until i is greater or equal to 10 i = i + 1; this is the counter increment ; it is more commonly written i ++ printf("i is %d\n", i); is the loop statement

While loops The most basic loop in C is the while loop. The general syntax of a while loop is while( expression ) statement The expression represents the loop guard. While it is true repeat the statement. Terminate when the expression evaluates to false

Example of a while loop #include main() {int x x = 2; while(x < 1000) { printf("%d\n", x); x = x * 2; } } This program repeatedly prints out powers of two until we generate a number greater than or equal to 1000 The terminating condition is thus when the power of two equals or exceeds 1000

An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared Int a[10] which declares an array of 10 integers whose indices go from 0-9

Putting values in arrays We can use assignment A[5] = 27 A[3] = 8 A[9] = 888

We could use scanf and a for loop #include main() { int a[10] int i for ( i = 0;i < 10 ; i++) { printf(“enter a number”); scanf(“%d”,&a[i]); } for ( i = 0;i < 10 ; i++) printf(“the %d th number in the arrray is %d \n”,i,a[i]); }

Exercise Write a program to enter 10 numbers into an array and to print out the maximum element of the array.

Strings A special kind of array is an array of characters ending in the null character \0 called string arrays A string is declared as an array of characters char s[10] char p[30] When declaring a string don’t forget to leave a space for the null character which is also known as the string terminator character

C offers four main operations on strings strcpy - copy one string into another strcat - append one string onto the right side of the other strcmp – compare alphabetic order of two strings strlen – return the length of a string

strcpy strcpy(destinationstring, sourcestring) Copies sourcestring into destinationstring For example strcpy(str, “hello world”); assigns “hello world” to the string str

strcat strcat(destinationstring, sourcestring) appends sourcestring to right hand side of destinationstring For example if str had value “a big ” strcat(str, “hello world”); appends “hello world” to the string “a big ” to get “ a big hello world”

strcpy strcmp(stringa, stringb) Compares stringa and stringb alphabetically Returns a negative value if stringa precedes stringb alphabetically Returns a positive value if stringb precedes stringa alphabetically Returns 0 if they are equal Note lowercase characters are greater than Uppercase

Input output functions of characters and strings getchar() reads a character from the screen in a non-interactive environment getche() like getchar() except interactive putchar(int ch) outputs a character to screen gets(str) gets a string from the keyboard puts(str) outputs string to screen

strlen strlen(str) returns length of string excluding null character strlen(“tttt”) = 4 not 5 since \0 not counted