Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

C for Beginner -- Part6: C Weapon logy --if/else

c beginner if if/else control statement

  • Please log in to reply
No replies to this topic

#1 kernelcoder

kernelcoder

    CC Devotee

  • Expert Member
  • PipPipPipPipPipPip
  • 990 posts
  • Location:Dhaka
  • Programming Language:C, Java, C++, C#, Visual Basic .NET
  • Learning:Objective-C, PHP, Python, Delphi/Object Pascal

Posted 13 March 2013 - 02:27 AM

So far we had come to know about basic concepts of C. Today we will start learning about C weapon logy-- C control statements. 

 

 

Control Statements

Control statements allow us to change the computer's control from automatically reading the next line of code to reading a different one. Computer programs are run sequentially from first line to last, the control statements allow us to control which statements should run and when.  Lets say we only want to run some code on some conditions.  For example, let's say, we are making a program for a bank. If someone wants to see his records, and gives his password, the program should check if the person is the right one. Firstly, we need to see if his password is correct. We  then need to create a control statement saying "if" the password is correct, then run the code to let him see the records. "else" run the code for people who enter the wrong password. So in computer programming we call these as weapon. There are several control statements in C,
  1. Conditional statements (if,if/else,else if, switch)
  2. Looping(for, while, do while)
  3. Labels (Goto) etc
 
The if/else Statement
The if/else statement is one of the most important weapon in programming. The if/else statement is used to choose a path from the two possible routes. The basic if/else code block should look like this
If(<condition>){
//some code will run when  the condition is true
}else {
//another code block will run when the if <condition> is false
}
In the if/else block the <condition> can be an expression, other functions or anything else, if the condition possess a value of non zero, yes, strictly non zero, its is treated as true otherwise false. So what will happen with this code?
if(100){
  printf("In if statement block.\n");//this code will always run,
}
 
if(-10000)
  printf("This printf will run because the condition is non zero.\n");
 
if(5>10)
  printf("This will not run");
printf("\nThis will always run.");
in the above code, the first if condition there 100 that is nonzero so the printf() function will always run. In the second if same case will happen, notice there is no '{}' (Curly BRACKET), this is a valid structure but not a good programming practice. See there is a '\n' at the end of the printf() argument, this is an ASCII character( ASCII Code = 10) used to go to the beginning of the next line. Showing some formatted output is a good programming practice. In the last if statement the condition is false, so the printf("This will not run"); will not run, but the next printf() will always run. Why? Because this printf() is not in the if block, so there is no condition to run this printf().  
#include<stdio.h>
#include<conio.h>
void main(){
	int a = 10;
	if(a == 10){
		printf("A is 10");
	}
	getch();
}

See the above program has an if statement or block, notice carefully the condition was ‘a == 10’. There is a ‘==’ operator, whenever we want some comparison there must be a ‘==’ operator in if condition. We can extend this program as using else statement with the if block.

#include<stdio.h>
#include<conio.h>
void main(){
	int a = 10;
	if(a == 10){
		printf("A is 10");
	}else {
		printf("A is not 10");
	}
	getch();
}

Notice that we used else statement at immediate after the if statement, otherwise compiler will throw an error. We cant use the else without the preceding if.

 
Lets think of a problem we discussed earlier, the leap year problem. We want to make a program to check whether a given year is leap year or not. Our program will take an user input for the year and will check if this is a leap year. Basic tasks to do:
  • Input year 
  • Check the year has a reminder after the operation year%4
  • Show the output
Input the year is easy as we learnt it in the previous part,
//this will wait for a real number is pressed, for simplicity we assume the user will input a legal year(an integer variable)
scanf(“%d”,&year);
Now we will use the if conditional statement to check the year is leap year or not. The logic is if the year%4 operation has a reminder 0 then the year is leap year otherwise not a leap year.
if(year%4 == 0){
	printf("The year %d is a leap year",year);
} else {
	printf("The year %d is not a leap year",year);
}

 

 

Exception
If the user doen't provide a legal year what will happen? Suppose a user inputs a year -1. our program will show the year is not a leap year, but we know the given year is not valid. So our program should have some validation control system for the user input. This is a good programming practice.
if(year<0){
	printf("%d is not a valid year. Program will terminate now.",year);
}else {
	if(year%4 == 0){
		printf("The year %d is a leap year",year);
	} else {
		printf("The year %d is not a leap year",year);
	}
}

 

 

Nested if/else
See the above code block, there is if/else statement in another else block. We can place if/else statements into another if or else  statement block like that. This is called nesting of a statement. Basic block of nesting of if/else statement should look like this,
If(<condition>){
   If(<some other condition>){
      //some other codes if the <some other condition> is true
   }else {
      //else some other codes 
   }
}else {
   //code will execute if the <condition> is false
}
We can use multilevel nesting as we need in our program. Lets think a program, we have three numbers and this program will find which one is the biggest.
#include<stdio.h>
#include<conio.h>
void main(){
	int a = 10, b = 20 , c = 5;
	if(a>b){
		if(a>c){
			printf("a(%d) is the biggest among-st %d %d %d ",a,a,b,c);
		}else {
			printf("c(%d) is the biggest among-st %d %d %d ",c,a,b,c);
		}
	}else {
		if(b>c){ 
			printf("b(%d) is the biggest among-st %d %d %d ",b,a,b,c);
		}else {
			printf("c(%d) is the biggest among-st %d %d %d ",c,a,b,c);
		}	
	}
getch();
}

So this program is able to find out the biggest number among three numbers. But we some time need this process done among thousands of numbers! How? We will see this in the next part.  

 

 

Tasks

  • Input a number and find out the number is even or odd.
  • Input two numbers and make a swap program(interchanging the value of two variables)
  • Test various exceptions, Check the errors occur for misplacing the if and else block in a program, Check for a number bigger than the range of integer
  • What will happen when a user inputs a character for the leap year program?  

  • 0





Also tagged with one or more of these keywords: c, beginner, if, if/else, control statement