int main() { int n = 1; // declaration statement n = n + 1; // expression statement printf("the value of n is %d",n); // expression statement return 0; // return statement }
C++ includes the following types of statements:
The correct way of writing syntax is: int main() { printf("welcome"); ; // null statements printf("bye"); }
An expression followed by a semicolon is a statement.
int main() { int a=10; a=a+10; // an expression statement printf("The value of a is %d",a); return 0; }
Labeled Statements:
A label is an identifier used to flag a location in a program as the target of a goto
statement or switch
statement. A label has the following syntax:
identifier : statement
case constant-expression : statement
default : statement
The scope of the label is the containing function body. Variables can have the same name as a label in the function because labels and variables have different name spaces
There are three kinds of labeled statements in C:
- Any statement preceded by a label
- A
case
statement in switch - A
default
statement in switch
Componed statements:
Compound statements or blocks are brace-enclosed sequences of statements.
control { statement; statement; statement; }
Selection/control statements:
A selection statement selects among a set of statements depending on the value of a controlling expression.
The selection statements are:
1. if
statement
2. switch
statement
There are following types of if conditional statements in C.
-
Simple If statement
-
If-Else statement
-
Nested If-else statement
-
If-Else If ladder
Simple If statement:
The statements inside the body of “if” only execute if the given condition returns true. If the condition returns false then the statements inside “if” are skipped.
Syntax of If Statement:if(condition) { // Block of if statements // these block will execute only if conditon is true. }
Diagrametic Representation of Simple If:
Example program:
/* Expected output: ---------------- case 1: if the given value is lessthan 20 Enter any integer:5 given vlaue is less than 20: the given value is : 5 case 2: if the given value is greater than or equal to 20 Enter any integer:30 the given value is : 30 */ #include<stdio.h> int main () { int a; printf("Enter any integer:"); scanf("%d",&a); if( a < 20 ) { printf("given vlaue is less than 20:\n"); } printf("the given value is : %d\n", a); return 0; }Try it your self
If else statement
Syntax of if else statement:
If condition returns true then the statements inside the body of “if” are executed and the statements inside body of “else” are skipped.
If condition returns false then the statements inside the body of “if” are skipped and the statements in “else” are executed.
if(condition) { // Statements inside body of if } else { //Statements inside body of else }
Flow diagram of if else statement
/* write a prgram to print biggest between two integer:
Expected output: ---------------- Enter any two integers: 10 20 big=20 */ #include<stdio.h> int main () { int a,b; printf("Enter any two integer:"); scanf("%d%d",&a,&b); if( a >b ) { printf("big :%d",a); } else { printf("big : %d\n", b); } return 0; }
Nested If..else statement:
When an if else statement is present inside the body of another “if” or “else” then this is called nested if else.
Syntax of Nested if else statement:
if(condition) { //Nested if else inside the body of "if" if(condition2) { //Statements inside the body of nested "if" } else { //Statements inside the body of nested "else" } } else { //Statements inside the body of "else" }
Example program for Nested If:
/* write a prgram to print biggest amont the three integer: Expected output: ---------------- Enter any two integers: 10 30 20 big=30 */ #include <stdio.h> int main () { int a,b,c; printf("Enter any three integer:"); scanf("%d%d%d",&a,&b,&c); if( a >b ) { if(a>c) // nested if statement - declared if inside another if { printf("big :%d",a); } else { printf("big :%d",c); } } else { if(b>c) { printf("big :%d",b); } else { printf("big :%d",c); } } return 0; } https://onlinegdb.com/4rVAwWnSt
If.. else..if statement
The else..if statement is useful when you need to check multiple conditions within the program, nesting of if-else blocks can be avoided using else..if statement.
Syntax of else..if statement:
if (condition1) { //These statements would execute if the condition1 is true } else if(condition2) { //These statements would execute if the condition2 is true } else if (condition3) { //These statements would execute if the condition3 is true } . . else { //These statements would execute if all the conditions return false. }
Example program for If else If Ladder: /* write a prgram to print biggest amont the four integer: Expected output: ---------------- Enter any four integer:10 20 100 3 big :100 */ #include<stdio.h> int main () { int a,b,c,d; printf("Enter any four integer:"); scanf("%d%d%d%d",&a,&b,&c,&d); if(a>b && a>c && a>>d) { printf("big :%d",a); } else if(b>a && b>c && b>d) { printf("big :%d",b); } else if(c>a && c>b && c>d) { printf("big :%d",c); } else { printf("big :%d",d); } return 0; } https://onlinegdb.com/hySYmMsDD
C – Switch Case Statement
The switch case statement is used when we have multiple options and we need to perform a different task for each option.
Before we see how a switch case statement works in a C program, let’s checkout the syntax of it.
switch (variable or an integer expression) { case constant: //C Statements ; case constant: //C Statements ; default: //C Statements ; }
Flow Diagram of Switch Case
/* customer case example - language selection: =========================================== Enter your choice from following language: 1. telugu 2. hindi 3. English 3 you have selected English Language */ #include<stdio.h> int main() { int choice; printf("Enter your choice from following language:\n "); printf("1.telugu \n 2. hindi \n 3. English"); scanf("%d",&choice); switch(choice) { case 1: printf("You have selected Telugu Language"); break; case 2: printf("You have selected Hindi Language"); break; case 3: printf("You have selected English Language"); break; default: printf("Wrong Selection"); } return 0; }
C – while loop
Syntax of while loop:
while (condition test) { //Statements to be executed repeatedly // Increment (++) or Decrement (--) Operation }
Diagram for while Loop:
Example of while loop
#include <stdio.h> int main() { int count=1; while (count <= 4) { printf("%d ", count); count++; } return 0; }
Output:
1 2 3 4
step1: The variable count is initialized with value 1 and then it has been tested for the condition.
step2: If the condition returns true then the statements inside the body of while loop are executed else control comes out of the loop.
step3: The value of count is incremented using ++ operator then it has been tested again for the loop condition.
Guess the output of this while loop
#include <stdio.h> int main() { int var=1; while (var <=2) { printf("%d ", var); } }
The program is an example of infinite while loop. Since the value of the variable var is same (there is no ++ or – operator used on this variable, inside the body of loop) the condition var<=2 will be true forever and the loop would never terminate.
Examples of infinite while loop
Example 1:
#include <stdio.h> int main() { int var = 6; while (var >=5) { printf("%d", var); var++; } return 0; }
Infinite loop: var will always have value >=5 so the loop would never end.
Example 2:
#include <stdio.h> int main() { int var =5; while (var <=10) { printf("%d", var); var--; } return 0; }
Infinite loop: var value will keep decreasing because of –- operator, hence it will always be <= 10.
Use of Logical operators in while loop
Just like relational operators (<, >, >=, <=, ! =, ==), we can also use logical operators in while loop. The following scenarios are valid :
while(num1<=10 && num2<=10)
-using AND(&&) operator, which means both the conditions should be true.
while(num1<=10||num2<=10)
– OR(||) operator, this loop will run until both conditions return false.
while(num1!=num2 &&num1 <=num2)
– Here we are using two logical operators NOT (!) and AND(&&).
while(num1!=10 ||num2>=num1)
Example of while loop using logical operator
In this example we are testing multiple conditions using logical operator inside while loop.
#include <stdio.h> int main() { int i=1, j=1; while (i <= 4 || j <= 3) { printf("%d %d\n",i, j); i++; j++; } return 0; }
Output:
1 1 2 2 3 3 4 4
C – do..while loop
A do while loop is similar to while loop with one exception that it executes the statements inside the body of do-while before checking the condition. On the other hand in the while loop, first the condition is checked and then the statements in while loop are executed. So you can say that if a condition is false at the first place then the do while would run once, however the while loop would not run at all.
Syntax of do-while loop
do { //Statements }while(condition test);
Flow diagram of do while loop
Example of do while loop
#include <stdio.h> int main() { int j=0; do { printf("Value of variable j is: %d\n", j); j++; }while (j<=3); return 0; }
Output:
Value of variable j is: 0 Value of variable j is: 1 Value of variable j is: 2 Value of variable j is: 3
While vs do..while loop in C
Using while loop:
#include <stdio.h> int main() { int i=0; while(i==1) { printf("while vs do-while"); } printf("Out of loop"); }
Output:
Out of loop
Same example using do-while loop
#include <stdio.h> int main() { int i=0; do { printf("while vs do-while\n"); }while(i==1); printf("Out of loop"); }
Output:
while vs do-while Out of loop
Explanation: As I mentioned in the beginning of this guide that do-while runs at least once even if the condition is false because the condition is evaluated, after the execution of the body of loop.
C For loop
This is one of the most frequently used loop in C programming.
Syntax of for loop:
for (initialization; condition test; increment or decrement) { //Statements to be executed repeatedly }
Flow Diagram of For loop:
Step 1: First initialization happens and the counter variable gets initialized.
Step 2: In the second step the condition is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed, if the condition returns false then the for loop gets terminated and the control comes out of the loop.
Step 3: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –).
Example of For loop
#include <stdio.h> int main() { int i; for (i=1; i<=3; i++) { printf("%d\n", i); } return 0; }
Output:
1 2 3
Various forms of for loop in C
I am using variable num as the counter in all the following examples –
1) Here instead of num++, I’m using num=num+1 which is same as num++.
for (num=10; num<20; num=num+1)
2) Initialization part can be skipped from loop as shown below, the counter variable is declared before the loop.
int num=10; for (;num<20;num++)
Note: Even though we can skip initialization part but semicolon (;) before condition is must, without which you will get compilation error.
3) Like initialization, you can also skip the increment part as we did below. In this case semicolon (;) is must after condition logic. In this case the increment or decrement part is done inside the loop.
for (num=10; num<20; ) { //Statements num++; }
4) This is also possible. The counter variable is initialized before the loop and incremented inside the loop.
int num=10; for (;num<20;) { //Statements num++; }
5) As mentioned above, the counter variable can be decremented as well. In the below example the variable gets decremented each time the loop runs until the condition num>10 returns false.
for(num=20; num>10; num--)
Nested For Loop in C
Nesting of loop is also possible. Lets take an example to understand this:
#include <stdio.h> int main() { for (int i=0; i<2; i++) { for (int j=0; j<4; j++) { printf("%d, %d\n",i ,j); } } return 0; }
Output:
0, 0 0, 1 0, 2 0, 3 1, 0 1, 1 1, 2 1, 3
In the above example we have a for loop inside another for loop, this is called nesting of loops. One of the example where we use nested for loop is Two dimensional array.
Multiple initialization inside for Loop in C
We can have multiple initialization in the for loop as shown below.
for (i=1,j=1;i<10 && j<10; i++, j++)
What’s the difference between above for loop and a simple for loop?
1. It is initializing two variables. Note: both are separated by comma (,).
2. It has two test conditions joined together using AND (&&) logical operator. Note: You cannot use multiple test conditions separated by comma, you must use logical operator such as && or || to join conditions.
3. It has two variables in increment part. Note: Should be separated by comma.
Example of for loop with multiple test conditions
#include <stdio.h> int main() { int i,j; for (i=1,j=1 ; i<3 || j<5; i++,j++) { printf("%d, %d\n",i ,j); } return 0; }
Comments
Post a Comment